Merge pull request #469 from danvk/re-smooth-plotter
[dygraphs.git] / auto_tests / tests / update_options.js
CommitLineData
9ca829f2
DV
1// Copyright 2011 Google Inc. All Rights Reserved.
2
3/**
4 * @fileoverview Tests for the updateOptions function.
5 * @author antrob@google.com (Anthony Robledo)
6 */
7var UpdateOptionsTestCase = TestCase("update-options");
8
9UpdateOptionsTestCase.prototype.opts = {
10 width: 480,
11 height: 320,
12};
13
14UpdateOptionsTestCase.prototype.data = "X,Y1,Y2\n" +
15 "2011-01-01,2,3\n" +
16 "2011-02-02,5,3\n" +
17 "2011-03-03,6,1\n" +
18 "2011-04-04,9,5\n" +
19 "2011-05-05,8,3\n";
20
21UpdateOptionsTestCase.prototype.setUp = function() {
1a27bd14 22 document.body.innerHTML = "<div id='graph'></div><div id='labels'></div>";
9ca829f2
DV
23};
24
25UpdateOptionsTestCase.prototype.tearDown = function() {
26};
27
8c5d3797
RK
28/*
29 * Tweaks the dygraph so it sets g._testDrawCalled to true when internal method
30 * drawGraph_ is called. Call unWrapDrawGraph when done with this.
31 */
32UpdateOptionsTestCase.prototype.wrapDrawGraph = function(g) {
9ca829f2 33 g._testDrawCalled = false;
8c5d3797
RK
34 g._oldDrawGraph = g.drawGraph_;
35 g.drawGraph_ = function() {
9ca829f2 36 g._testDrawCalled = true;
8c5d3797 37 g._oldDrawGraph.call(g);
9ca829f2 38 }
9ca829f2
DV
39}
40
8c5d3797
RK
41/*
42 * See wrapDrawGraph
43 */
44UpdateOptionsTestCase.prototype.unwrapDrawGraph = function(g) {
45 g.drawGraph_ = g._oldDrawGraph;
9ca829f2
DV
46}
47
48UpdateOptionsTestCase.prototype.testStrokeAll = function() {
49 var graphDiv = document.getElementById("graph");
50 var graph = new Dygraph(graphDiv, this.data, this.opts);
51 var updatedOptions = { };
52
53 updatedOptions['strokeWidth'] = 3;
54
55 // These options will allow us to jump to renderGraph_()
56 // drawGraph_() will be skipped.
8c5d3797 57 this.wrapDrawGraph(graph);
9ca829f2 58 graph.updateOptions(updatedOptions);
8c5d3797 59 this.unwrapDrawGraph(graph);
9ca829f2
DV
60 assertFalse(graph._testDrawCalled);
61};
62
63UpdateOptionsTestCase.prototype.testStrokeSingleSeries = function() {
64 var graphDiv = document.getElementById("graph");
65 var graph = new Dygraph(graphDiv, this.data, this.opts);
66 var updatedOptions = { };
67 var optionsForY1 = { };
68
69 optionsForY1['strokeWidth'] = 3;
70 updatedOptions['Y1'] = optionsForY1;
71
72 // These options will allow us to jump to renderGraph_()
73 // drawGraph_() will be skipped.
8c5d3797 74 this.wrapDrawGraph(graph);
9ca829f2 75 graph.updateOptions(updatedOptions);
8c5d3797 76 this.unwrapDrawGraph(graph);
9ca829f2
DV
77 assertFalse(graph._testDrawCalled);
78};
79
80UpdateOptionsTestCase.prototype.testSingleSeriesRequiresNewPoints = function() {
81 var graphDiv = document.getElementById("graph");
82 var graph = new Dygraph(graphDiv, this.data, this.opts);
83 var updatedOptions = { };
84 var optionsForY1 = { };
85 var optionsForY2 = { };
86
87 // This will not require new points.
88 optionsForY1['strokeWidth'] = 2;
89 updatedOptions['Y1'] = optionsForY1;
90
91 // This will require new points.
92 optionsForY2['stepPlot'] = true;
93 updatedOptions['Y2'] = optionsForY2;
94
95 // These options will not allow us to jump to renderGraph_()
96 // drawGraph_() must be called
8c5d3797 97 this.wrapDrawGraph(graph);
9ca829f2 98 graph.updateOptions(updatedOptions);
8c5d3797 99 this.unwrapDrawGraph(graph);
9ca829f2
DV
100 assertTrue(graph._testDrawCalled);
101};
102
103UpdateOptionsTestCase.prototype.testWidthChangeNeedsNewPoints = function() {
104 var graphDiv = document.getElementById("graph");
105 var graph = new Dygraph(graphDiv, this.data, this.opts);
106 var updatedOptions = { };
107
108 // This will require new points.
109 updatedOptions['width'] = 600;
110
111 // These options will not allow us to jump to renderGraph_()
112 // drawGraph_() must be called
8c5d3797 113 this.wrapDrawGraph(graph);
9ca829f2 114 graph.updateOptions(updatedOptions);
8c5d3797 115 this.unwrapDrawGraph(graph);
9ca829f2
DV
116 assertTrue(graph._testDrawCalled);
117};
66ad3609
RK
118
119// Test https://github.com/danvk/dygraphs/issues/87
120UpdateOptionsTestCase.prototype.testUpdateLabelsDivDoesntInfiniteLoop = function() {
121 var graphDiv = document.getElementById("graph");
122 var labelsDiv = document.getElementById("labels");
123 var graph = new Dygraph(graphDiv, this.data, this.opts);
124 graph.updateOptions({labelsDiv : labelsDiv});
125}
126
8c5d3797
RK
127// Test https://github.com/danvk/dygraphs/issues/247
128UpdateOptionsTestCase.prototype.testUpdateColors = function() {
129 var graphDiv = document.getElementById("graph");
130 var graph = new Dygraph(graphDiv, this.data, this.opts);
131
132 var defaultColors = ["rgb(0,128,0)", "rgb(0,0,128)"];
133 assertEquals(["rgb(0,128,0)", "rgb(0,0,128)"], graph.getColors());
134
135 var colors1 = [ "#aaa", "#bbb" ];
136 graph.updateOptions({ colors: colors1 });
137 assertEquals(colors1, graph.getColors());
138
a8f4eb62 139 // extra colors are ignored until you add additional data series.
45efb726 140 var colors2 = [ "#ddd", "#eee", "#fff" ];
8c5d3797 141 graph.updateOptions({ colors: colors2 });
45efb726 142 assertEquals(["#ddd", "#eee"], graph.getColors());
8c5d3797 143
a8f4eb62
DV
144 graph.updateOptions({ file:
145 "X,Y1,Y2,Y3\n" +
146 "2011-01-01,2,3,4\n" +
147 "2011-02-02,5,3,2\n"
148 });
149 assertEquals(colors2, graph.getColors());
8c5d3797 150
a8f4eb62 151 graph.updateOptions({ colors: null, file: this.data });
8c5d3797
RK
152 assertEquals(defaultColors, graph.getColors());
153}
4a0cb9c4
DV
154
155// Regression test for http://code.google.com/p/dygraphs/issues/detail?id=249
156// Verifies that setting 'legend: always' via update immediately shows the
157// legend.
158UpdateOptionsTestCase.prototype.testUpdateLegendAlways = function() {
159 var graphDiv = document.getElementById("graph");
160 var graph = new Dygraph(graphDiv, this.data, this.opts);
161
162 var legend = document.getElementsByClassName("dygraph-legend");
163 assertEquals(1, legend.length);
164 legend = legend[0];
165 assertEquals("", legend.innerHTML);
166
167 graph.updateOptions({legend: 'always'});
168
169 legend = document.getElementsByClassName("dygraph-legend");
170 assertEquals(1, legend.length);
171 legend = legend[0];
172 assertNotEquals(-1, legend.textContent.indexOf("Y1"));
173 assertNotEquals(-1, legend.textContent.indexOf("Y2"));
174};