From: Dan Vanderkam Date: Sun, 16 Nov 2014 23:40:53 +0000 (-0500) Subject: more tests for plugins X-Git-Tag: v1.1.0~23^2~1 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=48238e1d74c1a973c74f80f5f87bd9205fbf252e;p=dygraphs.git more tests for plugins --- diff --git a/auto_tests/tests/plugins.js b/auto_tests/tests/plugins.js index 8dde4d8..cff1d92 100644 --- a/auto_tests/tests/plugins.js +++ b/auto_tests/tests/plugins.js @@ -7,6 +7,13 @@ var pluginsTestCase = TestCase("plugins"); pluginsTestCase.prototype.setUp = function() { document.body.innerHTML = "
"; + + this.data = "X,Y1,Y2\n" + + "0,1,2\n" + + "1,2,1\n" + + "2,1,2\n" + + "3,2,1\n" + ; }; pluginsTestCase.prototype.tearDown = function() { @@ -32,15 +39,178 @@ pluginsTestCase.prototype.testWillDrawChart = function() { return p; })(); - var data = "X,Y1,Y2\n" + - "0,1,1\n" + - "1,1,1\n" + - "2,1,1\n" + - "3,1,1\n" - ; + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, this.data, {plugins: [plugin]}); + + assertEquals(1, draw); +}; + +pluginsTestCase.prototype.testPassingInstance = function() { + // You can also pass an instance of a plugin instead of a Plugin class. + var draw = 0; + var p = { + activate: function(g) { + return { + willDrawChart: this.willDrawChart + } + }, + willDrawChart: function(g) { + draw++; + } + }; var graph = document.getElementById("graph"); - var g = new Dygraph(graph, data, { plugins : [ plugin ] }); + var g = new Dygraph(graph, this.data, {plugins: [p]}); assertEquals(1, draw); }; + +pluginsTestCase.prototype.testPreventDefault = function() { + var data1 = "X,Y\n" + + "20,-1\n" + + "21,0\n" + + "22,1\n" + + "23,0\n"; + + var events = []; + + var p = { + pointClickPreventDefault: false, + clickPreventDefault: false, + activate: function(g) { + return { + pointClick: this.pointClick, + click: this.click + } + }, + pointClick: function(e) { + events.push(['plugin.pointClick', e.point.xval, e.point.yval]); + if (this.pointClickPreventDefault) { + e.preventDefault(); + } + }, + click: function(e) { + events.push(['plugin.click', e.xval]); + if (this.clickPreventDefault) { + e.preventDefault(); + } + } + }; + + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data1, { + plugins: [p], + clickCallback: function(e, x) { + events.push(['clickCallback', x]); + }, + pointClickCallback: function(e, pt) { + events.push(['pointClickCallback', pt.xval, pt.yval]); + } + }); + + // Click the point at x=20 + function clickOnPoint() { + var x = 58, y = 275; + DygraphOps.dispatchMouseDown_Point(g, x, y); + DygraphOps.dispatchMouseMove_Point(g, x, y); + DygraphOps.dispatchMouseUp_Point(g, x, y); + } + + p.pointClickPreventDefault = false; + p.clickPreventDefault = false; + clickOnPoint(); + assertEquals([ + ['plugin.pointClick', 20, -1], + ['pointClickCallback', 20, -1], + ['plugin.click', 20], + ['clickCallback', 20] + ], events); + + events = []; + p.pointClickPreventDefault = true; + p.clickPreventDefault = false; + clickOnPoint(); + assertEquals([ + ['plugin.pointClick', 20, -1] + ], events); + + events = []; + p.pointClickPreventDefault = false; + p.clickPreventDefault = true; + clickOnPoint(); + assertEquals([ + ['plugin.pointClick', 20, -1], + ['pointClickCallback', 20, -1], + ['plugin.click', 20] + ], events); +}; + +pluginsTestCase.prototype.testEventSequence = function() { + var events = []; + + var eventLogger = function(name) { + return function(e) { + events.push(name); + }; + }; + + var p = { + activate: function(g) { + return { + clearChart: eventLogger('clearChart'), + predraw: eventLogger('predraw'), + willDrawChart: eventLogger('willDrawChart'), + didDrawChart: eventLogger('didDrawChart'), + dataWillUpdate: eventLogger('dataWillUpdate'), + dataDidUpdate: eventLogger('dataDidUpdate') + } + } + }; + + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, this.data, {plugins: [p]}); + + // Initial draw sequence + assertEquals([ + "dataDidUpdate", // should dataWillUpdate be called here, too? + "predraw", + "clearChart", + "willDrawChart", + "didDrawChart" + ], events); + + // An options change triggers a redraw, but doesn't change the data. + events = []; + g.updateOptions({series: {Y1: {color: 'blue'}}}); + assertEquals([ + "clearChart", + "predraw", + "clearChart", // why is clearChart called twice? + "willDrawChart", + "didDrawChart" + ], events); + + // A pan shouldn't cause a new "predraw" + events = []; + DygraphOps.dispatchMouseDown_Point(g, 100, 100, {shiftKey: true}); + DygraphOps.dispatchMouseMove_Point(g, 200, 100, {shiftKey: true}); + DygraphOps.dispatchMouseUp_Point(g, 200, 100, {shiftKey: true}); + assertEquals([ + "clearChart", + "willDrawChart", + "didDrawChart" + ], events); + + // New data triggers the full sequence. + events = []; + g.updateOptions({file: this.data + '\n4,1,2'}); + assertEquals([ + "dataWillUpdate", + "dataDidUpdate", + "clearChart", + "predraw", + "clearChart", // why is clearChart called twice? + "willDrawChart", + "didDrawChart" + ], events); +}; diff --git a/dygraph-interaction-model.js b/dygraph-interaction-model.js index 6f27968..c6f370d 100644 --- a/dygraph-interaction-model.js +++ b/dygraph-interaction-model.js @@ -332,6 +332,7 @@ Dygraph.Interaction.treatMouseOpAsClick = function(g, event, context) { } var e = { + cancelable: true, xval: g.lastx_, // closest point by x value pts: g.selPoints_, canvasx: context.dragEndX,