2 * @fileoverview Tests for the plugins option.
4 * @author konigsberg@google.com (Robert Konigsberg)
7 import Dygraph from
'../../src/dygraph';
8 import DygraphOps from
'./DygraphOps';
10 describe("plugins", function() {
16 beforeEach(function() {
25 it('testWillDrawChart', function() {
28 var plugin
= (function() {
32 p
.prototype.activate
= function(g
) {
34 willDrawChart
: this.willDrawChart
38 p
.prototype.willDrawChart
= function(e
) {
45 var graph
= document
.getElementById("graph");
46 var g
= new Dygraph(graph
, data
, {plugins
: [plugin
]});
48 assert
.equal(1, draw
);
51 it('testPassingInstance', function() {
52 // You can also pass an instance of a plugin instead of a Plugin class.
55 activate
: function(g
) {
57 willDrawChart
: this.willDrawChart
60 willDrawChart
: function(g
) {
65 var graph
= document
.getElementById("graph");
66 var g
= new Dygraph(graph
, data
, {plugins
: [p
]});
68 assert
.equal(1, draw
);
71 it('testPreventDefault', function() {
81 pointClickPreventDefault
: false,
82 clickPreventDefault
: false,
83 activate
: function(g
) {
85 pointClick
: this.pointClick
,
89 pointClick
: function(e
) {
90 events
.push(['plugin.pointClick', e
.point
.xval
, e
.point
.yval
]);
91 if (this.pointClickPreventDefault
) {
96 events
.push(['plugin.click', e
.xval
]);
97 if (this.clickPreventDefault
) {
103 var graph
= document
.getElementById("graph");
104 var g
= new Dygraph(graph
, data1
, {
106 clickCallback
: function(e
, x
) {
107 events
.push(['clickCallback', x
]);
109 pointClickCallback
: function(e
, pt
) {
110 events
.push(['pointClickCallback', pt
.xval
, pt
.yval
]);
114 // Click the point at x=20
115 function clickOnPoint() {
117 DygraphOps
.dispatchMouseDown_Point(g
, x
, y
);
118 DygraphOps
.dispatchMouseMove_Point(g
, x
, y
);
119 DygraphOps
.dispatchMouseUp_Point(g
, x
, y
);
122 p
.pointClickPreventDefault
= false;
123 p
.clickPreventDefault
= false;
126 ['plugin.pointClick', 20, -1],
127 ['pointClickCallback', 20, -1],
128 ['plugin.click', 20],
129 ['clickCallback', 20]
133 p
.pointClickPreventDefault
= true;
134 p
.clickPreventDefault
= false;
137 ['plugin.pointClick', 20, -1]
141 p
.pointClickPreventDefault
= false;
142 p
.clickPreventDefault
= true;
145 ['plugin.pointClick', 20, -1],
146 ['pointClickCallback', 20, -1],
151 it('testEventSequence', function() {
154 var eventLogger
= function(name
) {
161 activate
: function(g
) {
163 clearChart
: eventLogger('clearChart'),
164 predraw
: eventLogger('predraw'),
165 willDrawChart
: eventLogger('willDrawChart'),
166 didDrawChart
: eventLogger('didDrawChart'),
167 dataWillUpdate
: eventLogger('dataWillUpdate'),
168 dataDidUpdate
: eventLogger('dataDidUpdate')
173 var graph
= document
.getElementById("graph");
174 var g
= new Dygraph(graph
, data
, {plugins
: [p
]});
176 // Initial draw sequence
178 "dataDidUpdate", // should dataWillUpdate be called here, too?
185 // An options change triggers a redraw, but doesn't change the data.
187 g
.updateOptions({series
: {Y1
: {color
: 'blue'}}});
195 // A pan shouldn't cause a new "predraw"
197 DygraphOps
.dispatchMouseDown_Point(g
, 100, 100, {shiftKey
: true});
198 DygraphOps
.dispatchMouseMove_Point(g
, 200, 100, {shiftKey
: true});
199 DygraphOps
.dispatchMouseUp_Point(g
, 200, 100, {shiftKey
: true});
206 // New data triggers the full sequence.
208 g
.updateOptions({file
: data
+ '\n4,1,2'});
219 it('testDestroyCalledInOrder', function() {
220 var destructions
= [];
221 var makePlugin
= function(name
) {
223 activate
: function(g
) { return {} },
224 destroy
: function() {
225 destructions
.push(name
);
230 var graph
= document
.getElementById("graph");
231 var g
= new Dygraph(graph
, data
, {
232 plugins
: [makePlugin('p'), makePlugin('q')]
235 assert
.deepEqual([], destructions
);
237 assert
.deepEqual(['q', 'p'], destructions
);