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