code review
[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 };
67 new Dygraph(graph, data, opts);
68 console.warn = oldWarn;
69 return warnings;
70};
71
72DygraphOptionsTestCase.prototype.testLogWarningForNonexistentOption = function() {
73 if (typeof(Dygraph.OPTIONS_REFERENCE) === 'undefined') {
74 return; // this test won't pass in non-debug mode.
75 }
76
77 var graph = document.getElementById("graph");
78 var data = "X,Y,Y2,Y3\n" +
79 "1,-1,2,3";
80
81 var expectWarning = function(opts, badOptionName) {
82 DygraphOptions.resetWarnings_();
83 var warnings = getWarnings(graph, data, opts);
84 assertEquals(['Unknown option ' + badOptionName], warnings);
85 };
86 var expectNoWarning = function(opts) {
87 DygraphOptions.resetWarnings_();
88 var warnings = getWarnings(graph, data, opts);
89 assertEquals([], warnings);
90 };
91
92 expectNoWarning({});
93 expectWarning({nonExistentOption: true}, 'nonExistentOption');
94 expectWarning({series: {Y: {nonExistentOption: true}}}, 'nonExistentOption');
95 // expectWarning({Y: {nonExistentOption: true}});
96 expectWarning({axes: {y: {anotherNonExistentOption: true}}}, 'anotherNonExistentOption');
97 expectWarning({highlightSeriesOpts: {anotherNonExistentOption: true}}, 'anotherNonExistentOption');
98 expectNoWarning({highlightSeriesOpts: {strokeWidth: 20}});
99 expectNoWarning({strokeWidth: 20});
100};
101
102DygraphOptionsTestCase.prototype.testOnlyLogsEachWarningOnce = function() {
103 if (typeof(Dygraph.OPTIONS_REFERENCE) === 'undefined') {
104 return; // this test won't pass in non-debug mode.
105 }
106
107 var graph = document.getElementById("graph");
108 var data = "X,Y,Y2,Y3\n" +
109 "1,-1,2,3";
110
111 var warnings1 = getWarnings(graph, data, {nonExistent: true});
112 var warnings2 = getWarnings(graph, data, {nonExistent: true});
113 assertEquals(['Unknown option nonExistent'], warnings1);
114 assertEquals([], warnings2);
5b9b2142 115};