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