1 // Copyright 2011 Google Inc. All Rights Reserved.
4 * @fileoverview Tests for the updateOptions function.
5 * @author antrob@google.com (Anthony Robledo)
7 var UpdateOptionsTestCase
= TestCase("update-options");
9 UpdateOptionsTestCase
.prototype.opts
= {
14 UpdateOptionsTestCase
.prototype.data
= "X,Y1,Y2\n" +
21 UpdateOptionsTestCase
.prototype.setUp
= function() {
22 document
.body
.innerHTML
= "<div id='graph'></div><div id='labels'></div>";
25 UpdateOptionsTestCase
.prototype.tearDown
= function() {
29 * Tweaks the dygraph so it sets g._testDrawCalled to true when internal method
30 * drawGraph_ is called. Call unWrapDrawGraph when done with this.
32 UpdateOptionsTestCase
.prototype.wrapDrawGraph
= function(g
) {
33 g
._testDrawCalled
= false;
34 g
._oldDrawGraph
= g
.drawGraph_
;
35 g
.drawGraph_
= function() {
36 g
._testDrawCalled
= true;
37 g
._oldDrawGraph
.call(g
);
44 UpdateOptionsTestCase
.prototype.unwrapDrawGraph
= function(g
) {
45 g
.drawGraph_
= g
._oldDrawGraph
;
48 UpdateOptionsTestCase
.prototype.testStrokeAll
= function() {
49 var graphDiv
= document
.getElementById("graph");
50 var graph
= new Dygraph(graphDiv
, this.data
, this.opts
);
51 var updatedOptions
= { };
53 updatedOptions
['strokeWidth'] = 3;
55 // These options will allow us to jump to renderGraph_()
56 // drawGraph_() will be skipped.
57 this.wrapDrawGraph(graph
);
58 graph
.updateOptions(updatedOptions
);
59 this.unwrapDrawGraph(graph
);
60 assertFalse(graph
._testDrawCalled
);
63 UpdateOptionsTestCase
.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
= { };
69 optionsForY1
['strokeWidth'] = 3;
70 updatedOptions
['series'] = {'Y1': optionsForY1
};
72 // These options will allow us to jump to renderGraph_()
73 // drawGraph_() will be skipped.
74 this.wrapDrawGraph(graph
);
75 graph
.updateOptions(updatedOptions
);
76 this.unwrapDrawGraph(graph
);
77 assertFalse(graph
._testDrawCalled
);
80 UpdateOptionsTestCase
.prototype.testSingleSeriesRequiresNewPoints
= function() {
81 var graphDiv
= document
.getElementById("graph");
82 var graph
= new Dygraph(graphDiv
, this.data
, this.opts
);
83 var updatedOptions
= {
94 // These options will not allow us to jump to renderGraph_()
95 // drawGraph_() must be called
96 this.wrapDrawGraph(graph
);
97 graph
.updateOptions(updatedOptions
);
98 this.unwrapDrawGraph(graph
);
99 assertTrue(graph
._testDrawCalled
);
102 UpdateOptionsTestCase
.prototype.testWidthChangeNeedsNewPoints
= function() {
103 var graphDiv
= document
.getElementById("graph");
104 var graph
= new Dygraph(graphDiv
, this.data
, this.opts
);
105 var updatedOptions
= { };
107 // This will require new points.
108 updatedOptions
['width'] = 600;
110 // These options will not allow us to jump to renderGraph_()
111 // drawGraph_() must be called
112 this.wrapDrawGraph(graph
);
113 graph
.updateOptions(updatedOptions
);
114 this.unwrapDrawGraph(graph
);
115 assertTrue(graph
._testDrawCalled
);
118 // Test https://github.com/danvk/dygraphs/issues/87
119 UpdateOptionsTestCase
.prototype.testUpdateLabelsDivDoesntInfiniteLoop
= function() {
120 var graphDiv
= document
.getElementById("graph");
121 var labelsDiv
= document
.getElementById("labels");
122 var graph
= new Dygraph(graphDiv
, this.data
, this.opts
);
123 graph
.updateOptions({labelsDiv
: labelsDiv
});
126 // Test https://github.com/danvk/dygraphs/issues/247
127 UpdateOptionsTestCase
.prototype.testUpdateColors
= function() {
128 var graphDiv
= document
.getElementById("graph");
129 var graph
= new Dygraph(graphDiv
, this.data
, this.opts
);
131 var defaultColors
= ["rgb(0,128,0)", "rgb(0,0,128)"];
132 assertEquals(["rgb(0,128,0)", "rgb(0,0,128)"], graph
.getColors());
134 var colors1
= [ "#aaa", "#bbb" ];
135 graph
.updateOptions({ colors
: colors1
});
136 assertEquals(colors1
, graph
.getColors());
138 // extra colors are ignored until you add additional data series.
139 var colors2
= [ "#ddd", "#eee", "#fff" ];
140 graph
.updateOptions({ colors
: colors2
});
141 assertEquals(["#ddd", "#eee"], graph
.getColors());
143 graph
.updateOptions({ file
:
145 "2011-01-01,2,3,4\n" +
148 assertEquals(colors2
, graph
.getColors());
150 graph
.updateOptions({ colors
: null, file
: this.data
});
151 assertEquals(defaultColors
, graph
.getColors());
154 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=249
155 // Verifies that setting 'legend: always' via update immediately shows the
157 UpdateOptionsTestCase
.prototype.testUpdateLegendAlways
= function() {
158 var graphDiv
= document
.getElementById("graph");
159 var graph
= new Dygraph(graphDiv
, this.data
, this.opts
);
161 var legend
= document
.getElementsByClassName("dygraph-legend");
162 assertEquals(1, legend
.length
);
164 assertEquals("", legend
.innerHTML
);
166 graph
.updateOptions({legend
: 'always'});
168 legend
= document
.getElementsByClassName("dygraph-legend");
169 assertEquals(1, legend
.length
);
171 assertNotEquals(-1, legend
.textContent
.indexOf("Y1"));
172 assertNotEquals(-1, legend
.textContent
.indexOf("Y2"));