Fork off Karma file for in-browser debug
[dygraphs.git] / auto_tests / tests / dygraph-options-tests.js
1 /**
2 * @fileoverview Test cases for DygraphOptions.
3 */
4 describe("dygraph-options-tests", function() {
5
6 var graph;
7
8 beforeEach(function() {
9 document.body.innerHTML = "<div id='graph'></div>";
10 graph = document.getElementById("graph");
11 });
12
13 afterEach(function() {
14 });
15
16 /*
17 * Pathalogical test to ensure getSeriesNames works
18 */
19 it('testGetSeriesNames', function() {
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.
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);
33 assert.deepEqual(["Y", "Y2", "Y3"], o.seriesNames());
34 });
35
36 /*
37 * Ensures that even if logscale is set globally, it doesn't impact the
38 * x axis.
39 */
40 it('testGetLogscaleForX', function() {
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.
49 var g = new Dygraph(graph, data, opts);
50
51 assert.isFalse(!!g.getOptionForAxis('logscale', 'x'));
52 assert.isFalse(!!g.getOptionForAxis('logscale', 'y'));
53
54 g.updateOptions({ logscale : true });
55 assert.isFalse(!!g.getOptionForAxis('logscale', 'x'));
56 assert.isTrue(!!g.getOptionForAxis('logscale', 'y'));
57 });
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 };
68 try {
69 new Dygraph(graph, data, opts);
70 } catch (e) {
71 }
72 console.warn = oldWarn;
73 return warnings;
74 };
75
76 it('testLogWarningForNonexistentOption', function() {
77 if (typeof(Dygraph.OPTIONS_REFERENCE) === 'undefined') {
78 return; // this test won't pass in non-debug mode.
79 }
80
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 assert.deepEqual(['Unknown option ' + badOptionName], warnings);
88 };
89 var expectNoWarning = function(opts) {
90 DygraphOptions.resetWarnings_();
91 var warnings = getWarnings(graph, data, opts);
92 assert.deepEqual([], 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
105 it('testOnlyLogsEachWarningOnce', function() {
106 if (typeof(Dygraph.OPTIONS_REFERENCE) === 'undefined') {
107 return; // this test won't pass in non-debug mode.
108 }
109
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});
115 assert.deepEqual(['Unknown option nonExistent'], warnings1);
116 assert.deepEqual([], warnings2);
117 });
118
119 });