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