From: Dan Vanderkam Date: Wed, 21 Oct 2015 02:27:30 +0000 (-0400) Subject: Rewrite tests to use ES6 modules. X-Git-Tag: v2.0.0~38^2~15 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=e8c70e4e0f4c124a2c68eb43d6ec4e781d1bf810;p=dygraphs.git Rewrite tests to use ES6 modules. --- diff --git a/auto_tests/runner.html b/auto_tests/runner.html new file mode 100644 index 0000000..1768c1f --- /dev/null +++ b/auto_tests/runner.html @@ -0,0 +1,60 @@ + + + + dygraphs tests + + + +
+
+ + + + + + + + + + + + + + + + + + + diff --git a/auto_tests/tests/CanvasAssertions.js b/auto_tests/tests/CanvasAssertions.js index 2b37ec5..ef37fb9 100644 --- a/auto_tests/tests/CanvasAssertions.js +++ b/auto_tests/tests/CanvasAssertions.js @@ -244,3 +244,5 @@ CanvasAssertions.match = function(predicate, call) { } return true; }; + +export default CanvasAssertions; diff --git a/auto_tests/tests/DygraphOps.js b/auto_tests/tests/DygraphOps.js index 52f5943..460547b 100644 --- a/auto_tests/tests/DygraphOps.js +++ b/auto_tests/tests/DygraphOps.js @@ -18,6 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. +import * as utils from '../../src/dygraph-utils'; /** * @fileoverview Utility functions for Dygraphs. @@ -109,7 +110,7 @@ DygraphOps.dispatchDoubleClick = function(g, custom) { * type, screenX, screenY, clientX, clientY. */ DygraphOps.createOptsForPoint_ = function(g, type, x, y) { - var pos = Dygraph.findPos(g.canvas_); + var pos = utils.findPos(g.canvas_); var pageX = pos.x + x; var pageY = pos.y + y; @@ -213,3 +214,5 @@ DygraphOps.dispatchMouseOut = function(g, x, y, custom) { custom); }; + +export default DygraphOps; diff --git a/auto_tests/tests/PixelSampler.js b/auto_tests/tests/PixelSampler.js index e1b3be3..d45ab8a 100644 --- a/auto_tests/tests/PixelSampler.js +++ b/auto_tests/tests/PixelSampler.js @@ -33,6 +33,17 @@ PixelSampler.prototype.colorAtPixel = function(x, y) { }; /** + * Convenience wrapper around colorAtPixel if you only care about RGB (not A). + * @param {number} x The screen x-coordinate at which to sample. + * @param {number} y The screen y-coordinate at which to sample. + * @return {Array.} a 3D array: [R, G, B]. All three values + * are in [0, 255]. A pixel which has never been touched will be [0,0,0]. + */ +PixelSampler.prototype.rgbAtPixel = function(x, y) { + return this.colorAtPixel(x, y).slice(0, 3); +}; + +/** * The method samples a color using data coordinates (not screen coordinates). * This will round your data coordinates to the nearest screen pixel before * sampling. @@ -45,3 +56,5 @@ PixelSampler.prototype.colorAtCoordinate = function(x, y) { var dom_xy = this.dygraph_.toDomCoords(x, y); return this.colorAtPixel(Math.round(dom_xy[0]), Math.round(dom_xy[1])); }; + +export default PixelSampler; diff --git a/auto_tests/tests/Proxy.js b/auto_tests/tests/Proxy.js index 9ffc385..2348b87 100644 --- a/auto_tests/tests/Proxy.js +++ b/auto_tests/tests/Proxy.js @@ -75,3 +75,5 @@ Proxy.prototype.log__ = function(name, args) { Proxy.reset = function(proxy) { proxy.calls__ = []; } + +export default Proxy; diff --git a/auto_tests/tests/Util.js b/auto_tests/tests/Util.js index 3b8c1b8..36532cd 100644 --- a/auto_tests/tests/Util.js +++ b/auto_tests/tests/Util.js @@ -3,6 +3,9 @@ * * @author konigsberg@google.com (Robert Konigsberg) */ + +import * as utils from '../../src/dygraph-utils'; + var Util = {}; /** @@ -90,14 +93,14 @@ Util.makeNumbers = function(ary) { /** * Sample a pixel from the canvas. * Returns an [r, g, b, a] tuple where each values is in [0, 255]. + * This is _very_ slow! If you want to sample many pixels, use PixelSampler. */ Util.samplePixel = function(canvas, x, y) { var ctx = canvas.getContext("2d"); // bypasses Proxy if applied. - // TODO(danvk): Any performance issues with this? var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); - var scale = Dygraph.getContextPixelRatio(ctx); + var scale = utils.getContextPixelRatio(ctx); var i = 4 * (x * scale + imageData.width * y * scale); var d = imageData.data; @@ -145,7 +148,7 @@ Util.overrideXMLHttpRequest = function(data) { * @return {string} The date formatted as YYYY-MM-DD. */ Util.formatDate = function(dateMillis) { - return Dygraph.dateString_(dateMillis).slice(0, 10); // 10 == "YYYY/MM/DD".length + return utils.dateString_(dateMillis).slice(0, 10); // 10 == "YYYY/MM/DD".length }; /** @@ -168,3 +171,5 @@ Util.captureConsole = function(obj) { console.error = orig[2]; }; }; + +export default Util; diff --git a/auto_tests/tests/annotations.js b/auto_tests/tests/annotations.js index 3ea5e7c..3be1936 100644 --- a/auto_tests/tests/annotations.js +++ b/auto_tests/tests/annotations.js @@ -3,14 +3,13 @@ * * @author danvk@google.com (Dan Vanderkam) */ -describe("annotations", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; +import Util from './Util'; -afterEach(function() { -}); +describe("annotations", function() { + +cleanupAfterEach(); it('testAnnotationsDrawn', function() { var opts = { diff --git a/auto_tests/tests/axis_labels-deprecated.js b/auto_tests/tests/axis_labels-deprecated.js deleted file mode 100644 index 6ca1dc1..0000000 --- a/auto_tests/tests/axis_labels-deprecated.js +++ /dev/null @@ -1,318 +0,0 @@ -/** - * @fileoverview Test cases for how axis labels are chosen and formatted, - * specializing on the deprecated xLabelFormatter, etc. - * - * @author dan@dygraphs.com (Dan Vanderkam) - */ -describe("axis-labels-deprecated", function() { - -beforeEach(function() { - document.body.innerHTML = "
"; -}); - -afterEach(function() { -}); - -it('testDeprecatedDeprecatedXAxisTimeLabelFormatter', function() { - var opts = { - width: 480, - height: 320, - labels: ['X', 'Y1'] - }; - var data = [[5.0,0],[5.1,1],[5.2,2],[5.3,3],[5.4,4],[5.5,5],[5.6,6],[5.7,7],[5.8,8],[5.9,9]]; - var graph = document.getElementById("graph"); - var g = new Dygraph(graph, data, opts); - g.updateOptions({ - axes : { - x : { - axisLabelFormatter: function (totalMinutes) { - var hours = Math.floor( totalMinutes / 60); - var minutes = Math.floor((totalMinutes - (hours * 60))); - var seconds = Math.round((totalMinutes * 60) - (hours * 3600) - (minutes * 60)); - - if (hours < 10) hours = "0" + hours; - if (minutes < 10) minutes = "0" + minutes; - if (seconds < 10) seconds = "0" + seconds; - - return hours + ':' + minutes + ':' + seconds; - } - } - } - }); - - assert.deepEqual(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], Util.getXLabels()); - - // The legend does not use the xAxisLabelFormatter: - g.setSelection(1); - assert.equal('5.1: Y1: 1', Util.getLegend()); -}); - -it('testDeprecatedAxisLabelFormatter', function() { - var opts = { - width: 480, - height: 320, - axes : { - x : { - axisLabelFormatter: function(x, granularity, opts, dg) { - assert.equal('number', typeof(x)); - assert.equal('number', typeof(granularity)); - assert.equal('function', typeof(opts)); - assert.equal('[Dygraph graph]', dg.toString()); - return 'x' + x; - } - }, - y : { - axisLabelFormatter: function(y, granularity, opts, dg) { - assert.equal('number', typeof(y)); - assert.equal('number', typeof(granularity)); - assert.equal('function', typeof(opts)); - assert.equal('[Dygraph graph]', dg.toString()); - return 'y' + y; - } - } - }, - labels: ['x', 'y'] - }; - var data = []; - for (var i = 0; i < 10; i++) { - data.push([i, 2 * i]); - } - var graph = document.getElementById("graph"); - var g = new Dygraph(graph, data, opts); - - assert.deepEqual(['x0','x2','x4','x6','x8'], Util.getXLabels()); - assert.deepEqual(["y0","y5","y10","y15"], Util.getYLabels()); - - g.setSelection(2); - assert.equal("2: y: 4", Util.getLegend()); -}); - -it('testDeprecatedDateAxisLabelFormatter', function() { - var opts = { - width: 480, - height: 320, - axes : { - x : { - axisLabelFormatter: function(x, granularity, opts, dg) { - assert.isTrue(Dygraph.isDateLike(x)); - assert.equal('number', typeof(granularity)); - assert.equal('function', typeof(opts)); - assert.equal('[Dygraph graph]', dg.toString()); - return 'x' + Util.formatDate(x); - }, - pixelsPerLabel: 60 - }, - y : { - axisLabelFormatter: function(y, granularity, opts, dg) { - assert.equal('number', typeof(y)); - assert.equal('number', typeof(granularity)); - assert.equal('function', typeof(opts)); - assert.equal('[Dygraph graph]', dg.toString()); - return 'y' + y; - } - } - }, - labels: ['x', 'y'] - }; - var data = []; - for (var i = 1; i < 10; i++) { - data.push([new Date("2011/01/0" + i), 2 * i]); - } - var graph = document.getElementById("graph"); - var g = new Dygraph(graph, data, opts); - - assert.deepEqual(["x2011/01/02","x2011/01/04","x2011/01/06","x2011/01/08"], Util.getXLabels()); - assert.deepEqual(["y5","y10","y15"], Util.getYLabels()); - - g.setSelection(0); - assert.equal("2011/01/01: y: 2", Util.getLegend()); -}); - -// This test verifies that when a valueFormatter is set (but not an -// axisLabelFormatter), then the valueFormatter is used to format the axis -// labels. -it('testDeprecatedValueFormatter', function() { - var opts = { - width: 480, - height: 320, - axes : { - x : { - valueFormatter: function(x, opts, series_name, dg) { - assert.equal('number', typeof(x)); - assert.equal('function', typeof(opts)); - assert.equal('string', typeof(series_name)); - assert.equal('[Dygraph graph]', dg.toString()); - return 'x' + x; - } - }, - y : { - valueFormatter: function(y, opts, series_name, dg) { - assert.equal('number', typeof(y)); - assert.equal('function', typeof(opts)); - assert.equal('string', typeof(series_name)); - assert.equal('[Dygraph graph]', dg.toString()); - return 'y' + y; - } - } - }, - labels: ['x', 'y'] - }; - var data = []; - for (var i = 0; i < 10; i++) { - data.push([i, 2 * i]); - } - var graph = document.getElementById("graph"); - var g = new Dygraph(graph, data, opts); - - // the valueFormatter options do not affect the ticks. - assert.deepEqual(['0','2','4','6','8'], Util.getXLabels()); - assert.deepEqual(["0","5","10","15"], Util.getYLabels()); - - // they do affect the legend, however. - g.setSelection(2); - assert.equal("x2: y: y4", Util.getLegend()); -}); - -it('testDeprecatedDateValueFormatter', function() { - var opts = { - width: 480, - height: 320, - axes : { - x : { - valueFormatter: function(x, opts, series_name, dg) { - assert.equal('number', typeof(x)); - assert.equal('function', typeof(opts)); - assert.equal('string', typeof(series_name)); - assert.equal('[Dygraph graph]', dg.toString()); - return 'x' + Util.formatDate(x); - }, - pixelsPerLabel: 60 - }, - y : { - valueFormatter: function(y, opts, series_name, dg) { - assert.equal('number', typeof(y)); - assert.equal('function', typeof(opts)); - assert.equal('string', typeof(series_name)); - assert.equal('[Dygraph graph]', dg.toString()); - return 'y' + y; - } - } - }, - labels: ['x', 'y'] - }; - - var data = []; - for (var i = 1; i < 10; i++) { - data.push([new Date("2011/01/0" + i), 2 * i]); - } - var graph = document.getElementById("graph"); - var g = new Dygraph(graph, data, opts); - - // valueFormatters do not affect ticks. - assert.deepEqual(["02 Jan","04 Jan","06 Jan","08 Jan"], Util.getXLabels()); - assert.deepEqual(["5","10","15"], Util.getYLabels()); - - // the valueFormatter options also affect the legend. - g.setSelection(2); - assert.equal('x2011/01/03: y: y6', Util.getLegend()); -}); - -// This test verifies that when both a valueFormatter and an axisLabelFormatter -// are specified, the axisLabelFormatter takes precedence. -it('testDeprecatedAxisLabelFormatterPrecedence', function() { - var opts = { - width: 480, - height: 320, - axes : { - x : { - valueFormatter: function(x) { - return 'xvf' + x; - }, - axisLabelFormatter: function(x, granularity) { - return 'x' + x; - }, - }, - y : { - valueFormatter: function(y) { - return 'yvf' + y; - }, - axisLabelFormatter: function(y) { - return 'y' + y; - }, - } - }, - labels: ['x', 'y'] - }; - var data = []; - for (var i = 0; i < 10; i++) { - data.push([i, 2 * i]); - } - var graph = document.getElementById("graph"); - var g = new Dygraph(graph, data, opts); - - assert.deepEqual(['x0','x2','x4','x6','x8'], Util.getXLabels()); - assert.deepEqual(["y0","y5","y10","y15"], Util.getYLabels()); - - g.setSelection(9); - assert.equal("xvf9: y: yvf18", Util.getLegend()); -}); - -// This is the same as the previous test, except that options are added -// one-by-one. -it('testDeprecatedAxisLabelFormatterIncremental', function() { - var opts = { - width: 480, - height: 320, - labels: ['x', 'y'] - }; - var data = []; - for (var i = 0; i < 10; i++) { - data.push([i, 2 * i]); - } - var graph = document.getElementById("graph"); - var g = new Dygraph(graph, data, opts); - g.updateOptions({ - axes : { - x : { - valueFormatter: function(x) { - return 'xvf' + x; - } - } - } - }); - g.updateOptions({ - axes : { - y : { - valueFormatter: function(y) { - return 'yvf' + y; - } - } - } - }); - g.updateOptions({ - axes : { - x : { - axisLabelFormatter: function(x) { - return 'x' + x; - } - } - } - }); - g.updateOptions({ - axes : { - y : { - axisLabelFormatter: function(y) { - return 'y' + y; - } - } - } - }); - - assert.deepEqual(["x0","x2","x4","x6","x8"], Util.getXLabels()); - assert.deepEqual(["y0","y5","y10","y15"], Util.getYLabels()); - - g.setSelection(9); - assert.equal("xvf9: y: yvf18", Util.getLegend()); -}); - -}); diff --git a/auto_tests/tests/axis_labels.js b/auto_tests/tests/axis_labels.js index abff8b2..0af8085 100644 --- a/auto_tests/tests/axis_labels.js +++ b/auto_tests/tests/axis_labels.js @@ -3,14 +3,16 @@ * * @author dan@dygraphs.com (Dan Vanderkam) */ -describe("axis-labels", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; +import DEFAULT_ATTRS from '../../src/dygraph-default-attrs'; +import Util from './Util'; +import {assertDeepCloseTo} from './custom_asserts'; -afterEach(function() { -}); +describe("axis-labels", function() { + +cleanupAfterEach(); var simpleData = "X,Y,Y2\n" + @@ -204,7 +206,7 @@ it('testDateAxisLabelFormatter', function() { x : { pixelsPerLabel: 60, axisLabelFormatter : function(x, granularity, opts, dg) { - assert.isTrue(Dygraph.isDateLike(x)); + assert.isTrue(utils.isDateLike(x)); assert.equal('number', typeof(granularity)); assert.equal('function', typeof(opts)); assert.equal('[Dygraph graph]', dg.toString()); @@ -705,7 +707,7 @@ it('testAxisLabelFontSize', function() { var g = new Dygraph(graph, simpleData, {}); // Be sure we're dealing with a 14-point default. - assert.equal(14, Dygraph.DEFAULT_ATTRS.axisLabelFontSize); + assert.equal(14, DEFAULT_ATTRS.axisLabelFontSize); var assertFontSize = function(selector, expected) { Util.assertStyleOfChildren(selector, "font-size", expected); @@ -763,7 +765,7 @@ it('testAxisLabelFontSizeNull', function() { }; // Be sure we're dealing with a 14-point default. - assert.equal(14, Dygraph.DEFAULT_ATTRS.axisLabelFontSize); + assert.equal(14, DEFAULT_ATTRS.axisLabelFontSize); assertFontSize(document.querySelectorAll(".dygraph-axis-label-x"), "14px"); assertFontSize(document.querySelectorAll(".dygraph-axis-label-y"), "14px"); @@ -774,7 +776,7 @@ it('testAxisLabelColor', function() { var g = new Dygraph(graph, simpleData, {}); // Be sure we're dealing with a black default. - assert.equal("black", Dygraph.DEFAULT_ATTRS.axisLabelColor); + assert.equal("black", DEFAULT_ATTRS.axisLabelColor); var assertColor = function(selector, expected) { Util.assertStyleOfChildren(selector, "color", expected); @@ -832,7 +834,7 @@ it('testAxisLabelColorNull', function() { } // Be sure we're dealing with a 14-point default. - assert.equal(14, Dygraph.DEFAULT_ATTRS.axisLabelFontSize); + assert.equal(14, DEFAULT_ATTRS.axisLabelFontSize); assertColor(document.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 0)"); assertColor(document.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 0, 0)"); diff --git a/auto_tests/tests/callback.js b/auto_tests/tests/callback.js index b12ea64..1e00815 100644 --- a/auto_tests/tests/callback.js +++ b/auto_tests/tests/callback.js @@ -4,12 +4,22 @@ * @author uemit.seren@gmail.com (Ümit Seren) */ +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; +import Util from './Util'; +import DygraphOps from './DygraphOps'; + describe("callback", function() { +cleanupAfterEach(); + var xhr, styleSheet; +var graph; beforeEach(function() { - document.body.innerHTML = "
"; + var container = document.getElementById('graph'); + container.innerHTML = "
"; + graph = container.querySelector('#inner-graph'); xhr = XMLHttpRequest; styleSheet = document.createElement("style"); styleSheet.type = "text/css"; @@ -42,7 +52,6 @@ it('testHighlightCallbackIsCalled', function() { h_pts = pts; }; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, { width: 100, @@ -71,7 +80,6 @@ it('testDrawPointCallback_disabled', function() { called = true; }; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, { drawPointCallback: callback, }); @@ -91,7 +99,6 @@ it('testDrawPointCallback_enabled', function() { called = true; }; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, { drawPoints: true, drawPointCallback: callback @@ -114,7 +121,6 @@ it('testDrawPointCallback_pointSize', function() { count++; }; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, { drawPoints: true, drawPointCallback: callback @@ -144,10 +150,9 @@ it('testDrawPointCallback_isolated', function() { assert.equal(g, this); var dx = g.toDataXCoord(cx); xvalues.push(dx); - Dygraph.Circles.DEFAULT.apply(this, arguments); + utils.Circles.DEFAULT.apply(this, arguments); }; - var graph = document.getElementById("graph"); var testdata = [[10, 2], [11, 3], [12, NaN], [13, 2], [14, NaN], [15, 3]]; var graphOpts = { labels: ['X', 'Y'], @@ -188,7 +193,6 @@ it('testDrawHighlightPointCallbackIsCalled', function() { called = true; }; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, { width: 100, @@ -211,7 +215,6 @@ var runClosestTest = function(isStacked, widthNormal, widthHighlighted) { var h_pts; var h_series; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, { width: 600, @@ -359,7 +362,6 @@ it('testNaNData', function() { h_pts = pts; }; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, dataNaN, { width: 600, @@ -412,7 +414,6 @@ it('testNaNDataStack', function() { h_pts = pts; }; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, dataNaN, { width: 600, @@ -478,7 +479,6 @@ it('testGapHighlight', function() { h_pts = pts; }; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, dataGap, { width: 400, height: 300, @@ -521,7 +521,6 @@ it('testFailedResponse', function() { throw "should not reach here"; }; - var graph = document.getElementById("graph"); graph.style.border = "2px solid black"; var g = new Dygraph(graph, "data.csv", { // fake name width: 400, @@ -551,7 +550,6 @@ it('testHighlightCallbackRow', function() { highlightRow = row; }; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, "X,Y,Z\n" + "0,1,2\n" + // 0 @@ -602,7 +600,6 @@ it('testUnderlayCallback_noSeries', function() { yMax = g.yAxisRange(0)[1]; }; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, "\n", { underlayCallback: callback }); @@ -625,7 +622,6 @@ it('testUnderlayCallback_yAxisRange', function() { yMax = g.yAxisRange(0)[1]; }; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, "\n", { valueRange: [0,10], underlayCallback: callback @@ -645,10 +641,9 @@ it('testDrawPointCallback_idx', function() { var callback = function(g, seriesName, canvasContext, cx, cy, color, pointSizeParam,idx) { assert.equal(g, this); indices.push(idx); - Dygraph.Circles.DEFAULT.apply(this, arguments); + utils.Circles.DEFAULT.apply(this, arguments); }; - var graph = document.getElementById("graph"); var testdata = [[10, 2], [11, 3], [12, NaN], [13, 2], [14, NaN], [15, 3]]; var graphOpts = { @@ -695,7 +690,6 @@ it('testDrawHighlightPointCallback_idx', function() { idxToCheck = idx; }; var testdata = [[1, 2], [2, 3], [3, NaN], [4, 2], [5, NaN], [6, 3]]; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, testdata, { drawHighlightPointCallback: drawHighlightPointCallback, labels: ['X', 'Y'] diff --git a/auto_tests/tests/connect_separated_points.js b/auto_tests/tests/connect_separated_points.js index 2636b41..7b9fe4c 100644 --- a/auto_tests/tests/connect_separated_points.js +++ b/auto_tests/tests/connect_separated_points.js @@ -3,17 +3,20 @@ * * @author julian.eichstaedt@ch.sauter-bc.com (Fr. Sauter AG) */ + +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; +import CanvasAssertions from './CanvasAssertions'; +import Proxy from './Proxy'; + describe("connect-separated-points", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +cleanupAfterEach(); -var origFunc = Dygraph.getContext; +var origFunc = utils.getContext; beforeEach(function() { - document.body.innerHTML = "
"; - Dygraph.getContext = function(canvas) { + utils.getContext = function(canvas) { return new Proxy(origFunc(canvas)); }; }); diff --git a/auto_tests/tests/css.js b/auto_tests/tests/css.js index 89a0680..b1c96de 100644 --- a/auto_tests/tests/css.js +++ b/auto_tests/tests/css.js @@ -4,20 +4,25 @@ * @fileoverview Regression test based on some strange customBars data. * @author danvk@google.com (Dan Vanderkam) */ + +import Dygraph from '../../src/dygraph'; + describe("css", function() { +cleanupAfterEach(); + var data = "X,Y,Z\n1,2,3\n4,5,6\n"; var styleSheet; beforeEach(function() { - document.body.innerHTML = "
"; styleSheet = document.createElement("style"); styleSheet.type = "text/css"; document.getElementsByTagName("head")[0].appendChild(styleSheet); }); afterEach(function() { + styleSheet.innerHTML = ''; }); // Verifies that an unstyled, unsized dygraph gets a default size. @@ -78,12 +83,13 @@ it('testPixelStyleWins', function() { // Verifies that a CSS percentage size works. it('testPercentageSize', function() { - document.body.innerHTML = + var testdiv = document.getElementById("graph"); + testdiv.innerHTML = '
' + - '
'; + '
'; var opts = { }; - var graph = document.getElementById("graph"); + var graph = document.getElementById("inner-graph"); graph.style.width = '50%'; graph.style.height = '50%'; @@ -109,22 +115,20 @@ it('testClassPixelSize', function() { // An invisible chart div shouldn't produce an error. it('testInvisibleChart', function() { - document.body.innerHTML = + graph.innerHTML = '
' + - '
' + + '
' + '
'; - var graph = document.getElementById("graph"); - new Dygraph(graph, data, {}); + new Dygraph('inner-graph', data, {}); }); // An invisible chart div shouldn't produce an error. it('testInvisibleChartDate', function() { - document.body.innerHTML = + graph.innerHTML = '
' + - '
' + + '
' + '
'; - var graph = document.getElementById("graph"); - new Dygraph(graph, + new Dygraph('inner-graph', "Date,Y\n" + "2010/01/01,100\n" + "2010/02/01,200\n" + @@ -137,11 +141,12 @@ it('testInvisibleChartDate', function() { // An invisible chart div that becomes visible. it('testInvisibleThenVisibleChart', function() { - document.body.innerHTML = + var testdiv = document.getElementById("graph"); + testdiv.innerHTML = ''; - var graph = document.getElementById("graph"); + var graph = document.getElementById("inner-graph"); var g = new Dygraph(graph, "Date,Y\n" + "2010/01/01,100\n" + diff --git a/auto_tests/tests/custom_asserts.js b/auto_tests/tests/custom_asserts.js index 06e2444..9391f89 100644 --- a/auto_tests/tests/custom_asserts.js +++ b/auto_tests/tests/custom_asserts.js @@ -2,7 +2,7 @@ * @fileoverview Assertions that chai doesn't provide out of the box. */ -var assertDeepCloseTo = function(actualArray, expectedArray, epsilon) { +export function assertDeepCloseTo(actualArray, expectedArray, epsilon) { assert.isArray(actualArray); assert.isArray(expectedArray); for (var i = 0; i < actualArray.length; i++) { diff --git a/auto_tests/tests/custom_bars.js b/auto_tests/tests/custom_bars.js index cbffe7e..ebc867a 100644 --- a/auto_tests/tests/custom_bars.js +++ b/auto_tests/tests/custom_bars.js @@ -4,18 +4,25 @@ * @fileoverview Regression test based on some strange customBars data. * @author danvk@google.com (Dan Vanderkam) */ +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; +import CanvasAssertions from './CanvasAssertions'; +import PixelSampler from './PixelSampler'; +import Proxy from './Proxy'; + describe("custom-bars", function() { -var _origFunc = Dygraph.getContext; +cleanupAfterEach(); + +var _origFunc = utils.getContext; beforeEach(function() { - document.body.innerHTML = "
"; - Dygraph.getContext = function(canvas) { + utils.getContext = function(canvas) { return new Proxy(_origFunc(canvas)); } }); afterEach(function() { - Dygraph.getContext = _origFunc; + utils.getContext = _origFunc; }); // This test used to reliably produce an infinite loop. diff --git a/auto_tests/tests/data_api.js b/auto_tests/tests/data_api.js index 4b0ebe0..e692fc6 100644 --- a/auto_tests/tests/data_api.js +++ b/auto_tests/tests/data_api.js @@ -3,12 +3,14 @@ * * @author danvdk@gmail.com (Dan Vanderkam) */ +import Dygraph from '../../src/dygraph'; describe("data-api", function() { +cleanupAfterEach(); + var opts, graphDiv; beforeEach(function() { - document.body.innerHTML = "
"; opts = { width: 480, height: 320 @@ -17,9 +19,6 @@ beforeEach(function() { graphDiv = document.getElementById("graph"); }); -afterEach(function() { -}); - it('testBasicAccessors', function() { var g = new Dygraph(graphDiv, temperature_data, opts); diff --git a/auto_tests/tests/date_formats.js b/auto_tests/tests/date_formats.js index d15567b..f157b8d 100644 --- a/auto_tests/tests/date_formats.js +++ b/auto_tests/tests/date_formats.js @@ -3,13 +3,10 @@ * * @author dan@dygraphs.com (Dan Vanderkam) */ -describe("date-formats", function() { -beforeEach(function() { -}); +import * as utils from '../../src/dygraph-utils'; -afterEach(function() { -}); +describe("date-formats", function() { it('testISO8601', function() { // Format: YYYY-MM-DDTHH:MM:SS.ddddddZ @@ -19,7 +16,7 @@ it('testISO8601', function() { // Firefox <4 does not support this format: // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse if (navigator.userAgent.indexOf("Firefox/3.5") == -1) { - assert.equal(946816496789, Dygraph.dateParser("2000-01-02T12:34:56.789012Z")); + assert.equal(946816496789, utils.dateParser("2000-01-02T12:34:56.789012Z")); } }); @@ -36,7 +33,7 @@ it('testHyphenatedDate', function() { zp(d.getDate()) + ' ' + zp(d.getHours()) + ':' + zp(d.getMinutes()); - assert.equal(Date.UTC(2000, 1, 2), Dygraph.dateParser(str)); + assert.equal(Date.UTC(2000, 1, 2), utils.dateParser(str)); }); }); diff --git a/auto_tests/tests/date_ticker.js b/auto_tests/tests/date_ticker.js index 554105b..c02331d 100644 --- a/auto_tests/tests/date_ticker.js +++ b/auto_tests/tests/date_ticker.js @@ -6,22 +6,23 @@ * @author danvdk@gmail.com (Dan Vanderkam) */ +import * as DygraphTickers from '../../src/dygraph-tickers'; +import DEFAULT_ATTRS from '../../src/dygraph-default-attrs'; + describe("date-ticker-tests", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +cleanupAfterEach(); var createOptionsViewForAxis = function(axis, dict) { return function (x) { if (dict && dict.hasOwnProperty(x)) { return dict[x]; } - if (Dygraph.DEFAULT_ATTRS.axes[axis].hasOwnProperty(x)) { - return Dygraph.DEFAULT_ATTRS.axes[axis][x]; + if (DEFAULT_ATTRS.axes[axis].hasOwnProperty(x)) { + return DEFAULT_ATTRS.axes[axis][x]; } - if (Dygraph.DEFAULT_ATTRS.hasOwnProperty(x)) { - return Dygraph.DEFAULT_ATTRS[x]; + if (DEFAULT_ATTRS.hasOwnProperty(x)) { + return DEFAULT_ATTRS[x]; } if (x == 'axisLabelFormatter') return null; throw "mysterious " + axis + "-axis option: " + x; @@ -41,7 +42,7 @@ it('testBasicDateTicker', function() { var opts = {labelsUTC: true}; var options = createOptionsViewForAxis('x', opts); - var ticks = Dygraph.dateTicker(-1797534000000, 1255579200000, 800, options); + var ticks = DygraphTickers.dateTicker(-1797534000000, 1255579200000, 800, options); var expected_ticks = [ {"v":-1577923200000,"label":"1920"}, {"v":-1262304000000,"label":"1930"}, @@ -57,8 +58,8 @@ it('testBasicDateTicker', function() { var start = Date.UTC(1999, 11, 31, 14, 0, 0); var end = Date.UTC(2000, 0, 1, 12, 0, 0); - var granularity = Dygraph.TWO_HOURLY; - ticks = Dygraph.getDateAxis(start, end, granularity, options); + var granularity = DygraphTickers.Granularity.TWO_HOURLY; + ticks = DygraphTickers.getDateAxis(start, end, granularity, options); changeNbspToSpace(ticks); expected_ticks = [ // months of the year are zero-based. {v: Date.UTC(1999, 11, 31, 14, 0, 0), label: '14:00'}, @@ -86,7 +87,7 @@ it('testAllDateTickers', function() { // In these tests, those spurious ticks are removed to test new behavior. var ticker = function() { - var ticks = Dygraph.dateTicker.apply(null, arguments); + var ticks = DygraphTickers.dateTicker.apply(null, arguments); changeNbspToSpace(ticks); return ticks; }; diff --git a/auto_tests/tests/dygraph-options-tests.js b/auto_tests/tests/dygraph-options-tests.js index 4311fb5..aa9db33 100644 --- a/auto_tests/tests/dygraph-options-tests.js +++ b/auto_tests/tests/dygraph-options-tests.js @@ -1,18 +1,20 @@ /** * @fileoverview Test cases for DygraphOptions. */ + +import Dygraph from '../../src/dygraph'; +import DygraphOptions from '../../src/dygraph-options'; + describe("dygraph-options-tests", function() { +cleanupAfterEach(); + var graph; beforeEach(function() { - document.body.innerHTML = "
"; graph = document.getElementById("graph"); }); -afterEach(function() { -}); - /* * Pathalogical test to ensure getSeriesNames works */ diff --git a/auto_tests/tests/error_bars.js b/auto_tests/tests/error_bars.js index e04acde..4fd3369 100644 --- a/auto_tests/tests/error_bars.js +++ b/auto_tests/tests/error_bars.js @@ -3,23 +3,17 @@ * * @author danvk@google.com (Dan Vanderkam) */ -describe("error-bars", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; +import Util from './Util'; +import Proxy from './Proxy'; +import CanvasAssertions from './CanvasAssertions'; -var _origFunc = Dygraph.getContext; -beforeEach(function() { - document.body.innerHTML = "
"; - Dygraph.getContext = function(canvas) { - return new Proxy(_origFunc(canvas)); - } -}); +describe("error-bars", function() { -afterEach(function() { - Dygraph.getContext = _origFunc; -}); +cleanupAfterEach(); +useProxyCanvas(utils, Proxy); it('testErrorBarsDrawn', function() { var opts = { diff --git a/auto_tests/tests/fast_canvas_proxy.js b/auto_tests/tests/fast_canvas_proxy.js index 5d3322b..217dbd6 100644 --- a/auto_tests/tests/fast_canvas_proxy.js +++ b/auto_tests/tests/fast_canvas_proxy.js @@ -3,13 +3,11 @@ * * @author danvdk@gmail.com (Dan Vanderkam) */ -describe("fast-canvas-proxy", function() { -beforeEach(function() { -}); +import DygraphCanvasRenderer from '../../src/dygraph-canvas'; +import Proxy from './Proxy'; -afterEach(function() { -}); +describe("fast-canvas-proxy", function() { var fakeCanvasContext = { moveTo: function() {}, diff --git a/auto_tests/tests/fill_step_plot.js b/auto_tests/tests/fill_step_plot.js index 6406016..1c43145 100644 --- a/auto_tests/tests/fill_step_plot.js +++ b/auto_tests/tests/fill_step_plot.js @@ -4,23 +4,25 @@ * * @author benoitboivin.pro@gmail.com (Benoit Boivin) */ +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; +import CanvasAssertions from './CanvasAssertions'; +import Proxy from './Proxy'; + describe("fill-step-plot", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +cleanupAfterEach(); -var origFunc = Dygraph.getContext; +var origFunc = utils.getContext; beforeEach(function() { - document.body.innerHTML = "
"; - Dygraph.getContext = function(canvas) { + utils.getContext = function(canvas) { return new Proxy(origFunc(canvas)); }; }); afterEach(function() { - Dygraph.getContext = origFunc; + utils.getContext = origFunc; }); diff --git a/auto_tests/tests/formats.js b/auto_tests/tests/formats.js index a0b779b..f16cc6d 100644 --- a/auto_tests/tests/formats.js +++ b/auto_tests/tests/formats.js @@ -3,14 +3,12 @@ * * @author konigsberg@google.com (Robert Konigsberg) */ -describe("formats", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; -afterEach(function() { -}); +describe("formats", function() { + +cleanupAfterEach(); var dataString = "X,Y\n" + diff --git a/auto_tests/tests/grid_per_axis.js b/auto_tests/tests/grid_per_axis.js index 6ca239c..99cc6da 100644 --- a/auto_tests/tests/grid_per_axis.js +++ b/auto_tests/tests/grid_per_axis.js @@ -4,24 +4,19 @@ * * @author david.eberlein@ch.sauter-bc.com (Fr. Sauter AG) */ -describe("grid-per-axis", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; -var origFunc = Dygraph.getContext; +import Proxy from './Proxy'; +import CanvasAssertions from './CanvasAssertions'; +import Util from './Util'; +import PixelSampler from './PixelSampler'; -beforeEach(function() { - document.body.innerHTML = "
"; - Dygraph.getContext = function(canvas) { - return new Proxy(origFunc(canvas)); - }; -}); +describe("grid-per-axis", function() { -afterEach(function() { - Dygraph.getContext = origFunc; -}); +cleanupAfterEach(); +useProxyCanvas(utils, Proxy); it('testIndependentGrids', function() { var opts = { @@ -130,17 +125,19 @@ it('testPerAxisGridColors', function() { } var x, y; x = halfUp(g.plotter_.area.x); + var sampler = new PixelSampler(g); // Step through y(0) and y2(1) axis for ( var axis = 0; axis < 2; axis++) { // Step through all gridlines of the axis for ( var i = 0; i < gridlines[axis].length; i++) { y = halfDown(g.toDomYCoord(gridlines[axis][i], axis)); // Check the grid colors. - assert.deepEqual(gridColors[axis], Util.samplePixel(g.hidden_, x, y), + assert.deepEqual(gridColors[axis], sampler.colorAtPixel(x, y), "Unexpected grid color found at pixel: x: " + x + "y: " + y); } } }); + it('testPerAxisGridWidth', function() { var opts = { width : 480, @@ -192,6 +189,8 @@ it('testPerAxisGridWidth', function() { } var x, y; x = halfUp(g.plotter_.area.x + 10); + + var sampler = new PixelSampler(g); // Step through y(0) and y2(1) axis for ( var axis = 0; axis < 2; axis++) { // Step through all gridlines of the axis @@ -200,11 +199,11 @@ it('testPerAxisGridWidth', function() { // Ignore the alpha value // FIXME(pholden): this test fails with a context pixel ratio of 2. - var drawnPixeldown2 = Util.samplePixel(g.hidden_, x, y - 2).slice(0, 3); - var drawnPixeldown1 = Util.samplePixel(g.hidden_, x, y - 1).slice(0, 3); - var drawnPixel = Util.samplePixel(g.hidden_, x, y).slice(0, 3); - var drawnPixelup1 = Util.samplePixel(g.hidden_, x, y + 1).slice(0, 3); - var drawnPixelup2 = Util.samplePixel(g.hidden_, x, y + 2).slice(0, 3); + var drawnPixeldown2 = sampler.rgbAtPixel(x, y - 2); + var drawnPixeldown1 = sampler.rgbAtPixel(x, y - 1); + var drawnPixel = sampler.rgbAtPixel(x, y); + var drawnPixelup1 = sampler.rgbAtPixel(x, y + 1); + var drawnPixelup2 = sampler.rgbAtPixel(x, y + 2); // Check the grid width. switch (axis) { case 0: // y with 2 pixels width @@ -227,26 +226,26 @@ it('testPerAxisGridWidth', function() { y = halfDown(g.plotter_.area.y) + 10; for ( var i = 0; i < xGridlines.length; i++) { x = halfUp(g.toDomXCoord(xGridlines[i])); - assert.deepEqual(emptyColor, Util.samplePixel(g.hidden_, x - 4, y).slice(0, 3), + assert.deepEqual(emptyColor, sampler.rgbAtPixel(x - 4, y), "Unexpected x-grid color found at pixel: x: " + x + "y: " + y); - assert.deepEqual(gridColor, Util.samplePixel(g.hidden_, x - 3, y).slice(0, 3), + assert.deepEqual(gridColor, sampler.rgbAtPixel(x - 3, y), "Unexpected x-grid color found at pixel: x: " + x + "y: " + y); - assert.deepEqual(gridColor, Util.samplePixel(g.hidden_, x - 2, y).slice(0, 3), + assert.deepEqual(gridColor, sampler.rgbAtPixel(x - 2, y), "Unexpected x-grid color found at pixel: x: " + x + "y: " + y); - assert.deepEqual(gridColor, Util.samplePixel(g.hidden_, x - 1, y).slice(0, 3), + assert.deepEqual(gridColor, sampler.rgbAtPixel(x - 1, y), "Unexpected x-grid color found at pixel: x: " + x + "y: " + y); - assert.deepEqual(gridColor, Util.samplePixel(g.hidden_, x, y).slice(0, 3), + assert.deepEqual(gridColor, sampler.rgbAtPixel(x, y), "Unexpected x-grid color found at pixel: x: " + x + "y: " + y); - assert.deepEqual(gridColor, Util.samplePixel(g.hidden_, x + 1, y).slice(0, 3), + assert.deepEqual(gridColor, sampler.rgbAtPixel(x + 1, y), "Unexpected x-grid color found at pixel: x: " + x + "y: " + y); - assert.deepEqual(emptyColor, Util.samplePixel(g.hidden_, x + 2, y).slice(0, 3), + assert.deepEqual(emptyColor, sampler.rgbAtPixel(x + 2, y), "Unexpected x-grid color found at pixel: x: " + x + "y: " + y); } }); it('testGridLinePattern', function() { var opts = { - width : 120, + width : 480, height : 320, errorBars : false, labels : [ "X", "Left", "Right" ], @@ -287,6 +286,7 @@ it('testGridLinePattern', function() { return Math.round(y) - 1; } var x, y; + var sampler = new PixelSampler(g); // Step through all gridlines of the axis for (var i = 0; i < yGridlines.length; i++) { y = halfDown(g.toDomYCoord(yGridlines[i], 0)); @@ -295,9 +295,11 @@ it('testGridLinePattern', function() { // avoid checking the edge pixels since they differ depending on the OS. var pixelpos = x % 10; if(pixelpos < 1 || pixelpos > 8) continue; + + // XXX: check what this looks like at master // Ignore alpha - var drawnPixel = Util.samplePixel(g.hidden_, x, y).slice(0,3); + var drawnPixel = sampler.rgbAtPixel(x, y); var pattern = (Math.floor((x) / 10)) % 2; switch (pattern) { case 0: // fill diff --git a/auto_tests/tests/gviz.js b/auto_tests/tests/gviz.js index c663029..735e6f8 100644 --- a/auto_tests/tests/gviz.js +++ b/auto_tests/tests/gviz.js @@ -2,14 +2,13 @@ * Unit tests for GViz data table support. */ -describe('gviz', function() { +import Dygraph from '../../src/dygraph'; - beforeEach(function() { - document.body.innerHTML = "
"; - }); +import Util from './Util'; - afterEach(function() { - }); +describe('gviz', function() { + + cleanupAfterEach(); // This is a fake version of the gviz DataTable API, which can only be // sourced using the google js loader. diff --git a/auto_tests/tests/hidpi.js b/auto_tests/tests/hidpi.js index cdf0ee4..1dd131c 100644 --- a/auto_tests/tests/hidpi.js +++ b/auto_tests/tests/hidpi.js @@ -3,14 +3,17 @@ * * @author danvdk@gmail.com (Dan Vanderkam) */ + +import Dygraph from '../../src/dygraph'; + describe("hidpi", function() { +cleanupAfterEach(); + var savePixelRatio; beforeEach(function() { savePixelRatio = window.devicePixelRatio; window.devicePixelRatio = 2; - - document.body.innerHTML = "
"; }); afterEach(function() { diff --git a/auto_tests/tests/interaction_model.js b/auto_tests/tests/interaction_model.js index 3d6e3d0..a846df5 100644 --- a/auto_tests/tests/interaction_model.js +++ b/auto_tests/tests/interaction_model.js @@ -3,14 +3,14 @@ * * @author konigsberg@google.com (Robert Konigsbrg) */ -describe("interaction-model", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; +import DygraphInteraction from '../../src/dygraph-interaction-model'; +import DygraphOps from './DygraphOps'; -afterEach(function() { -}); +describe("interaction-model", function() { + +cleanupAfterEach(); var data1 = "X,Y\n" + "20,-1\n" + @@ -100,15 +100,15 @@ it('testClickCallbackIsCalledOnCustomPan', function() { function customDown(event, g, context) { context.initializeMouseDown(event, g, context); - Dygraph.startPan(event, g, context); + DygraphInteraction.startPan(event, g, context); } function customMove(event, g, context) { - Dygraph.movePan(event, g, context); + DygraphInteraction.movePan(event, g, context); } function customUp(event, g, context) { - Dygraph.endPan(event, g, context); + DygraphInteraction.endPan(event, g, context); } var opts = { @@ -153,7 +153,7 @@ it('testClickCallbackIsCalledWithNonInteractiveModel', function() { width: 100, height : 100, clickCallback : clickCallback, - interactionModel : Dygraph.Interaction.nonInteractiveModel_ + interactionModel : DygraphInteraction.nonInteractiveModel_ }; var graph = document.getElementById("graph"); diff --git a/auto_tests/tests/missing_points.js b/auto_tests/tests/missing_points.js index 48ef26d..dce01ed 100644 --- a/auto_tests/tests/missing_points.js +++ b/auto_tests/tests/missing_points.js @@ -23,21 +23,19 @@ * * @author konigsberg@google.com (Robert Konigsberg) */ + +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; +import Proxy from './Proxy'; +import CanvasAssertions from './CanvasAssertions'; +import Util from './Util'; + var ZERO_TO_FIFTY = [[ 10, 0 ] , [ 20, 50 ]]; describe("missing-points", function() { -var _origFunc = Dygraph.getContext; -beforeEach(function() { - document.body.innerHTML = "
"; - Dygraph.getContext = function(canvas) { - return new Proxy(_origFunc(canvas)); - } -}); - -afterEach(function() { - Dygraph.getContext = _origFunc; -}); +cleanupAfterEach(); +useProxyCanvas(utils, Proxy); it('testSeparatedPointsDontDraw', function() { var graph = document.getElementById("graph"); diff --git a/auto_tests/tests/multi_csv.js b/auto_tests/tests/multi_csv.js index c6b8d4f..99008e2 100644 --- a/auto_tests/tests/multi_csv.js +++ b/auto_tests/tests/multi_csv.js @@ -3,14 +3,12 @@ * * @author dan@dygraphs.com (Dan Vanderkam) */ -describe("multi-csv", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; -afterEach(function() { -}); +describe("multi-csv", function() { + +cleanupAfterEach(); function getXLabels() { var x_labels = document.getElementsByClassName("dygraph-axis-label-x"); diff --git a/auto_tests/tests/multiple_axes.js b/auto_tests/tests/multiple_axes.js index 3935b52..68c62a5 100644 --- a/auto_tests/tests/multiple_axes.js +++ b/auto_tests/tests/multiple_axes.js @@ -4,11 +4,13 @@ * @author danvdk@gmail.com (Dan Vanderkam) */ +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; +import Util from './Util'; + describe("multiple-axes-tests", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +cleanupAfterEach(); var getData = function() { var data = []; @@ -220,12 +222,12 @@ it('testDrawPointCallback', function() { var results = { y : {}, y2 : {}}; var firstCallback = function(g, seriesName, ctx, canvasx, canvasy, color, radius) { results.y[seriesName] = 1; - Dygraph.Circles.DEFAULT(g, seriesName, ctx, canvasx, canvasy, color, radius); + utils.Circles.DEFAULT(g, seriesName, ctx, canvasx, canvasy, color, radius); }; var secondCallback = function(g, seriesName, ctx, canvasx, canvasy, color, radius) { results.y2[seriesName] = 1; - Dygraph.Circles.DEFAULT(g, seriesName, ctx, canvasx, canvasy, color, radius); + utils.Circles.DEFAULT(g, seriesName, ctx, canvasx, canvasy, color, radius); }; var g = new Dygraph( diff --git a/auto_tests/tests/no_hours.js b/auto_tests/tests/no_hours.js index 323c84e..bcfc1fe 100644 --- a/auto_tests/tests/no_hours.js +++ b/auto_tests/tests/no_hours.js @@ -5,14 +5,13 @@ * * @author danvk@google.com (Dan Vanderkam) */ -describe("no-hours", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; +import Util from './Util'; -afterEach(function() { -}); +describe("no-hours", function() { + +cleanupAfterEach(); it('testNoHours', function() { var opts = { diff --git a/auto_tests/tests/numeric_ticker.js b/auto_tests/tests/numeric_ticker.js index 27707d4..b3b4620 100644 --- a/auto_tests/tests/numeric_ticker.js +++ b/auto_tests/tests/numeric_ticker.js @@ -6,22 +6,24 @@ * @author danvdk@gmail.com (Dan Vanderkam) */ +import Dygraph from '../../src/dygraph'; +import * as DygraphTickers from '../../src/dygraph-tickers'; +import DEFAULT_ATTRS from '../../src/dygraph-default-attrs'; + describe("numeric-ticker-tests", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +cleanupAfterEach(); var createOptionsViewForAxis = function(axis, dict) { return function (x) { if (dict && dict.hasOwnProperty(x)) { return dict[x]; } - if (Dygraph.DEFAULT_ATTRS.axes[axis].hasOwnProperty(x)) { - return Dygraph.DEFAULT_ATTRS.axes[axis][x]; + if (DEFAULT_ATTRS.axes[axis].hasOwnProperty(x)) { + return DEFAULT_ATTRS.axes[axis][x]; } - if (Dygraph.DEFAULT_ATTRS.hasOwnProperty(x)) { - return Dygraph.DEFAULT_ATTRS[x]; + if (DEFAULT_ATTRS.hasOwnProperty(x)) { + return DEFAULT_ATTRS[x]; } if (x == 'axisLabelFormatter') return null; throw "mysterious " + axis + "-axis option: " + x; @@ -32,7 +34,7 @@ it('testBasicNumericTicker', function() { var opts = {"logscale":null,"labelsKMG2":false,"labelsKMB":false}; var options = createOptionsViewForAxis('y', opts); - var ticks = Dygraph.numericTicks(-0.4, 4.4, 320, options); + var ticks = DygraphTickers.numericTicks(-0.4, 4.4, 320, options); var expected_ticks = [ {"v":-0.5,"label":"-0.5"}, {"v":0,"label":"0"}, @@ -47,7 +49,7 @@ it('testBasicNumericTicker', function() { {"v":4.5,"label":"4.5"}]; assert.deepEqual(expected_ticks, ticks); - ticks = Dygraph.numericTicks(1, 84, 540, options); + ticks = DygraphTickers.numericTicks(1, 84, 540, options); var expected_ticks = [ {"v":0,"label":"0"}, {"v":5,"label":"5"}, diff --git a/auto_tests/tests/parser.js b/auto_tests/tests/parser.js index e74cbef..6634cd5 100644 --- a/auto_tests/tests/parser.js +++ b/auto_tests/tests/parser.js @@ -3,14 +3,13 @@ * * @author danvk@google.com (Dan Vanderkam) */ -describe("parser", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; -afterEach(function() { -}); +describe("parser", function() { + +cleanupAfterEach(); it('testDetectLineDelimiter', function() { var data = "X,Y\r" + @@ -19,7 +18,7 @@ it('testDetectLineDelimiter', function() { "2,1\r" + "3,0\r" ; - assert.equal("\r", Dygraph.detectLineDelimiter(data)); + assert.equal("\r", utils.detectLineDelimiter(data)); data = "X,Y\n" + "0,-1\n" + @@ -27,7 +26,7 @@ it('testDetectLineDelimiter', function() { "2,1\n" + "3,0\n" ; - assert.equal("\n", Dygraph.detectLineDelimiter(data)); + assert.equal("\n", utils.detectLineDelimiter(data)); data = "X,Y\n\r" + "0,-1\n\r" + @@ -35,7 +34,7 @@ it('testDetectLineDelimiter', function() { "2,1\n\r" + "3,0\n\r" ; - assert.equal("\n\r", Dygraph.detectLineDelimiter(data)); + assert.equal("\n\r", utils.detectLineDelimiter(data)); }); it('testParseDosNewlines', function() { diff --git a/auto_tests/tests/pathological_cases.js b/auto_tests/tests/pathological_cases.js index 297167c..0bcab0b 100644 --- a/auto_tests/tests/pathological_cases.js +++ b/auto_tests/tests/pathological_cases.js @@ -4,12 +4,17 @@ * * @author dan@dygraphs.com (Dan Vanderkam) */ + +import Dygraph from '../../src/dygraph'; +import Util from './Util'; + describe("pathological-cases", function() { +cleanupAfterEach(); + var restoreConsole; var logs = {}; beforeEach(function() { - document.body.innerHTML = "
"; restoreConsole = Util.captureConsole(logs); }); @@ -17,6 +22,8 @@ afterEach(function() { restoreConsole(); }); +var graph = document.getElementById("graph"); + it('testZeroPoint', function() { var opts = { width: 480, @@ -24,7 +31,6 @@ it('testZeroPoint', function() { }; var data = "X,Y\n"; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); }); @@ -36,7 +42,6 @@ it('testOnePoint', function() { var data = "X,Y\n" + "1,2\n"; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); }); @@ -97,7 +102,7 @@ it('testCombinations', function() { var h = document.createElement('h3'); h.appendChild(document.createTextNode(baseName + ' ' + variantName)); - document.body.appendChild(h); + graph.appendChild(h); for (var dataName in dataSets) { var data = dataSets[dataName]; @@ -109,7 +114,7 @@ it('testCombinations', function() { var gdiv = document.createElement('div'); gdiv.style.display = 'inline-block'; box.appendChild(gdiv); - document.body.appendChild(box); + graph.appendChild(box); var cols = data && data[0] ? data[0].length : 0; opts.labels = ['X', 'A', 'B', 'C'].slice(0, cols); @@ -139,7 +144,6 @@ it('testNullLegend', function() { var data = "X,Y\n" + "1,2\n"; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); }); diff --git a/auto_tests/tests/per_axis.js b/auto_tests/tests/per_axis.js index 4ae6208..d7f1a49 100644 --- a/auto_tests/tests/per_axis.js +++ b/auto_tests/tests/per_axis.js @@ -3,9 +3,18 @@ * * @author konigsberg@google.com (Robert Konigsberg) */ + +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; + +import Util from './Util'; +import CanvasAssertions from './CanvasAssertions'; +import Proxy from './Proxy'; + describe("per-axis", function() { -var _origGetContext = Dygraph.getContext; +cleanupAfterEach(); +useProxyCanvas(utils, Proxy); var xAxisLineColor = "#00ffff"; var yAxisLineColor = "#ffff00"; @@ -13,11 +22,6 @@ var yAxisLineColor = "#ffff00"; var g, graph; beforeEach(function() { - document.body.innerHTML = "
"; - Dygraph.getContext = function(canvas) { - return new Proxy(_origGetContext(canvas)); - } - var opts = { axes : { x : { @@ -42,10 +46,6 @@ beforeEach(function() { g = new Dygraph(graph, data, opts); }); -afterEach(function() { - Dygraph.getContext = _origGetContext; -}); - it('testDrawXAxis', function() { g.updateOptions({ axes : { x : { drawAxis: true }} }); assert.isTrue(graph.getElementsByClassName('dygraph-axis-label-x').length > 0); diff --git a/auto_tests/tests/per_series.js b/auto_tests/tests/per_series.js index 07af084..f3ff16d 100644 --- a/auto_tests/tests/per_series.js +++ b/auto_tests/tests/per_series.js @@ -3,14 +3,14 @@ * * @author danvk@google.com (Dan Vanderkam) */ -describe("per-series", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; -afterEach(function() { -}); +import PixelSampler from './PixelSampler'; + +describe("per-series", function() { + +cleanupAfterEach(); it('testPerSeriesFill', function() { var opts = { diff --git a/auto_tests/tests/plugins.js b/auto_tests/tests/plugins.js index 1abccd4..f339656 100644 --- a/auto_tests/tests/plugins.js +++ b/auto_tests/tests/plugins.js @@ -3,13 +3,17 @@ * * @author konigsberg@google.com (Robert Konigsberg) */ + +import Dygraph from '../../src/dygraph'; +import DygraphOps from './DygraphOps'; + describe("plugins", function() { +cleanupAfterEach(); + var data; beforeEach(function() { - document.body.innerHTML = "
"; - data = "X,Y1,Y2\n" + "0,1,2\n" + "1,2,1\n" + @@ -18,9 +22,6 @@ beforeEach(function() { ; }); -afterEach(function() { -}); - it('testWillDrawChart', function() { var draw = 0; diff --git a/auto_tests/tests/plugins_legend.js b/auto_tests/tests/plugins_legend.js index 124d929..26daf92 100644 --- a/auto_tests/tests/plugins_legend.js +++ b/auto_tests/tests/plugins_legend.js @@ -1,10 +1,16 @@ +import Dygraph from '../../src/dygraph'; +import LegendPlugin from '../../src/plugins/legend'; +import Util from './Util'; + describe("plugins-legend", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +var graph; -afterEach(function() { +cleanupAfterEach(); +beforeEach(function() { + var testDiv = document.getElementById('graph'); + testDiv.innerHTML = "
"; + graph = document.getElementById('inner-graph'); }); it('testLegendEscape', function() { @@ -19,10 +25,9 @@ it('testLegendEscape', function() { "3,0\n" ; - var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); - var legendPlugin = new Dygraph.Plugins.Legend(); + var legendPlugin = new LegendPlugin(); legendPlugin.activate(g); var e = { selectedX: 'selectedX', @@ -42,7 +47,7 @@ it('testLegendEscape', function() { it('should let labelsDiv be a string', function() { var labelsDiv = document.getElementById('label'); - var g = new Dygraph('graph', 'X,Y\n1,2\n', {labelsDiv: 'label'}); + var g = new Dygraph(graph, 'X,Y\n1,2\n', {labelsDiv: 'label'}); null g.setSelection(0); assert.equal('1: Y: 2', Util.nbspToSpace(labelsDiv.textContent)); @@ -50,14 +55,14 @@ null it('should let labelsDiv be an Element', function() { var labelsDiv = document.getElementById('label'); - var g = new Dygraph('graph', 'X,Y\n1,2\n', { labelsDiv: labelsDiv }); + var g = new Dygraph(graph, 'X,Y\n1,2\n', { labelsDiv: labelsDiv }); assert.isNull(labelsDiv.getAttribute('class')); // dygraph-legend not added. g.setSelection(0); assert.equal('1: Y: 2', Util.nbspToSpace(labelsDiv.textContent)); }); it('should render dashed patterns', function() { - var g = new Dygraph('graph', 'X,Y\n1,2\n', { + var g = new Dygraph(graph, 'X,Y\n1,2\n', { strokePattern: [5, 5], color: 'red', legend: 'always' diff --git a/auto_tests/tests/range_selector.js b/auto_tests/tests/range_selector.js index 584e054..82884c8 100644 --- a/auto_tests/tests/range_selector.js +++ b/auto_tests/tests/range_selector.js @@ -4,12 +4,23 @@ * @fileoverview Regression tests for range selector. * @author paul.eric.felix@gmail.com (Paul Felix) */ + +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; +import RangeSelectorPlugin from '../../src/plugins/range-selector'; + +import Util from './Util'; +import DygraphOps from './DygraphOps'; +import CanvasAssertions from './CanvasAssertions'; +import Proxy from './Proxy'; + describe("range-selector", function() { +cleanupAfterEach(); + var restoreConsole; var logs = {}; beforeEach(function() { - document.body.innerHTML = "
"; restoreConsole = Util.captureConsole(logs); }); @@ -403,9 +414,9 @@ it('testRangeSelectorPositionIfXAxisNotDrawn', function() { it('testMiniPlotDrawn', function() { // Install Proxy to track canvas calls. - var origFunc = Dygraph.getContext; + var origFunc = utils.getContext; var miniHtx; - Dygraph.getContext = function(canvas) { + utils.getContext = function(canvas) { if (canvas.className != 'dygraph-rangesel-bgcanvas') { return origFunc(canvas); } @@ -435,7 +446,7 @@ it('testMiniPlotDrawn', function() { assert.isNotNull(miniHtx); assert.isTrue(0 < CanvasAssertions.numLinesDrawn(miniHtx, '#ff0000')); - Dygraph.getContext = origFunc; + utils.getContext = origFunc; }); // Tests data computation for the mini plot with a single series. @@ -452,7 +463,7 @@ it('testSingleCombinedSeries', function() { var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); - var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector); + var rangeSelector = g.getPluginInstance_(RangeSelectorPlugin); assert.isNotNull(rangeSelector); var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); @@ -482,7 +493,7 @@ it('testCombinedSeries', function() { var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); - var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector); + var rangeSelector = g.getPluginInstance_(RangeSelectorPlugin); assert.isNotNull(rangeSelector); var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); @@ -515,7 +526,7 @@ it('testSelectedCombinedSeries', function() { var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); - var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector); + var rangeSelector = g.getPluginInstance_(RangeSelectorPlugin); assert.isNotNull(rangeSelector); var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); @@ -545,7 +556,7 @@ it('testSingleCombinedSeriesCustomBars', function() { var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); - var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector); + var rangeSelector = g.getPluginInstance_(RangeSelectorPlugin); assert.isNotNull(rangeSelector); var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); @@ -574,7 +585,7 @@ it('testSingleCombinedSeriesErrorBars', function() { var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); - var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector); + var rangeSelector = g.getPluginInstance_(RangeSelectorPlugin); assert.isNotNull(rangeSelector); var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); @@ -604,7 +615,7 @@ it('testTwoCombinedSeriesCustomBars', function() { var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); - var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector); + var rangeSelector = g.getPluginInstance_(RangeSelectorPlugin); assert.isNotNull(rangeSelector); var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); diff --git a/auto_tests/tests/range_tests.js b/auto_tests/tests/range_tests.js index 1391a2a..ff1018e 100644 --- a/auto_tests/tests/range_tests.js +++ b/auto_tests/tests/range_tests.js @@ -24,6 +24,12 @@ * * @author konigsberg@google.com (Robert Konigsberg) */ + +import Dygraph from '../../src/dygraph'; + +import DygraphOps from './DygraphOps'; +import {assertDeepCloseTo} from './custom_asserts'; + var ZERO_TO_FIFTY = [[ 10, 0 ] , [ 20, 50 ]]; var ZERO_TO_FIFTY_STEPS = (function() { var a = []; @@ -42,9 +48,7 @@ var FIVE_TO_ONE_THOUSAND = [ describe("range-tests", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +cleanupAfterEach(); var createGraph = function(opts, data, expectRangeX, expectRangeY) { if (data === undefined) data = ZERO_TO_FIFTY_STEPS; diff --git a/auto_tests/tests/resize.js b/auto_tests/tests/resize.js index 9a8cffe..6230ffb 100644 --- a/auto_tests/tests/resize.js +++ b/auto_tests/tests/resize.js @@ -3,8 +3,16 @@ * * @author konigsberg@google.com (Robert Konigsberg) */ + +import Dygraph from '../../src/dygraph'; + +import DygraphOps from './DygraphOps'; +import Util from './Util'; + describe("resize", function() { +cleanupAfterEach(); + var data = "X,Y\n" + "1,100\n" + @@ -14,18 +22,9 @@ var data = "5,300\n" + "6,100\n"; -beforeEach(function() { - document.body.innerHTML = "
"; -}); - -afterEach(function() { -}); - it('testResizeMaintainsMouseOperations', function() { - document.body.innerHTML = - '
' + - ''; - var graph = document.getElementById("graph"); + var graph = document.getElementById('graph'); + graph.setAttribute('style', 'width: 640px; height: 480px;'); var callbackCount = 0; var callback = function() { @@ -46,7 +45,7 @@ it('testResizeMaintainsMouseOperations', function() { strum(g, 300, 640); assert.equal(6, callbackCount); - document.getElementById("graph").style.width = "500px"; + graph.style.width = "500px"; g.resize(); callbackCount = 0; diff --git a/auto_tests/tests/rolling_average.js b/auto_tests/tests/rolling_average.js index 5a16aa4..4b68da2 100644 --- a/auto_tests/tests/rolling_average.js +++ b/auto_tests/tests/rolling_average.js @@ -3,14 +3,14 @@ * * @author danvk@google.com (Dan Vanderkam) */ -describe("rolling-average", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; -afterEach(function() { -}); +import Util from './Util'; + +describe("rolling-average", function() { + +cleanupAfterEach(); it('testRollingAverage', function() { var opts = { diff --git a/auto_tests/tests/sanity.js b/auto_tests/tests/sanity.js index 0f3057c..e1d37ab 100644 --- a/auto_tests/tests/sanity.js +++ b/auto_tests/tests/sanity.js @@ -25,14 +25,14 @@ * @author konigsberg@google.com (Robert Konigsberg) */ +import Dygraph from '../../src/dygraph'; + describe("dygraphs-sanity", function() { var DEAD_SIMPLE_DATA = 'X,Y\n10,2100'; var ZERO_TO_FIFTY = 'X,Y\n10,0\n20,50'; -beforeEach(function() { - document.body.innerHTML = "
"; -}); +cleanupAfterEach(); /** * The sanity test of sanity tests. diff --git a/auto_tests/tests/scientific_notation.js b/auto_tests/tests/scientific_notation.js index ebbf2ed..a502484 100644 --- a/auto_tests/tests/scientific_notation.js +++ b/auto_tests/tests/scientific_notation.js @@ -5,14 +5,13 @@ * * @author danvk@google.com (Dan Vanderkam) */ -describe("scientific-notation", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; +import {assertDeepCloseTo} from './custom_asserts'; -afterEach(function() { -}); +describe("scientific-notation", function() { + +cleanupAfterEach(); function getXValues(g) { var xs = []; diff --git a/auto_tests/tests/scrolling_div.js b/auto_tests/tests/scrolling_div.js index ce95a7e..c9dec0c 100644 --- a/auto_tests/tests/scrolling_div.js +++ b/auto_tests/tests/scrolling_div.js @@ -3,6 +3,10 @@ * * @author konigsberg@google.com (Robert Konigsbrg) */ + +import Dygraph from '../../src/dygraph'; +import DygraphOps from './DygraphOps'; + describe("scrolling-div", function() { var point, g; @@ -18,13 +22,19 @@ var LOREM_IPSUM = "non proident, sunt in culpa qui officia deserunt mollit anim id est\n" + "laborum.

"; - document.body.innerHTML = + var testDiv = document.getElementById('graph'); + testDiv.innerHTML = "
" + - "
" + + "
" + "
" + LOREM_IPSUM + "
" + "
" + LOREM_IPSUM + "
" + "
"; + // The old test runner had an 8px margin on the body + // The Mocha test runner does not. We set it on the test div to keep the + // coordinates the same. + testDiv.style.margin = '8px'; + var data = [ [ 10, 1 ], [ 20, 3 ], @@ -35,7 +45,7 @@ var LOREM_IPSUM = [ 70, 4 ], [ 80, 6 ] ]; - var graph = document.getElementById("graph"); + var graph = document.getElementById("graph-inner"); point = null; g = new Dygraph(graph, data, @@ -74,9 +84,6 @@ var detectScrollbarWidth = function() { return scrollbarWidth; }; -afterEach(function() { -}); - /** * This tests that when the nested div is unscrolled, things work normally. */ diff --git a/auto_tests/tests/selection.js b/auto_tests/tests/selection.js index 02f1227..bb8e852 100644 --- a/auto_tests/tests/selection.js +++ b/auto_tests/tests/selection.js @@ -6,11 +6,12 @@ * @author danvk@google.com (Dan Vanderkam) */ +import Dygraph from '../../src/dygraph'; +import DefaultHandler from '../../src/datahandler/default'; + describe("selection", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +cleanupAfterEach(); it('testSetGetSelection', function() { var graph = document.getElementById("graph"); @@ -51,7 +52,7 @@ it('testSetGetSelectionDense', function() { it('testSetGetSelectionMissingPoints', function() { var dataHandler = function() {}; - dataHandler.prototype = new Dygraph.DataHandlers.DefaultHandler(); + dataHandler.prototype = new DefaultHandler(); dataHandler.prototype.seriesToPoints = function(series, setName, boundaryIdStart) { var val = null; if (setName == 'A') { diff --git a/auto_tests/tests/simple_drawing.js b/auto_tests/tests/simple_drawing.js index bda0e84..2ef84f3 100644 --- a/auto_tests/tests/simple_drawing.js +++ b/auto_tests/tests/simple_drawing.js @@ -24,21 +24,19 @@ * @author konigsberg@google.com (Robert Konigsberg) */ -describe("simple-drawing", function() { +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; -var ZERO_TO_FIFTY = 'X,Y\n10,0\n20,50'; +import CanvasAssertions from './CanvasAssertions'; +import Proxy from './Proxy'; +import PixelSampler from './PixelSampler'; -var _origFunc = Dygraph.getContext; -beforeEach(function() { - document.body.innerHTML = "
"; - Dygraph.getContext = function(canvas) { - return new Proxy(_origFunc(canvas)); - } -}); +describe("simple-drawing", function() { -afterEach(function() { - Dygraph.getContext = _origFunc; -}); +cleanupAfterEach(); +useProxyCanvas(utils, Proxy); + +var ZERO_TO_FIFTY = 'X,Y\n10,0\n20,50'; it('testDrawSimpleRangePlusOne', function() { var opts = { diff --git a/auto_tests/tests/smooth_plotter.js b/auto_tests/tests/smooth_plotter.js index 41b0f5d..146e426 100644 --- a/auto_tests/tests/smooth_plotter.js +++ b/auto_tests/tests/smooth_plotter.js @@ -3,8 +3,13 @@ * * @author danvdk@gmail.com (Dan Vanderkam) */ + +import Dygraph from '../../src/dygraph'; +import '../../src/extras/smooth-plotter'; // defines Dygraph.smoothPlotter + describe("smooth-plotter", function() { +var smoothPlotter = Dygraph.smoothPlotter; var getControlPoints = smoothPlotter._getControlPoints; beforeEach(function() { diff --git a/auto_tests/tests/stacked.js b/auto_tests/tests/stacked.js index 5e4c55c..3d997af 100644 --- a/auto_tests/tests/stacked.js +++ b/auto_tests/tests/stacked.js @@ -3,20 +3,18 @@ * * @author dan@dygraphs.com (Dan Vanderkam) */ -describe("stacked", function() { -var _origGetContext = Dygraph.getContext; +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; -beforeEach(function() { - document.body.innerHTML = "
"; - Dygraph.getContext = function(canvas) { - return new Proxy(_origGetContext(canvas)); - } -}); +import Proxy from './Proxy'; +import CanvasAssertions from './CanvasAssertions'; +import Util from './Util'; -afterEach(function() { - Dygraph.getContext = _origGetContext; -}); +describe("stacked", function() { + +cleanupAfterEach(); +useProxyCanvas(utils, Proxy); it('testCorrectColors', function() { var opts = { diff --git a/auto_tests/tests/step_plot_per_series.js b/auto_tests/tests/step_plot_per_series.js index a5ff5ee..805a65a 100644 --- a/auto_tests/tests/step_plot_per_series.js +++ b/auto_tests/tests/step_plot_per_series.js @@ -8,24 +8,17 @@ * * @author julian.eichstaedt@ch.sauter-bc.com (Fr. Sauter AG) */ -describe("step-plot-per-series", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; -var origFunc = Dygraph.getContext; +import Proxy from './Proxy'; +import CanvasAssertions from './CanvasAssertions'; -beforeEach(function() { - document.body.innerHTML = "
"; - Dygraph.getContext = function(canvas) { - return new Proxy(origFunc(canvas)); - }; -}); +describe("step-plot-per-series", function() { -afterEach(function() { - Dygraph.getContext = origFunc; -}); +cleanupAfterEach(); +useProxyCanvas(utils, Proxy); it('testMixedModeStepAndLineFilled', function() { var opts = { diff --git a/auto_tests/tests/synchronize.js b/auto_tests/tests/synchronize.js index 173b32f..fdc20dc 100644 --- a/auto_tests/tests/synchronize.js +++ b/auto_tests/tests/synchronize.js @@ -3,6 +3,12 @@ * * @author nyx@nyx.cz (Marek Janda) */ + +import Dygraph from '../../src/dygraph'; +import '../../src/extras/synchronizer'; // Sets Dygraph.synchronize + +import DygraphOps from './DygraphOps'; + describe("synchronize", function() { var gs; var originalCallbackCalled; @@ -12,9 +18,10 @@ describe("synchronize", function() { "12,1,4,2\n" + "13,0,2,3\n"; var h_row, h_pts; + var graph = document.getElementById('graph'); beforeEach(function() { - document.body.innerHTML = "
"; + graph.innerHTML = "
"; originalCallbackCalled = false; h_row = 0, h_pts = []; gs = []; diff --git a/auto_tests/tests/to_dom_coords.js b/auto_tests/tests/to_dom_coords.js index 5aa4520..4540501 100644 --- a/auto_tests/tests/to_dom_coords.js +++ b/auto_tests/tests/to_dom_coords.js @@ -3,20 +3,17 @@ * * @author danvk@google.com (Dan Vanderkam) */ +import Dygraph from '../../src/dygraph'; +import * as utils from '../../src/dygraph-utils'; -describe("to-dom-coords", function() { +import Proxy from './Proxy'; +import CanvasAssertions from './CanvasAssertions'; +import {assertDeepCloseTo} from './custom_asserts'; -var origFunc = Dygraph.getContext; -beforeEach(function() { - document.body.innerHTML = "
"; - Dygraph.getContext = function(canvas) { - return new Proxy(origFunc(canvas)); - } -}); +describe("to-dom-coords", function() { -afterEach(function() { - Dygraph.getContext = origFunc; -}); +cleanupAfterEach(); +useProxyCanvas(utils, Proxy); // Checks that toDomCoords and toDataCoords are inverses of one another. var checkForInverses = function(g) { diff --git a/auto_tests/tests/two_digit_years.js b/auto_tests/tests/two_digit_years.js index 9e2d490..76241f8 100644 --- a/auto_tests/tests/two_digit_years.js +++ b/auto_tests/tests/two_digit_years.js @@ -3,6 +3,12 @@ * * @author gmadrid@gmail.com (George Madrid) */ + +import Dygraph from '../../src/dygraph'; +import {Granularity, getDateAxis} from '../../src/dygraph-tickers'; +import * as utils from '../../src/dygraph-utils'; +import DEFAULT_ATTRS from '../../src/dygraph-default-attrs'; + describe("two-digit-years", function() { it('testTwoDigitYears', function() { @@ -16,8 +22,8 @@ it('testTwoDigitYears', function() { start.setFullYear(9); end.setFullYear(11); - var ticks = Dygraph.getDateAxis(start, end, Dygraph.QUARTERLY, function(x) { - return Dygraph.DEFAULT_ATTRS.axes['x'][x]; + var ticks = getDateAxis(start, end, Granularity.QUARTERLY, function(x) { + return DEFAULT_ATTRS.axes['x'][x]; }); // This breaks in Firefox & Safari: diff --git a/auto_tests/tests/update_options.js b/auto_tests/tests/update_options.js index d2465c9..e581110 100644 --- a/auto_tests/tests/update_options.js +++ b/auto_tests/tests/update_options.js @@ -4,7 +4,12 @@ * @fileoverview Tests for the updateOptions function. * @author antrob@google.com (Anthony Robledo) */ + +import Dygraph from '../../src/dygraph'; + describe("update-options", function() { + +cleanupAfterEach(); var opts = { width: 480, @@ -18,13 +23,6 @@ var data = "X,Y1,Y2\n" + "2011-04-04,9,5\n" + "2011-05-05,8,3\n"; -beforeEach(function() { - document.body.innerHTML = "
"; -}); - -afterEach(function() { -}); - /* * Tweaks the dygraph so it sets g._testDrawCalled to true when internal method * drawGraph_ is called. Call unWrapDrawGraph when done with this. diff --git a/auto_tests/tests/update_while_panning.js b/auto_tests/tests/update_while_panning.js index ac01900..c810f1a 100644 --- a/auto_tests/tests/update_while_panning.js +++ b/auto_tests/tests/update_while_panning.js @@ -5,14 +5,13 @@ * * @author dan@dygraphs.com (Dan Vanderkam) */ -describe("update-while-panning", function() { -beforeEach(function() { - document.body.innerHTML = "
"; -}); +import Dygraph from '../../src/dygraph'; +import DygraphOps from './DygraphOps'; -afterEach(function() { -}); +describe("update-while-panning", function() { + +cleanupAfterEach(); // This tests the following sequence: // 1. Begin dragging a chart (x-panning) diff --git a/auto_tests/tests/utils_test.js b/auto_tests/tests/utils_test.js index e8ebe5a..6d96e26 100644 --- a/auto_tests/tests/utils_test.js +++ b/auto_tests/tests/utils_test.js @@ -4,6 +4,8 @@ * @author danvdk@gmail.com (Dan Vanderkam) */ +import * as utils from '../../src/dygraph-utils'; + describe("utils-tests", function() { it('testUpdate', function() { @@ -18,13 +20,13 @@ it('testUpdate', function() { assert.deepEqual({x: 1, y: 2}, a['c']); assert.deepEqual({f: 10, g: 20}, a['d']); - Dygraph.update(a, { c: { x: 2 } }); + utils.update(a, { c: { x: 2 } }); assert.deepEqual({x: 2}, a['c']); - Dygraph.update(a, { d: null }); + utils.update(a, { d: null }); assert.equal(null, a['d']); - Dygraph.update(a, { a: 10, b: [1, 2] }); + utils.update(a, { a: 10, b: [1, 2] }); assert.equal(10, a['a']); assert.deepEqual([1, 2], a['b']); assert.deepEqual({x: 2}, a['c']); @@ -43,13 +45,13 @@ it('testUpdateDeep', function() { assert.deepEqual({x: 1, y: 2}, a['c']); assert.deepEqual({f: 10, g: 20}, a['d']); - Dygraph.updateDeep(a, { c: { x: 2 } }); + utils.updateDeep(a, { c: { x: 2 } }); assert.deepEqual({x: 2, y: 2}, a['c']); - Dygraph.updateDeep(a, { d: null }); + utils.updateDeep(a, { d: null }); assert.equal(null, a['d']); - Dygraph.updateDeep(a, { a: 10, b: [1, 2] }); + utils.updateDeep(a, { a: 10, b: [1, 2] }); assert.equal(10, a['a']); assert.deepEqual([1, 2], a['b']); assert.deepEqual({x: 2, y: 2}, a['c']); @@ -64,7 +66,7 @@ it('testUpdateDeepDecoupled', function() { }; var b = {}; - Dygraph.updateDeep(b, a); + utils.updateDeep(b, a); b.a = 2; assert.equal(1, a.a); @@ -79,7 +81,7 @@ it('testUpdateDeepDecoupled', function() { it('testIterator_nopredicate', function() { var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; - var iter = Dygraph.createIterator(array, 1, 4); + var iter = utils.createIterator(array, 1, 4); assert.isTrue(iter.hasNext); assert.equal('b', iter.peek); assert.equal('b', iter.next()); @@ -99,7 +101,7 @@ it('testIterator_nopredicate', function() { it('testIterator_predicate', function() { var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; - var iter = Dygraph.createIterator(array, 1, 4, + var iter = utils.createIterator(array, 1, 4, function(array, idx) { return array[idx] !== 'd' }); assert.isTrue(iter.hasNext); assert.equal('b', iter.peek); @@ -117,13 +119,13 @@ it('testIterator_predicate', function() { it('testIterator_empty', function() { var array = []; - var iter = Dygraph.createIterator([], 0, 0); + var iter = utils.createIterator([], 0, 0); assert.isFalse(iter.hasNext); }); it('testIterator_outOfRange', function() { var array = ['a', 'b', 'c']; - var iter = Dygraph.createIterator(array, 1, 4, + var iter = utils.createIterator(array, 1, 4, function(array, idx) { return array[idx] !== 'd' }); assert.isTrue(iter.hasNext); assert.equal('b', iter.peek); @@ -140,7 +142,7 @@ it('testIterator_outOfRange', function() { // with invalid boundaries. it('testIterator_whole_array', function() { var array = ['a', 'b', 'c']; - var iter = Dygraph.createIterator(array, 0, array.length, + var iter = utils.createIterator(array, 0, array.length, function(array, idx) { if (idx < 0 || idx >= array.length) { throw "err"; @@ -160,7 +162,7 @@ it('testIterator_whole_array', function() { it('testIterator_no_args', function() { var array = ['a', 'b', 'c']; - var iter = Dygraph.createIterator(array); + var iter = utils.createIterator(array); assert.isTrue(iter.hasNext); assert.equal('a', iter.next()); assert.isTrue(iter.hasNext); @@ -172,15 +174,15 @@ it('testIterator_no_args', function() { }); it('testToRGB', function() { - assert.deepEqual({r: 255, g: 200, b: 150}, Dygraph.toRGB_('rgb(255,200,150)')); - assert.deepEqual({r: 255, g: 200, b: 150}, Dygraph.toRGB_('#FFC896')); - assert.deepEqual({r: 255, g: 0, b: 0}, Dygraph.toRGB_('red')); + assert.deepEqual({r: 255, g: 200, b: 150}, utils.toRGB_('rgb(255,200,150)')); + assert.deepEqual({r: 255, g: 200, b: 150}, utils.toRGB_('#FFC896')); + assert.deepEqual({r: 255, g: 0, b: 0}, utils.toRGB_('red')); assert.deepEqual({r: 255, g: 200, b: 150, a: 0.6}, - Dygraph.toRGB_('rgba(255, 200, 150, 0.6)')); + utils.toRGB_('rgba(255, 200, 150, 0.6)')); }); it('testIsPixelChangingOptionList', function() { - var isPx = Dygraph.isPixelChangingOptionList; + var isPx = utils.isPixelChangingOptionList; assert.isTrue(isPx([], { axes: { y: { digitsAfterDecimal: 3 }}})); assert.isFalse(isPx([], { axes: { y: { axisLineColor: 'blue' }}})); }); diff --git a/auto_tests/tests/visibility.js b/auto_tests/tests/visibility.js index 5f54639..1cc3764 100644 --- a/auto_tests/tests/visibility.js +++ b/auto_tests/tests/visibility.js @@ -3,14 +3,12 @@ * @author sergeyslepian@gmail.com */ -describe("visibility", function() { +import Dygraph from '../../src/dygraph'; +import Util from './Util'; -beforeEach(function() { - document.body.innerHTML = "
"; -}); +describe("visibility", function() { -afterEach(function() { -}); +cleanupAfterEach(); /** * Does a bunch of the shared busywork of setting up a graph and changing its visibility. diff --git a/package.json b/package.json index b26f91d..4d1f576 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "devDependencies": { "babelify": "^6.3.0", "browserify": "^11.2.0", + "chai": "^3.3.0", "closure-compiler": "^0.2.6", "coveralls": "^2.11.2", "gulp": "^3.8.10", diff --git a/src/datahandler/bars-custom.js b/src/datahandler/bars-custom.js index 7313ca2..d4e94d1 100644 --- a/src/datahandler/bars-custom.js +++ b/src/datahandler/bars-custom.js @@ -9,20 +9,19 @@ * @author David Eberlein (david.eberlein@ch.sauter-bc.com) */ -(function() { - /*global Dygraph:false */ "use strict"; +import BarsHandler from './bars'; + /** * @constructor * @extends Dygraph.DataHandlers.BarsHandler */ -Dygraph.DataHandlers.CustomBarsHandler = function() { +var CustomBarsHandler = function() { }; -var CustomBarsHandler = Dygraph.DataHandlers.CustomBarsHandler; -CustomBarsHandler.prototype = new Dygraph.DataHandlers.BarsHandler(); +CustomBarsHandler.prototype = new BarsHandler(); /** @inheritDoc */ CustomBarsHandler.prototype.extractSeries = function(rawData, i, options) { @@ -100,4 +99,4 @@ CustomBarsHandler.prototype.rollingAverage = return rollingData; }; -})(); +export default CustomBarsHandler; diff --git a/src/datahandler/bars-error.js b/src/datahandler/bars-error.js index 71dbe34..4a9e0bf 100644 --- a/src/datahandler/bars-error.js +++ b/src/datahandler/bars-error.js @@ -9,20 +9,19 @@ * @author David Eberlein (david.eberlein@ch.sauter-bc.com) */ -(function() { - /*global Dygraph:false */ "use strict"; +import BarsHandler from './bars'; + /** * @constructor - * @extends Dygraph.DataHandlers.BarsHandler + * @extends BarsHandler */ -Dygraph.DataHandlers.ErrorBarsHandler = function() { +var ErrorBarsHandler = function() { }; -var ErrorBarsHandler = Dygraph.DataHandlers.ErrorBarsHandler; -ErrorBarsHandler.prototype = new Dygraph.DataHandlers.BarsHandler(); +ErrorBarsHandler.prototype = new BarsHandler(); /** @inheritDoc */ ErrorBarsHandler.prototype.extractSeries = function(rawData, i, options) { @@ -99,4 +98,4 @@ ErrorBarsHandler.prototype.rollingAverage = return rollingData; }; -})(); +export default ErrorBarsHandler; diff --git a/src/datahandler/bars-fractions.js b/src/datahandler/bars-fractions.js index 8594c07..1cbc2a4 100644 --- a/src/datahandler/bars-fractions.js +++ b/src/datahandler/bars-fractions.js @@ -10,20 +10,19 @@ * @author David Eberlein (david.eberlein@ch.sauter-bc.com) */ -(function() { - /*global Dygraph:false */ "use strict"; +import BarsHandler from './bars'; + /** * @constructor * @extends Dygraph.DataHandlers.BarsHandler */ -Dygraph.DataHandlers.FractionsBarsHandler = function() { +var FractionsBarsHandler = function() { }; -var FractionsBarsHandler = Dygraph.DataHandlers.FractionsBarsHandler; -FractionsBarsHandler.prototype = new Dygraph.DataHandlers.BarsHandler(); +FractionsBarsHandler.prototype = new BarsHandler(); /** @inheritDoc */ FractionsBarsHandler.prototype.extractSeries = function(rawData, i, options) { @@ -110,4 +109,4 @@ FractionsBarsHandler.prototype.rollingAverage = return rollingData; }; -})(); +export default FractionsBarsHandler; diff --git a/src/datahandler/bars.js b/src/datahandler/bars.js index 7100148..b7975dc 100644 --- a/src/datahandler/bars.js +++ b/src/datahandler/bars.js @@ -11,23 +11,21 @@ * @author David Eberlein (david.eberlein@ch.sauter-bc.com) */ -(function() { - /*global Dygraph:false */ /*global DygraphLayout:false */ "use strict"; +import DygraphDataHandler from './datahandler'; +import DygraphLayout from '../dygraph-layout'; + /** * @constructor * @extends {Dygraph.DataHandler} */ -Dygraph.DataHandlers.BarsHandler = function() { - Dygraph.DataHandler.call(this); +var BarsHandler = function() { + DygraphDataHandler.call(this); }; -Dygraph.DataHandlers.BarsHandler.prototype = new Dygraph.DataHandler(); - -// alias for the rest of the implementation -var BarsHandler = Dygraph.DataHandlers.BarsHandler; +BarsHandler.prototype = new DygraphDataHandler(); // TODO(danvk): figure out why the jsdoc has to be copy/pasted from superclass. // (I get closure compiler errors if this isn't here.) @@ -66,8 +64,8 @@ BarsHandler.prototype.onPointsCreated_ = function(series, points) { var point = points[i]; point.y_top = NaN; point.y_bottom = NaN; - point.yval_minus = Dygraph.DataHandler.parseFloat(item[2][0]); - point.yval_plus = Dygraph.DataHandler.parseFloat(item[2][1]); + point.yval_minus = DygraphDataHandler.parseFloat(item[2][0]); + point.yval_plus = DygraphDataHandler.parseFloat(item[2][1]); } }; @@ -106,4 +104,4 @@ BarsHandler.prototype.onLineEvaluated = function(points, axis, logscale) { } }; -})(); +export default BarsHandler; diff --git a/src/datahandler/default-fractions.js b/src/datahandler/default-fractions.js index 35d36eb..2bf1793 100644 --- a/src/datahandler/default-fractions.js +++ b/src/datahandler/default-fractions.js @@ -9,20 +9,20 @@ * @author David Eberlein (david.eberlein@ch.sauter-bc.com) */ -(function() { - /*global Dygraph:false */ "use strict"; +import DygraphDataHandler from './datahandler'; +import DefaultHandler from './default'; + /** - * @extends Dygraph.DataHandlers.DefaultHandler + * @extends DefaultHandler * @constructor */ -Dygraph.DataHandlers.DefaultFractionHandler = function() { +var DefaultFractionHandler = function() { }; -var DefaultFractionHandler = Dygraph.DataHandlers.DefaultFractionHandler; -DefaultFractionHandler.prototype = new Dygraph.DataHandlers.DefaultHandler(); +DefaultFractionHandler.prototype = new DefaultHandler(); DefaultFractionHandler.prototype.extractSeries = function(rawData, i, options) { // TODO(danvk): pre-allocate series here. @@ -84,4 +84,4 @@ DefaultFractionHandler.prototype.rollingAverage = function(originalData, rollPer return rollingData; }; -})(); +export default DefaultFractionHandler; diff --git a/src/dygraph-canvas.js b/src/dygraph-canvas.js index 256edc1..7ba66ff 100644 --- a/src/dygraph-canvas.js +++ b/src/dygraph-canvas.js @@ -28,6 +28,7 @@ "use strict"; import * as utils from './dygraph-utils'; +import Dygraph from './dygraph'; /** @@ -153,7 +154,7 @@ DygraphCanvasRenderer._drawStyledLine = function(e, var ctx = e.drawingContext; ctx.save(); if (stroking) { - ctx.installPattern(strokePattern); + if (ctx.setLineDash) ctx.setLineDash(strokePattern); } var pointsOnLine = DygraphCanvasRenderer._drawSeries( @@ -162,7 +163,7 @@ DygraphCanvasRenderer._drawStyledLine = function(e, e, pointsOnLine, drawPointCallback, color, pointSize); if (stroking) { - ctx.uninstallPattern(); + if (ctx.setLineDash) ctx.setLineDash([]); } ctx.restore(); diff --git a/src/dygraph-gviz.js b/src/dygraph-gviz.js index d2d7a0d..12c55a7 100644 --- a/src/dygraph-gviz.js +++ b/src/dygraph-gviz.js @@ -17,17 +17,18 @@ * - http://dygraphs.com/tests/annotation-gviz.html */ -(function() { /*global Dygraph:false */ "use strict"; +import Dygraph from './dygraph'; + /** * A wrapper around Dygraph that implements the gviz API. * @param {!HTMLDivElement} container The DOM object the visualization should * live in. * @constructor */ -Dygraph.GVizChart = function(container) { +var GVizChart = function(container) { this.container = container; }; @@ -35,7 +36,7 @@ Dygraph.GVizChart = function(container) { * @param {GVizDataTable} data * @param {Object.<*>} options */ -Dygraph.GVizChart.prototype.draw = function(data, options) { +GVizChart.prototype.draw = function(data, options) { // Clear out any existing dygraph. // TODO(danvk): would it make more sense to simply redraw using the current // date_graph object? @@ -53,7 +54,7 @@ Dygraph.GVizChart.prototype.draw = function(data, options) { * @param {Array.<{row:number}>} selection_array array of the selected cells * @public */ -Dygraph.GVizChart.prototype.setSelection = function(selection_array) { +GVizChart.prototype.setSelection = function(selection_array) { var row = false; if (selection_array.length) { row = selection_array[0].row; @@ -66,7 +67,7 @@ Dygraph.GVizChart.prototype.setSelection = function(selection_array) { * @return {Array.<{row:number,column:number}>} array of the selected cells * @public */ -Dygraph.GVizChart.prototype.getSelection = function() { +GVizChart.prototype.getSelection = function() { var selection = []; var row = this.date_graph.getSelection(); @@ -81,4 +82,4 @@ Dygraph.GVizChart.prototype.getSelection = function() { return selection; }; -})(); +export default GVizChart; diff --git a/src/dygraph-tickers.js b/src/dygraph-tickers.js index 7eb17dd..31ae852 100644 --- a/src/dygraph-tickers.js +++ b/src/dygraph-tickers.js @@ -301,7 +301,7 @@ TICK_PLACEMENT[Granularity.CENTENNIAL] = {datefield: DateField.DATEFIELD_Y, * This is a list of human-friendly values at which to show tick marks on a log * scale. It is k * 10^n, where k=1..9 and n=-39..+39, so: * ..., 1, 2, 3, 4, 5, ..., 9, 10, 20, 30, ..., 90, 100, 200, 300, ... - * NOTE: this assumes that Dygraph.LOG_SCALE = 10. + * NOTE: this assumes that utils.LOG_SCALE = 10. * @type {Array.} */ var PREFERRED_LOG_TICK_VALUES = (function() { diff --git a/src/dygraph-utils.js b/src/dygraph-utils.js index d16da53..5ca8d67 100644 --- a/src/dygraph-utils.js +++ b/src/dygraph-utils.js @@ -330,7 +330,6 @@ export var DateAccessorsUTC = { * @private */ export function hmsString_(hh, mm, ss) { - var zeropad = Dygraph.zeropad; var ret = zeropad(hh) + ":" + zeropad(mm); if (ss) { ret += ":" + zeropad(ss); @@ -599,7 +598,7 @@ export function clone(o) { var r = []; for (var i = 0; i < o.length; i++) { if (isArrayLike(o[i])) { - r.push(Dygraph.clone(o[i])); + r.push(clone(o[i])); } else { r.push(o[i]); } @@ -768,7 +767,7 @@ export function repeatAndCleanup(repeatFn, maxFrames, framePeriodInMillis, (function loop() { if (frameNumber >= maxFrames) return; - Dygraph.requestAnimFrame.call(window, function() { + requestAnimFrame.call(window, function() { // Determine which frame to draw based on the delay so far. Will skip // frames if necessary. var currentTime = new Date().getTime(); diff --git a/src/dygraph.js b/src/dygraph.js index 396b858..321b8fa 100644 --- a/src/dygraph.js +++ b/src/dygraph.js @@ -54,8 +54,12 @@ import DygraphInteraction from './dygraph-interaction-model'; import * as DygraphTickers from './dygraph-tickers'; import * as utils from './dygraph-utils'; import DEFAULT_ATTRS from './dygraph-default-attrs'; -import DygraphDataHandler from './datahandler/datahandler'; + import DefaultHandler from './datahandler/default'; +import ErrorBarsHandler from './datahandler/bars-error'; +import CustomBarsHandler from './datahandler/bars-custom'; +import DefaultFractionHandler from './datahandler/default-fractions'; +import FractionsBarsHandler from './datahandler/bars-fractions'; import AnnotationsPlugin from './plugins/annotations'; import AxesPlugin from './plugins/axes'; @@ -64,6 +68,8 @@ import GridPlugin from './plugins/grid'; import LegendPlugin from './plugins/legend'; import RangeSelectorPlugin from './plugins/range-selector'; +import GVizChart from './dygraph-gviz'; + /*global DygraphLayout:false, DygraphCanvasRenderer:false, DygraphOptions:false, G_vmlCanvasManager:false,ActiveXObject:false */ "use strict"; @@ -648,10 +654,10 @@ Dygraph.prototype.toDataXCoord = function(x) { // // Use both sides as the exponent in 10^exp and we're done. // x = 10 ^ (log(xRange[0]) + (pct * (log(xRange[1]) - log(xRange[0]))) - var logr0 = Dygraph.log10(xRange[0]); - var logr1 = Dygraph.log10(xRange[1]); + var logr0 = utils.log10(xRange[0]); + var logr1 = utils.log10(xRange[1]); var exponent = logr0 + (pct * (logr1 - logr0)); - var value = Math.pow(Dygraph.LOG_SCALE, exponent); + var value = Math.pow(utils.LOG_SCALE, exponent); return value; } }; @@ -697,10 +703,10 @@ Dygraph.prototype.toDataYCoord = function(y, axis) { // // Use both sides as the exponent in 10^exp and we're done. // y = 10 ^ (log(yRange[1]) - (pct * (log(yRange[1]) - log(yRange[0])))); - var logr0 = Dygraph.log10(yRange[0]); - var logr1 = Dygraph.log10(yRange[1]); + var logr0 = utils.log10(yRange[0]); + var logr1 = utils.log10(yRange[1]); var exponent = logr1 - (pct * (logr1 - logr0)); - var value = Math.pow(Dygraph.LOG_SCALE, exponent); + var value = Math.pow(utils.LOG_SCALE, exponent); return value; } }; @@ -732,9 +738,9 @@ Dygraph.prototype.toPercentYCoord = function(y, axis) { var pct; var logscale = this.attributes_.getForAxis("logscale", axis); if (logscale) { - var logr0 = Dygraph.log10(yRange[0]); - var logr1 = Dygraph.log10(yRange[1]); - pct = (logr1 - Dygraph.log10(y)) / (logr1 - logr0); + var logr0 = utils.log10(yRange[0]); + var logr1 = utils.log10(yRange[1]); + pct = (logr1 - utils.log10(y)) / (logr1 - logr0); } else { // yRange[1] - y is unit distance from the bottom. // yRange[1] - yRange[0] is the scale of the range. @@ -766,9 +772,9 @@ Dygraph.prototype.toPercentXCoord = function(x) { var pct; var logscale = this.attributes_.getForAxis("logscale", 'x') ; if (logscale === true) { // logscale can be null so we test for true explicitly. - var logr0 = Dygraph.log10(xRange[0]); - var logr1 = Dygraph.log10(xRange[1]); - pct = (Dygraph.log10(x) - logr0) / (logr1 - logr0); + var logr0 = utils.log10(xRange[0]); + var logr1 = utils.log10(xRange[1]); + pct = (utils.log10(x) - logr0) / (logr1 - logr0); } else { // x - xRange[0] is unit distance from the left. // xRange[1] - xRange[0] is the scale of the range. @@ -932,11 +938,11 @@ Dygraph.prototype.destroy = function() { this.removeTrackedEvents_(); // remove mouse event handlers (This may not be necessary anymore) - Dygraph.removeEvent(window, 'mouseout', this.mouseOutHandler_); - Dygraph.removeEvent(this.mouseEventElement_, 'mousemove', this.mouseMoveHandler_); + utils.removeEvent(window, 'mouseout', this.mouseOutHandler_); + utils.removeEvent(this.mouseEventElement_, 'mousemove', this.mouseMoveHandler_); // remove window handlers - Dygraph.removeEvent(window,'resize', this.resizeHandler_); + utils.removeEvent(window,'resize', this.resizeHandler_); this.resizeHandler_ = null; removeRecursive(this.maindiv_); @@ -1019,7 +1025,7 @@ Dygraph.prototype.setColors_ = function() { // alternate colors for high contrast. var idx = i % 2 ? (half + (i + 1)/ 2) : Math.ceil((i + 1) / 2); var hue = (1.0 * idx / (1 + num)); - colorStr = Dygraph.hsvToRGB(hue, sat, val); + colorStr = utils.hsvToRGB(hue, sat, val); } } this.colors_.push(colorStr); @@ -1573,7 +1579,7 @@ Dygraph.prototype.findClosestPoint = function(domX, domY) { var points = this.layout_.points[setIdx]; for (var i = 0; i < points.length; ++i) { point = points[i]; - if (!Dygraph.isValidPoint(point)) continue; + if (!utils.isValidPoint(point)) continue; dx = point.canvasx - domX; dy = point.canvasy - domY; dist = dx * dx + dy * dy; @@ -1614,12 +1620,12 @@ Dygraph.prototype.findStackedPoint = function(domX, domY) { var points = this.layout_.points[setIdx]; if (rowIdx >= points.length) continue; var p1 = points[rowIdx]; - if (!Dygraph.isValidPoint(p1)) continue; + if (!utils.isValidPoint(p1)) continue; var py = p1.canvasy; if (domX > p1.canvasx && rowIdx + 1 < points.length) { // interpolate series Y value using next point var p2 = points[rowIdx + 1]; - if (Dygraph.isValidPoint(p2)) { + if (utils.isValidPoint(p2)) { var dx = p2.canvasx - p1.canvasx; if (dx > 0) { var r = (domX - p1.canvasx) / dx; @@ -1629,7 +1635,7 @@ Dygraph.prototype.findStackedPoint = function(domX, domY) { } else if (domX < p1.canvasx && rowIdx > 0) { // interpolate series Y value using previous point var p0 = points[rowIdx - 1]; - if (Dygraph.isValidPoint(p0)) { + if (utils.isValidPoint(p0)) { var dx = p1.canvasx - p0.canvasx; if (dx > 0) { var r = (p1.canvasx - domX) / dx; @@ -2014,14 +2020,14 @@ Dygraph.prototype.getHandlerClass_ = function() { handlerClass = this.attr_('dataHandler'); } else if (this.fractions_) { if (this.getBooleanOption('errorBars')) { - handlerClass = DygraphDataHandlers.FractionsBarsHandler; + handlerClass = FractionsBarsHandler; } else { - handlerClass = DygraphDataHandlers.DefaultFractionHandler; + handlerClass = DefaultFractionHandler; } } else if (this.getBooleanOption('customBars')) { - handlerClass = DygraphDataHandlers.CustomBarsHandler; + handlerClass = CustomBarsHandler; } else if (this.getBooleanOption('errorBars')) { - handlerClass = DygraphDataHandlers.ErrorBarsHandler; + handlerClass = ErrorBarsHandler; } else { handlerClass = DefaultHandler; } @@ -3548,4 +3554,6 @@ Dygraph.PLUGINS = [ GridPlugin ]; +Dygraph.GVizChart = GVizChart; + export default Dygraph; diff --git a/src/extras/smooth-plotter.js b/src/extras/smooth-plotter.js index 119bf3f..d0182f4 100644 --- a/src/extras/smooth-plotter.js +++ b/src/extras/smooth-plotter.js @@ -1,6 +1,13 @@ -var smoothPlotter = (function() { +(function() { "use strict"; +var Dygraph; +if (window.Dygraph) { + Dygraph = window.Dygraph; +} else if (typeof(module) !== 'undefined') { + Dygraph = require('../dygraph'); +} + /** * Given three sequential points, p0, p1 and p2, find the left and right * control points for p1. @@ -121,6 +128,10 @@ function smoothPlotter(e) { smoothPlotter.smoothing = 1/3; smoothPlotter._getControlPoints = getControlPoints; // for testing -return smoothPlotter; +// older versions exported a global. +// This will be removed in the future. +// The preferred way to access smoothPlotter is via Dygraph.smoothPlotter. +window.smoothPlotter = smoothPlotter; +Dygraph.smoothPlotter = smoothPlotter; })(); diff --git a/src/extras/synchronizer.js b/src/extras/synchronizer.js index dd4329c..bf5b6f3 100644 --- a/src/extras/synchronizer.js +++ b/src/extras/synchronizer.js @@ -35,7 +35,14 @@ /* global Dygraph:false */ 'use strict'; -Dygraph.synchronize = function(/* dygraphs..., opts */) { +var Dygraph; +if (window.Dygraph) { + Dygraph = window.Dygraph; +} else if (typeof(module) !== 'undefined') { + Dygraph = require('../dygraph'); +} + +var synchronize = function(/* dygraphs..., opts */) { if (arguments.length === 0) { throw 'Invalid invocation of Dygraph.synchronize(). Need >= 1 argument.'; } @@ -217,4 +224,6 @@ function attachSelectionHandlers(gs, prevCallbacks) { } } +Dygraph.synchronize = synchronize; + })(); diff --git a/src/plugins/grid.js b/src/plugins/grid.js index bf6e3fe..16a83a2 100644 --- a/src/plugins/grid.js +++ b/src/plugins/grid.js @@ -65,8 +65,9 @@ grid.prototype.willDrawChart = function(e) { for (i = 0; i < ticks.length; i++) { var axis = ticks[i][0]; if(drawGrid[axis]) { + ctx.save(); if (stroking[axis]) { - ctx.installPattern(strokePattern[axis]); + if (ctx.setLineDash) ctx.setLineDash(strokePattern[axis]); } ctx.strokeStyle = strokeStyles[axis]; ctx.lineWidth = lineWidths[axis]; @@ -76,12 +77,9 @@ grid.prototype.willDrawChart = function(e) { ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x + area.w, y); - ctx.closePath(); ctx.stroke(); - if (stroking[axis]) { - ctx.uninstallPattern(); - } + ctx.restore(); } } ctx.restore(); @@ -94,7 +92,7 @@ grid.prototype.willDrawChart = function(e) { var strokePattern = g.getOptionForAxis('gridLinePattern', 'x'); var stroking = strokePattern && (strokePattern.length >= 2); if (stroking) { - ctx.installPattern(strokePattern); + if (ctx.setLineDash) ctx.setLineDash(strokePattern); } ctx.strokeStyle = g.getOptionForAxis('gridLineColor', 'x'); ctx.lineWidth = g.getOptionForAxis('gridLineWidth', 'x'); @@ -108,7 +106,7 @@ grid.prototype.willDrawChart = function(e) { ctx.stroke(); } if (stroking) { - ctx.uninstallPattern(); + if (ctx.setLineDash) ctx.setLineDash([]); } ctx.restore(); } diff --git a/src/plugins/legend.js b/src/plugins/legend.js index 95a161c..2e667d2 100644 --- a/src/plugins/legend.js +++ b/src/plugins/legend.js @@ -26,12 +26,12 @@ import * as utils from '../dygraph-utils'; * * @constructor */ -var legend = function() { +var Legend = function() { this.legend_div_ = null; this.is_generated_div_ = false; // do we own this div, or was it user-specified? }; -legend.prototype.toString = function() { +Legend.prototype.toString = function() { return "Legend Plugin"; }; @@ -50,7 +50,7 @@ var generateLegendDashHTML; * @param {Dygraph} g Graph instance. * @return {object.} Mapping of event names to callbacks. */ -legend.prototype.activate = function(g) { +Legend.prototype.activate = function(g) { var div; var divWidth = g.getOption('labelsDivWidth'); @@ -77,7 +77,7 @@ legend.prototype.activate = function(g) { "overflow": "hidden"}; // TODO(danvk): get rid of labelsDivStyles? CSS is better. - Dygraph.update(messagestyle, g.getOption('labelsDivStyles')); + utils.update(messagestyle, g.getOption('labelsDivStyles')); div = document.createElement("div"); div.className = "dygraph-legend"; for (var name in messagestyle) { @@ -122,7 +122,7 @@ var escapeHTML = function(str) { return str.replace(/&/g, "&").replace(/"/g, """).replace(//g, ">"); }; -legend.prototype.select = function(e) { +Legend.prototype.select = function(e) { var xValue = e.selectedX; var points = e.selectedPoints; var row = e.selectedRow; @@ -156,12 +156,12 @@ legend.prototype.select = function(e) { this.legend_div_.style.top = topLegend + "px"; } - var html = legend.generateLegendHTML(e.dygraph, xValue, points, this.one_em_width_, row); + var html = Legend.generateLegendHTML(e.dygraph, xValue, points, this.one_em_width_, row); this.legend_div_.innerHTML = html; this.legend_div_.style.display = ''; }; -legend.prototype.deselect = function(e) { +Legend.prototype.deselect = function(e) { var legendMode = e.dygraph.getOption('legend'); if (legendMode !== 'always') { this.legend_div_.style.display = "none"; @@ -171,11 +171,11 @@ legend.prototype.deselect = function(e) { var oneEmWidth = calculateEmWidthInDiv(this.legend_div_); this.one_em_width_ = oneEmWidth; - var html = legend.generateLegendHTML(e.dygraph, undefined, undefined, oneEmWidth, null); + var html = Legend.generateLegendHTML(e.dygraph, undefined, undefined, oneEmWidth, null); this.legend_div_.innerHTML = html; }; -legend.prototype.didDrawChart = function(e) { +Legend.prototype.didDrawChart = function(e) { this.deselect(e); }; @@ -188,7 +188,7 @@ legend.prototype.didDrawChart = function(e) { * - its top edge is flush with the top edge of the charting area * @private */ -legend.prototype.predraw = function(e) { +Legend.prototype.predraw = function(e) { // Don't touch a user-specified labelsDiv. if (!this.is_generated_div_) return; @@ -205,7 +205,7 @@ legend.prototype.predraw = function(e) { * Called when dygraph.destroy() is called. * You should null out any references and detach any DOM elements. */ -legend.prototype.destroy = function() { +Legend.prototype.destroy = function() { this.legend_div_ = null; }; @@ -222,7 +222,7 @@ legend.prototype.destroy = function() { * 'always'}) and with dashed lines. * @param {number} row The selected row index. */ -legend.generateLegendHTML = function(g, x, sel_points, oneEmWidth, row) { +Legend.generateLegendHTML = function(g, x, sel_points, oneEmWidth, row) { // TODO(danvk): deprecate this option in place of {legend: 'never'} if (g.getOption('showLabelsOnHighlight') !== true) return ''; @@ -362,4 +362,4 @@ generateLegendDashHTML = function(strokePattern, color, oneEmWidth) { return dash; }; -export default legend; +export default Legend; diff --git a/tests/dashed-canvas.html b/tests/dashed-canvas.html index dc472fa..e12d998 100644 --- a/tests/dashed-canvas.html +++ b/tests/dashed-canvas.html @@ -1,6 +1,6 @@ - +

You should see solid black and blue lines with a dashed red line in between @@ -25,7 +25,8 @@ ctx.lineTo(580, 200); ctx.stroke(); // Draw a dashed line segment. -ctx.installPattern([10, 10]); +// ctx.installPattern([10, 10]); +ctx.setLineDash([10, 10]); ctx.strokeStyle = 'red'; ctx.beginPath(); ctx.moveTo(100, 50); @@ -42,7 +43,8 @@ ctx2.moveTo(100, 50); ctx2.lineTo(600, 50); ctx2.stroke(); -ctx.uninstallPattern(); +// ctx.uninstallPattern(); +ctx.setLineDash([]); // Now that we've uninstalled the pattern, should be normal again. ctx.strokeStyle = 'blue'; diff --git a/watch-tests.sh b/watch-tests.sh new file mode 100755 index 0000000..002f879 --- /dev/null +++ b/watch-tests.sh @@ -0,0 +1,63 @@ +watchify \ + -v -t babelify \ + --debug \ + -o dist/tests.js \ + auto_tests/tests/sanity.js \ + auto_tests/tests/pathological_cases.js \ + auto_tests/tests/axis_labels.js \ + auto_tests/tests/annotations.js \ + auto_tests/tests/callback.js \ + auto_tests/tests/connect_separated_points.js \ + auto_tests/tests/fill_step_plot.js \ + auto_tests/tests/custom_bars.js \ + auto_tests/tests/css.js \ + auto_tests/tests/data_api.js \ + auto_tests/tests/date_formats.js \ + auto_tests/tests/date_ticker.js \ + auto_tests/tests/error_bars.js \ + auto_tests/tests/dygraph-options-tests.js \ + auto_tests/tests/fast_canvas_proxy.js \ + auto_tests/tests/formats.js \ + auto_tests/tests/hidpi.js \ + auto_tests/tests/interaction_model.js \ + auto_tests/tests/missing_points.js \ + auto_tests/tests/multi_csv.js \ + + +cat < /dev/null + + +# These ones are going to be hard +cat < /dev/null