From a46d8426ab2c01d24e5648331328c38418209266 Mon Sep 17 00:00:00 2001 From: MacLeod Broad <> Date: Sun, 5 Nov 2017 10:11:19 -0500 Subject: [PATCH] Fixes issue 3321 by ensuring labels are drawn as part of initial construction of the DataAxis --- lib/timeline/component/DataAxis.js | 5 +++- test/DataAxis.test.js | 46 ++++++++++++++++++++++++++++++ test/Label.test.js | 2 +- test/canvas-mock.js | 25 ++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 test/DataAxis.test.js diff --git a/lib/timeline/component/DataAxis.js b/lib/timeline/component/DataAxis.js index d1dd69f1..8ad33eb2 100644 --- a/lib/timeline/component/DataAxis.js +++ b/lib/timeline/component/DataAxis.js @@ -82,6 +82,9 @@ function DataAxis(body, options, svg, linegraphOptions) { // create the HTML DOM this._create(); + if (this.scale == undefined) { + this._redrawLabels(); + } this.framework = {svg: this.svg, svgElements: this.svgElements, options: this.options, groups: this.groups}; var me = this; @@ -493,7 +496,7 @@ DataAxis.prototype._redrawLabel = function (y, text, orientation, className, cha */ DataAxis.prototype._redrawLine = function (y, orientation, className, offset, width) { if (this.master === true) { - var line = DOMutil.getDOMElement('div', this.DOMelements.lines, this.dom.lineContainer);//this.dom.redundant.lines.shift(); + var line = DOMutil.getDOMElement('div', this.DOMelements.lines, this.dom.lineContainer); //this.dom.redundant.lines.shift(); line.className = className; line.innerHTML = ''; diff --git a/test/DataAxis.test.js b/test/DataAxis.test.js new file mode 100644 index 00000000..651cfc82 --- /dev/null +++ b/test/DataAxis.test.js @@ -0,0 +1,46 @@ +var assert = require('assert'); +var jsdom_global = require('jsdom-global'); +var canvasMockify = require('./canvas-mock'); + +var DataAxis = require('../lib/timeline/component/DataAxis'); + +describe('DataAxis', function () { + beforeEach(function() { + this.jsdom_global = canvasMockify("
"); + this.container = document.getElementById('mygraph'); + this.svg = this.container = document.getElementById('svg'); + this.body = { + functions: {}, + emitter: { + on: function() {} + } + }; + }); + + afterEach(function() { + this.jsdom_global(); + this.container.remove(); + this.container = undefined; + this.svg.remove(); + this.svg = undefined; + }); + + it('should work', function () { + var dataAxis = new DataAxis(this.body, {}, this.svg, {}); + + }); + + describe('screenToValue', function () { + it('can called be without an explicit redraw', function () { + var dataAxis = new DataAxis(this.body, {}, this.svg, {}); + assert(isNaN(dataAxis.screenToValue(77))); + }); + }); + + describe('convertValue', function () { + it('can called be without an explicit redraw', function () { + var dataAxis = new DataAxis(this.body, {}, this.svg, {}); + assert(isNaN(dataAxis.convertValue(77))); + }); + }); +}); diff --git a/test/Label.test.js b/test/Label.test.js index 73794edb..e0c3e064 100644 --- a/test/Label.test.js +++ b/test/Label.test.js @@ -54,7 +54,7 @@ describe('Network Label', function() { emitter: { on: function() {} } - } + }; var nodesHandler = new NodesHandler(body, {}, options, new DummyLayoutEngine() ); //console.log(JSON.stringify(nodesHandler.options, null, 2)); diff --git a/test/canvas-mock.js b/test/canvas-mock.js index 9ea5c535..5136568e 100644 --- a/test/canvas-mock.js +++ b/test/canvas-mock.js @@ -92,6 +92,29 @@ function overrideCreateElement(window) { }; } +/** + * The override is only done if there is no 2D context already present. + * This allows for normal running in a browser, and for node.js the usage of 'style' + * property on a newly created svg element. + * + * @param {object} window - current global window object. This can possibly come from module 'jsdom', + * when running under node.js. + * @private + */ +function overrideCreateElementNS(window) { + var d = window.document; + var f = window.document.createElementNS; + + window.document.createElementNS = function(namespaceURI, qualifiedName) { + if (namespaceURI === 'http://www.w3.org/2000/svg') { + var result = f.call(d, namespaceURI, qualifiedName); + if (result.style == undefined) { + result.style = {}; + return result; + } + } + }; +} /** * Initialize the mock, jsdom and jsdom_global for unit test usage. @@ -132,6 +155,8 @@ function mockify(html = '') { overrideCreateElement(window); // The actual initialization of canvas-mock + overrideCreateElementNS(window); + return cleanupFunction; }