consolidating scripts
[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 */
e8c70e4e
DV
7
8import Dygraph from '../../src/dygraph';
9
89fdcedb 10describe("update-options", function() {
e8c70e4e
DV
11
12cleanupAfterEach();
9ca829f2 13
319d0361 14var opts = {
9ca829f2
DV
15 width: 480,
16 height: 320,
17};
18
319d0361 19var data = "X,Y1,Y2\n" +
9ca829f2
DV
20 "2011-01-01,2,3\n" +
21 "2011-02-02,5,3\n" +
22 "2011-03-03,6,1\n" +
23 "2011-04-04,9,5\n" +
24 "2011-05-05,8,3\n";
25
8c5d3797
RK
26/*
27 * Tweaks the dygraph so it sets g._testDrawCalled to true when internal method
28 * drawGraph_ is called. Call unWrapDrawGraph when done with this.
29 */
319d0361 30var wrapDrawGraph = function(g) {
9ca829f2 31 g._testDrawCalled = false;
8c5d3797
RK
32 g._oldDrawGraph = g.drawGraph_;
33 g.drawGraph_ = function() {
9ca829f2 34 g._testDrawCalled = true;
8c5d3797 35 g._oldDrawGraph.call(g);
9ca829f2 36 }
319d0361 37};
9ca829f2 38
8c5d3797
RK
39/*
40 * See wrapDrawGraph
41 */
319d0361 42var unwrapDrawGraph = function(g) {
8c5d3797 43 g.drawGraph_ = g._oldDrawGraph;
9ca829f2
DV
44}
45
89fdcedb 46it('testStrokeAll', function() {
9ca829f2 47 var graphDiv = document.getElementById("graph");
319d0361 48 var graph = new Dygraph(graphDiv, data, opts);
9ca829f2
DV
49 var updatedOptions = { };
50
51 updatedOptions['strokeWidth'] = 3;
52
53 // These options will allow us to jump to renderGraph_()
54 // drawGraph_() will be skipped.
319d0361 55 wrapDrawGraph(graph);
9ca829f2 56 graph.updateOptions(updatedOptions);
319d0361 57 unwrapDrawGraph(graph);
89fdcedb
DV
58 assert.isFalse(graph._testDrawCalled);
59});
9ca829f2 60
89fdcedb 61it('testStrokeSingleSeries', function() {
9ca829f2 62 var graphDiv = document.getElementById("graph");
319d0361 63 var graph = new Dygraph(graphDiv, data, opts);
9ca829f2
DV
64 var updatedOptions = { };
65 var optionsForY1 = { };
66
67 optionsForY1['strokeWidth'] = 3;
8887663f 68 updatedOptions['series'] = {'Y1': optionsForY1};
9ca829f2
DV
69
70 // These options will allow us to jump to renderGraph_()
71 // drawGraph_() will be skipped.
319d0361 72 wrapDrawGraph(graph);
9ca829f2 73 graph.updateOptions(updatedOptions);
319d0361 74 unwrapDrawGraph(graph);
89fdcedb
DV
75 assert.isFalse(graph._testDrawCalled);
76});
9ca829f2 77
89fdcedb 78it('testSingleSeriesRequiresNewPoints', function() {
9ca829f2 79 var graphDiv = document.getElementById("graph");
319d0361 80 var graph = new Dygraph(graphDiv, data, opts);
8887663f
DV
81 var updatedOptions = {
82 series: {
83 Y1: {
84 strokeWidth: 2
85 },
86 Y2: {
87 stepPlot: true
88 }
89 }
90 };
9ca829f2
DV
91
92 // These options will not allow us to jump to renderGraph_()
93 // drawGraph_() must be called
319d0361 94 wrapDrawGraph(graph);
9ca829f2 95 graph.updateOptions(updatedOptions);
319d0361 96 unwrapDrawGraph(graph);
89fdcedb
DV
97 assert.isTrue(graph._testDrawCalled);
98});
9ca829f2 99
89fdcedb 100it('testWidthChangeNeedsNewPoints', function() {
9ca829f2 101 var graphDiv = document.getElementById("graph");
319d0361 102 var graph = new Dygraph(graphDiv, data, opts);
9ca829f2
DV
103 var updatedOptions = { };
104
105 // This will require new points.
106 updatedOptions['width'] = 600;
107
108 // These options will not allow us to jump to renderGraph_()
109 // drawGraph_() must be called
319d0361 110 wrapDrawGraph(graph);
9ca829f2 111 graph.updateOptions(updatedOptions);
319d0361 112 unwrapDrawGraph(graph);
89fdcedb
DV
113 assert.isTrue(graph._testDrawCalled);
114});
66ad3609
RK
115
116// Test https://github.com/danvk/dygraphs/issues/87
89fdcedb 117it('testUpdateLabelsDivDoesntInfiniteLoop', function() {
66ad3609
RK
118 var graphDiv = document.getElementById("graph");
119 var labelsDiv = document.getElementById("labels");
319d0361 120 var graph = new Dygraph(graphDiv, data, opts);
66ad3609 121 graph.updateOptions({labelsDiv : labelsDiv});
89fdcedb 122});
66ad3609 123
8c5d3797 124// Test https://github.com/danvk/dygraphs/issues/247
89fdcedb 125it('testUpdateColors', function() {
8c5d3797 126 var graphDiv = document.getElementById("graph");
319d0361 127 var graph = new Dygraph(graphDiv, data, opts);
8c5d3797
RK
128
129 var defaultColors = ["rgb(0,128,0)", "rgb(0,0,128)"];
89fdcedb 130 assert.deepEqual(["rgb(0,128,0)", "rgb(0,0,128)"], graph.getColors());
8c5d3797
RK
131
132 var colors1 = [ "#aaa", "#bbb" ];
133 graph.updateOptions({ colors: colors1 });
dc910fce 134 assert.deepEqual(colors1, graph.getColors());
8c5d3797 135
a8f4eb62 136 // extra colors are ignored until you add additional data series.
45efb726 137 var colors2 = [ "#ddd", "#eee", "#fff" ];
8c5d3797 138 graph.updateOptions({ colors: colors2 });
89fdcedb 139 assert.deepEqual(["#ddd", "#eee"], graph.getColors());
8c5d3797 140
a8f4eb62
DV
141 graph.updateOptions({ file:
142 "X,Y1,Y2,Y3\n" +
143 "2011-01-01,2,3,4\n" +
144 "2011-02-02,5,3,2\n"
145 });
dc910fce 146 assert.deepEqual(colors2, graph.getColors());
8c5d3797 147
319d0361 148 graph.updateOptions({ colors: null, file: data });
dc910fce 149 assert.deepEqual(defaultColors, graph.getColors());
89fdcedb 150});
4a0cb9c4
DV
151
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
154// legend.
89fdcedb 155it('testUpdateLegendAlways', function() {
4a0cb9c4 156 var graphDiv = document.getElementById("graph");
319d0361 157 var graph = new Dygraph(graphDiv, data, opts);
4a0cb9c4
DV
158
159 var legend = document.getElementsByClassName("dygraph-legend");
89fdcedb 160 assert.equal(1, legend.length);
4a0cb9c4 161 legend = legend[0];
89fdcedb 162 assert.equal("", legend.innerHTML);
4a0cb9c4
DV
163
164 graph.updateOptions({legend: 'always'});
165
166 legend = document.getElementsByClassName("dygraph-legend");
89fdcedb 167 assert.equal(1, legend.length);
4a0cb9c4 168 legend = legend[0];
89fdcedb
DV
169 assert.notEqual(-1, legend.textContent.indexOf("Y1"));
170 assert.notEqual(-1, legend.textContent.indexOf("Y2"));
171});
172
173});