1 // Copyright 2011 Google Inc. All Rights Reserved.
4 * @fileoverview Tests for the updateOptions function.
5 * @author antrob@google.com (Anthony Robledo)
8 import Dygraph from
'../../src/dygraph';
10 describe("update-options", function() {
19 var data
= "X,Y1,Y2\n" +
27 * Tweaks the dygraph so it sets g._testDrawCalled to true when internal method
28 * drawGraph_ is called. Call unWrapDrawGraph when done with this.
30 var wrapDrawGraph
= function(g
) {
31 g
._testDrawCalled
= false;
32 g
._oldDrawGraph
= g
.drawGraph_
;
33 g
.drawGraph_
= function() {
34 g
._testDrawCalled
= true;
35 g
._oldDrawGraph
.call(g
);
42 var unwrapDrawGraph
= function(g
) {
43 g
.drawGraph_
= g
._oldDrawGraph
;
46 it('testStrokeAll', function() {
47 var graphDiv
= document
.getElementById("graph");
48 var graph
= new Dygraph(graphDiv
, data
, opts
);
49 var updatedOptions
= { };
51 updatedOptions
['strokeWidth'] = 3;
53 // These options will allow us to jump to renderGraph_()
54 // drawGraph_() will be skipped.
56 graph
.updateOptions(updatedOptions
);
57 unwrapDrawGraph(graph
);
58 assert
.isFalse(graph
._testDrawCalled
);
61 it('testStrokeSingleSeries', function() {
62 var graphDiv
= document
.getElementById("graph");
63 var graph
= new Dygraph(graphDiv
, data
, opts
);
64 var updatedOptions
= { };
65 var optionsForY1
= { };
67 optionsForY1
['strokeWidth'] = 3;
68 updatedOptions
['series'] = {'Y1': optionsForY1
};
70 // These options will allow us to jump to renderGraph_()
71 // drawGraph_() will be skipped.
73 graph
.updateOptions(updatedOptions
);
74 unwrapDrawGraph(graph
);
75 assert
.isFalse(graph
._testDrawCalled
);
78 it('testSingleSeriesRequiresNewPoints', function() {
79 var graphDiv
= document
.getElementById("graph");
80 var graph
= new Dygraph(graphDiv
, data
, opts
);
81 var updatedOptions
= {
92 // These options will not allow us to jump to renderGraph_()
93 // drawGraph_() must be called
95 graph
.updateOptions(updatedOptions
);
96 unwrapDrawGraph(graph
);
97 assert
.isTrue(graph
._testDrawCalled
);
100 it('testWidthChangeNeedsNewPoints', function() {
101 var graphDiv
= document
.getElementById("graph");
102 var graph
= new Dygraph(graphDiv
, data
, opts
);
103 var updatedOptions
= { };
105 // This will require new points.
106 updatedOptions
['width'] = 600;
108 // These options will not allow us to jump to renderGraph_()
109 // drawGraph_() must be called
110 wrapDrawGraph(graph
);
111 graph
.updateOptions(updatedOptions
);
112 unwrapDrawGraph(graph
);
113 assert
.isTrue(graph
._testDrawCalled
);
116 // Test https://github.com/danvk/dygraphs/issues/87
117 it('testUpdateLabelsDivDoesntInfiniteLoop', function() {
118 var graphDiv
= document
.getElementById("graph");
119 var labelsDiv
= document
.getElementById("labels");
120 var graph
= new Dygraph(graphDiv
, data
, opts
);
121 graph
.updateOptions({labelsDiv
: labelsDiv
});
124 // Test https://github.com/danvk/dygraphs/issues/247
125 it('testUpdateColors', function() {
126 var graphDiv
= document
.getElementById("graph");
127 var graph
= new Dygraph(graphDiv
, data
, opts
);
129 var defaultColors
= ["rgb(0,128,0)", "rgb(0,0,128)"];
130 assert
.deepEqual(["rgb(0,128,0)", "rgb(0,0,128)"], graph
.getColors());
132 var colors1
= [ "#aaa", "#bbb" ];
133 graph
.updateOptions({ colors
: colors1
});
134 assert
.deepEqual(colors1
, graph
.getColors());
136 // extra colors are ignored until you add additional data series.
137 var colors2
= [ "#ddd", "#eee", "#fff" ];
138 graph
.updateOptions({ colors
: colors2
});
139 assert
.deepEqual(["#ddd", "#eee"], graph
.getColors());
141 graph
.updateOptions({ file
:
143 "2011-01-01,2,3,4\n" +
146 assert
.deepEqual(colors2
, graph
.getColors());
148 graph
.updateOptions({ colors
: null, file
: data
});
149 assert
.deepEqual(defaultColors
, graph
.getColors());
152 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=249
153 // Verifies that setting 'legend: always' via update immediately shows the
155 it('testUpdateLegendAlways', function() {
156 var graphDiv
= document
.getElementById("graph");
157 var graph
= new Dygraph(graphDiv
, data
, opts
);
159 var legend
= document
.getElementsByClassName("dygraph-legend");
160 assert
.equal(1, legend
.length
);
162 assert
.equal("", legend
.innerHTML
);
164 graph
.updateOptions({legend
: 'always'});
166 legend
= document
.getElementsByClassName("dygraph-legend");
167 assert
.equal(1, legend
.length
);
169 assert
.notEqual(-1, legend
.textContent
.indexOf("Y1"));
170 assert
.notEqual(-1, legend
.textContent
.indexOf("Y2"));