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