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