Commit | Line | Data |
---|---|---|
cd872296 RK |
1 | /** |
2 | * @fileoverview Test cases for DygraphOptions. | |
3 | */ | |
e8c70e4e DV |
4 | |
5 | import Dygraph from '../../src/dygraph'; | |
6 | import DygraphOptions from '../../src/dygraph-options'; | |
7 | ||
89fdcedb | 8 | describe("dygraph-options-tests", function() { |
cd872296 | 9 | |
e8c70e4e DV |
10 | cleanupAfterEach(); |
11 | ||
89fdcedb DV |
12 | var graph; |
13 | ||
14 | beforeEach(function() { | |
89fdcedb DV |
15 | graph = document.getElementById("graph"); |
16 | }); | |
cd872296 | 17 | |
cd872296 RK |
18 | /* |
19 | * Pathalogical test to ensure getSeriesNames works | |
20 | */ | |
89fdcedb | 21 | it('testGetSeriesNames', function() { |
cd872296 RK |
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. | |
cd872296 RK |
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); | |
89fdcedb DV |
35 | assert.deepEqual(["Y", "Y2", "Y3"], o.seriesNames()); |
36 | }); | |
5b9b2142 RK |
37 | |
38 | /* | |
39 | * Ensures that even if logscale is set globally, it doesn't impact the | |
40 | * x axis. | |
41 | */ | |
89fdcedb | 42 | it('testGetLogscaleForX', function() { |
5b9b2142 RK |
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. | |
5b9b2142 RK |
51 | var g = new Dygraph(graph, data, opts); |
52 | ||
89fdcedb DV |
53 | assert.isFalse(!!g.getOptionForAxis('logscale', 'x')); |
54 | assert.isFalse(!!g.getOptionForAxis('logscale', 'y')); | |
5b9b2142 RK |
55 | |
56 | g.updateOptions({ logscale : true }); | |
89fdcedb DV |
57 | assert.isFalse(!!g.getOptionForAxis('logscale', 'x')); |
58 | assert.isTrue(!!g.getOptionForAxis('logscale', 'y')); | |
59 | }); | |
b3cb0794 DV |
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 | }; | |
ac3070f8 DV |
70 | try { |
71 | new Dygraph(graph, data, opts); | |
72 | } catch (e) { | |
73 | } | |
b3cb0794 DV |
74 | console.warn = oldWarn; |
75 | return warnings; | |
76 | }; | |
77 | ||
89fdcedb | 78 | it('testLogWarningForNonexistentOption', function() { |
b3cb0794 DV |
79 | if (typeof(Dygraph.OPTIONS_REFERENCE) === 'undefined') { |
80 | return; // this test won't pass in non-debug mode. | |
81 | } | |
82 | ||
b3cb0794 DV |
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); | |
89fdcedb | 89 | assert.deepEqual(['Unknown option ' + badOptionName], warnings); |
b3cb0794 DV |
90 | }; |
91 | var expectNoWarning = function(opts) { | |
92 | DygraphOptions.resetWarnings_(); | |
93 | var warnings = getWarnings(graph, data, opts); | |
89fdcedb | 94 | assert.deepEqual([], warnings); |
b3cb0794 DV |
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}); | |
89fdcedb | 105 | }); |
b3cb0794 | 106 | |
89fdcedb | 107 | it('testOnlyLogsEachWarningOnce', function() { |
b3cb0794 DV |
108 | if (typeof(Dygraph.OPTIONS_REFERENCE) === 'undefined') { |
109 | return; // this test won't pass in non-debug mode. | |
110 | } | |
111 | ||
b3cb0794 DV |
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}); | |
89fdcedb DV |
117 | assert.deepEqual(['Unknown option nonExistent'], warnings1); |
118 | assert.deepEqual([], warnings2); | |
119 | }); | |
120 | ||
121 | }); |