Add jsdoc for setAnnotations() and an example of how to use them with native-format...
[dygraphs.git] / auto_tests / tests / update_options.js
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 */
7 var UpdateOptionsTestCase = TestCase("update-options");
8
9 UpdateOptionsTestCase.prototype.opts = {
10 width: 480,
11 height: 320,
12 };
13
14 UpdateOptionsTestCase.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
21 UpdateOptionsTestCase.prototype.setUp = function() {
22 document.body.innerHTML = "<div id='graph'></div><div id='labels'></div>";
23 };
24
25 UpdateOptionsTestCase.prototype.tearDown = function() {
26 };
27
28 UpdateOptionsTestCase.prototype.wrap = function(g) {
29 g._testDrawCalled = false;
30 var oldDrawGraph = Dygraph.prototype.drawGraph_;
31 Dygraph.prototype.drawGraph_ = function() {
32 g._testDrawCalled = true;
33 oldDrawGraph.call(g);
34 }
35
36 return oldDrawGraph;
37 }
38
39 UpdateOptionsTestCase.prototype.unWrap = function(oldDrawGraph) {
40 Dygraph.prototype.drawGraph_ = oldDrawGraph;
41 }
42
43 UpdateOptionsTestCase.prototype.testStrokeAll = function() {
44 var graphDiv = document.getElementById("graph");
45 var graph = new Dygraph(graphDiv, this.data, this.opts);
46 var updatedOptions = { };
47
48 updatedOptions['strokeWidth'] = 3;
49
50 // These options will allow us to jump to renderGraph_()
51 // drawGraph_() will be skipped.
52 var oldDrawGraph = this.wrap(graph);
53 graph.updateOptions(updatedOptions);
54 this.unWrap(oldDrawGraph);
55 assertFalse(graph._testDrawCalled);
56 };
57
58 UpdateOptionsTestCase.prototype.testStrokeSingleSeries = function() {
59 var graphDiv = document.getElementById("graph");
60 var graph = new Dygraph(graphDiv, this.data, this.opts);
61 var updatedOptions = { };
62 var optionsForY1 = { };
63
64 optionsForY1['strokeWidth'] = 3;
65 updatedOptions['Y1'] = optionsForY1;
66
67 // These options will allow us to jump to renderGraph_()
68 // drawGraph_() will be skipped.
69 var oldDrawGraph = this.wrap(graph);
70 graph.updateOptions(updatedOptions);
71 this.unWrap(oldDrawGraph);
72 assertFalse(graph._testDrawCalled);
73 };
74
75 UpdateOptionsTestCase.prototype.testSingleSeriesRequiresNewPoints = function() {
76 var graphDiv = document.getElementById("graph");
77 var graph = new Dygraph(graphDiv, this.data, this.opts);
78 var updatedOptions = { };
79 var optionsForY1 = { };
80 var optionsForY2 = { };
81
82 // This will not require new points.
83 optionsForY1['strokeWidth'] = 2;
84 updatedOptions['Y1'] = optionsForY1;
85
86 // This will require new points.
87 optionsForY2['stepPlot'] = true;
88 updatedOptions['Y2'] = optionsForY2;
89
90 // These options will not allow us to jump to renderGraph_()
91 // drawGraph_() must be called
92 var oldDrawGraph = this.wrap(graph);
93 graph.updateOptions(updatedOptions);
94 this.unWrap(oldDrawGraph);
95 assertTrue(graph._testDrawCalled);
96 };
97
98 UpdateOptionsTestCase.prototype.testWidthChangeNeedsNewPoints = function() {
99 var graphDiv = document.getElementById("graph");
100 var graph = new Dygraph(graphDiv, this.data, this.opts);
101 var updatedOptions = { };
102
103 // This will require new points.
104 updatedOptions['width'] = 600;
105
106 // These options will not allow us to jump to renderGraph_()
107 // drawGraph_() must be called
108 var oldDrawGraph = this.wrap(graph);
109 graph.updateOptions(updatedOptions);
110 this.unWrap(oldDrawGraph);
111 assertTrue(graph._testDrawCalled);
112 };
113
114 // Test https://github.com/danvk/dygraphs/issues/87
115 UpdateOptionsTestCase.prototype.testUpdateLabelsDivDoesntInfiniteLoop = function() {
116 var graphDiv = document.getElementById("graph");
117 var labelsDiv = document.getElementById("labels");
118 var graph = new Dygraph(graphDiv, this.data, this.opts);
119 graph.updateOptions({labelsDiv : labelsDiv});
120 }
121