2 * @fileoverview Tests for the plugins option.
4 * @author konigsberg@google.com (Robert Konigsberg)
6 describe("plugins", function() {
10 beforeEach(function() {
11 document
.body
.innerHTML
= "<div id='graph'></div>";
21 afterEach(function() {
24 it('testWillDrawChart', function() {
27 var plugin
= (function() {
31 p
.prototype.activate
= function(g
) {
33 willDrawChart
: this.willDrawChart
37 p
.prototype.willDrawChart
= function(e
) {
44 var graph
= document
.getElementById("graph");
45 var g
= new Dygraph(graph
, data
, {plugins
: [plugin
]});
47 assert
.equal(1, draw
);
50 it('testPassingInstance', function() {
51 // You can also pass an instance of a plugin instead of a Plugin class.
54 activate
: function(g
) {
56 willDrawChart
: this.willDrawChart
59 willDrawChart
: function(g
) {
64 var graph
= document
.getElementById("graph");
65 var g
= new Dygraph(graph
, data
, {plugins
: [p
]});
67 assert
.equal(1, draw
);
70 it('testPreventDefault', function() {
80 pointClickPreventDefault
: false,
81 clickPreventDefault
: false,
82 activate
: function(g
) {
84 pointClick
: this.pointClick
,
88 pointClick
: function(e
) {
89 events
.push(['plugin.pointClick', e
.point
.xval
, e
.point
.yval
]);
90 if (this.pointClickPreventDefault
) {
95 events
.push(['plugin.click', e
.xval
]);
96 if (this.clickPreventDefault
) {
102 var graph
= document
.getElementById("graph");
103 var g
= new Dygraph(graph
, data1
, {
105 clickCallback
: function(e
, x
) {
106 events
.push(['clickCallback', x
]);
108 pointClickCallback
: function(e
, pt
) {
109 events
.push(['pointClickCallback', pt
.xval
, pt
.yval
]);
113 // Click the point at x=20
114 function clickOnPoint() {
116 DygraphOps
.dispatchMouseDown_Point(g
, x
, y
);
117 DygraphOps
.dispatchMouseMove_Point(g
, x
, y
);
118 DygraphOps
.dispatchMouseUp_Point(g
, x
, y
);
121 p
.pointClickPreventDefault
= false;
122 p
.clickPreventDefault
= false;
125 ['plugin.pointClick', 20, -1],
126 ['pointClickCallback', 20, -1],
127 ['plugin.click', 20],
128 ['clickCallback', 20]
132 p
.pointClickPreventDefault
= true;
133 p
.clickPreventDefault
= false;
136 ['plugin.pointClick', 20, -1]
140 p
.pointClickPreventDefault
= false;
141 p
.clickPreventDefault
= true;
144 ['plugin.pointClick', 20, -1],
145 ['pointClickCallback', 20, -1],
150 it('testEventSequence', function() {
153 var eventLogger
= function(name
) {
160 activate
: function(g
) {
162 clearChart
: eventLogger('clearChart'),
163 predraw
: eventLogger('predraw'),
164 willDrawChart
: eventLogger('willDrawChart'),
165 didDrawChart
: eventLogger('didDrawChart'),
166 dataWillUpdate
: eventLogger('dataWillUpdate'),
167 dataDidUpdate
: eventLogger('dataDidUpdate')
172 var graph
= document
.getElementById("graph");
173 var g
= new Dygraph(graph
, data
, {plugins
: [p
]});
175 // Initial draw sequence
177 "dataDidUpdate", // should dataWillUpdate be called here, too?
184 // An options change triggers a redraw, but doesn't change the data.
186 g
.updateOptions({series
: {Y1
: {color
: 'blue'}}});
194 // A pan shouldn't cause a new "predraw"
196 DygraphOps
.dispatchMouseDown_Point(g
, 100, 100, {shiftKey
: true});
197 DygraphOps
.dispatchMouseMove_Point(g
, 200, 100, {shiftKey
: true});
198 DygraphOps
.dispatchMouseUp_Point(g
, 200, 100, {shiftKey
: true});
205 // New data triggers the full sequence.
207 g
.updateOptions({file
: data
+ '\n4,1,2'});
218 it('testDestroyCalledInOrder', function() {
219 var destructions
= [];
220 var makePlugin
= function(name
) {
222 activate
: function(g
) { return {} },
223 destroy
: function() {
224 destructions
.push(name
);
229 var graph
= document
.getElementById("graph");
230 var g
= new Dygraph(graph
, data
, {
231 plugins
: [makePlugin('p'), makePlugin('q')]
234 assert
.deepEqual([], destructions
);
236 assert
.deepEqual(['q', 'p'], destructions
);