Rewrite tests to use ES6 modules.
[dygraphs.git] / auto_tests / tests / dygraph-options-tests.js
1 /**
2 * @fileoverview Test cases for DygraphOptions.
3 */
4
5 import Dygraph from '../../src/dygraph';
6 import DygraphOptions from '../../src/dygraph-options';
7
8 describe("dygraph-options-tests", function() {
9
10 cleanupAfterEach();
11
12 var graph;
13
14 beforeEach(function() {
15 graph = document.getElementById("graph");
16 });
17
18 /*
19 * Pathalogical test to ensure getSeriesNames works
20 */
21 it('testGetSeriesNames', function() {
22 var opts = {
23 width: 480,
24 height: 320
25 };
26 var data = "X,Y,Y2,Y3\n" +
27 "0,-1,0,0";
28
29 // Kind of annoying that you need a DOM to test the object.
30 var g = new Dygraph(graph, data, opts);
31
32 // We don't need to get at g's attributes_ object just
33 // to test DygraphOptions.
34 var o = new DygraphOptions(g);
35 assert.deepEqual(["Y", "Y2", "Y3"], o.seriesNames());
36 });
37
38 /*
39 * Ensures that even if logscale is set globally, it doesn't impact the
40 * x axis.
41 */
42 it('testGetLogscaleForX', function() {
43 var opts = {
44 width: 480,
45 height: 320
46 };
47 var data = "X,Y,Y2,Y3\n" +
48 "1,-1,2,3";
49
50 // Kind of annoying that you need a DOM to test the object.
51 var g = new Dygraph(graph, data, opts);
52
53 assert.isFalse(!!g.getOptionForAxis('logscale', 'x'));
54 assert.isFalse(!!g.getOptionForAxis('logscale', 'y'));
55
56 g.updateOptions({ logscale : true });
57 assert.isFalse(!!g.getOptionForAxis('logscale', 'x'));
58 assert.isTrue(!!g.getOptionForAxis('logscale', 'y'));
59 });
60
61 // Helper to gather all warnings emitted by Dygraph constructor.
62 // Removes everything after the first open parenthesis in each warning.
63 // Returns them in a (possibly empty) list.
64 var getWarnings = function(div, data, opts) {
65 var warnings = [];
66 var oldWarn = console.warn;
67 console.warn = function(message) {
68 warnings.push(message.replace(/ \(.*/, ''));
69 };
70 try {
71 new Dygraph(graph, data, opts);
72 } catch (e) {
73 }
74 console.warn = oldWarn;
75 return warnings;
76 };
77
78 it('testLogWarningForNonexistentOption', function() {
79 if (typeof(Dygraph.OPTIONS_REFERENCE) === 'undefined') {
80 return; // this test won't pass in non-debug mode.
81 }
82
83 var data = "X,Y,Y2,Y3\n" +
84 "1,-1,2,3";
85
86 var expectWarning = function(opts, badOptionName) {
87 DygraphOptions.resetWarnings_();
88 var warnings = getWarnings(graph, data, opts);
89 assert.deepEqual(['Unknown option ' + badOptionName], warnings);
90 };
91 var expectNoWarning = function(opts) {
92 DygraphOptions.resetWarnings_();
93 var warnings = getWarnings(graph, data, opts);
94 assert.deepEqual([], warnings);
95 };
96
97 expectNoWarning({});
98 expectWarning({nonExistentOption: true}, 'nonExistentOption');
99 expectWarning({series: {Y: {nonExistentOption: true}}}, 'nonExistentOption');
100 // expectWarning({Y: {nonExistentOption: true}});
101 expectWarning({axes: {y: {anotherNonExistentOption: true}}}, 'anotherNonExistentOption');
102 expectWarning({highlightSeriesOpts: {anotherNonExistentOption: true}}, 'anotherNonExistentOption');
103 expectNoWarning({highlightSeriesOpts: {strokeWidth: 20}});
104 expectNoWarning({strokeWidth: 20});
105 });
106
107 it('testOnlyLogsEachWarningOnce', function() {
108 if (typeof(Dygraph.OPTIONS_REFERENCE) === 'undefined') {
109 return; // this test won't pass in non-debug mode.
110 }
111
112 var data = "X,Y,Y2,Y3\n" +
113 "1,-1,2,3";
114
115 var warnings1 = getWarnings(graph, data, {nonExistent: true});
116 var warnings2 = getWarnings(graph, data, {nonExistent: true});
117 assert.deepEqual(['Unknown option nonExistent'], warnings1);
118 assert.deepEqual([], warnings2);
119 });
120
121 });