From c83492e23fd4193768526533ccac24879c3f5720 Mon Sep 17 00:00:00 2001 From: Brad Hards Date: Sun, 26 Feb 2017 07:02:55 +1100 Subject: [PATCH] Initial tests for timeline ItemSet. (#2750) Somewhat more complicated setup, associated with the need for a real window. --- package.json | 1 + test/TestSupport.js | 7 ++- test/TimelineItemSet.test.js | 108 +++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 test/TimelineItemSet.test.js diff --git a/package.json b/package.json index bed62546..f7698cd3 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "gulp-rename": "^1.2.2", "gulp-util": "^3.0.8", "jsdom": "9.9.1", + "jsdom-global": "^2.1.1", "mocha": "^3.2.0", "mocha-jsdom": "^1.1.0", "rimraf": "^2.5.4", diff --git a/test/TestSupport.js b/test/TestSupport.js index d767c180..1de998b6 100644 --- a/test/TestSupport.js +++ b/test/TestSupport.js @@ -22,7 +22,12 @@ module.exports = { clientWidth: 1000 } }, - domProps: this.props, + domProps: { + centerContainer: { + width: 900, + height: 600 + } + }, emitter: { on: function () {}, off: function () {}, diff --git a/test/TimelineItemSet.test.js b/test/TimelineItemSet.test.js new file mode 100644 index 00000000..8892c49e --- /dev/null +++ b/test/TimelineItemSet.test.js @@ -0,0 +1,108 @@ +var assert = require('assert'); + +describe('Timeline ItemSet', function () { + before(function () { + delete require.cache[require.resolve('../dist/vis')] + this.jsdom = require('jsdom-global')(); + this.vis = require('../dist/vis'); + var TestSupport = require('./TestSupport'); + var rangeBody = TestSupport.buildSimpleTimelineRangeBody(); + this.testrange = new this.vis.timeline.Range(rangeBody); + this.testrange.setRange(new Date(2017, 1, 26, 13, 26, 3, 320), new Date(2017, 1, 26, 13, 26, 4, 320), false, false, null); + this.testitems = new this.vis.DataSet({ + type: { + start: 'Date', + end: 'Date' + } + }); + // add single items with different date types + this.testitems.add({id: 1, content: 'Item 1', start: new Date(2017, 1, 26, 13, 26, 3, 600), type: 'point'}); + this.testitems.add({id: 2, content: 'Item 2', start: new Date(2017, 1, 26, 13, 26, 5, 600), type: 'point'}); + }) + + after(function () { + this.jsdom(); + }) + + var getBasicBody = function() { + var body = { + dom: { + container: document.createElement('div'), + leftContainer: document.createElement('div'), + centerContainer: document.createElement('div'), + top: document.createElement('div'), + left: document.createElement('div'), + center: document.createElement('div'), + backgroundVertical: document.createElement('div') + }, + domProps: { + root: {}, + background: {}, + centerContainer: {}, + leftContainer: {}, + rightContainer: {}, + center: {}, + left: {}, + right: {}, + top: {}, + bottom: {}, + border: {}, + scrollTop: 0, + scrollTopMin: 0 + }, + emitter: { + on: function() {return {};}, + emit: function() {} + }, + util: { + } + } + return body; + }; + + it('should initialise with minimal data', function () { + var body = getBasicBody(); + var itemset = new this.vis.timeline.components.ItemSet(body, {}); + assert(itemset); + }); + + it('should redraw() and have the right classNames', function () { + var body = getBasicBody(); + body.range = this.testrange; + var itemset = new this.vis.timeline.components.ItemSet(body, {}); + itemset.redraw(); + assert.equal(itemset.dom.frame.className, 'vis-itemset'); + assert.equal(itemset.dom.background.className, 'vis-background'); + assert.equal(itemset.dom.foreground.className, 'vis-foreground'); + assert.equal(itemset.dom.axis.className, 'vis-axis'); + assert.equal(itemset.dom.labelSet.className, 'vis-labelset'); + }); + + it('should start with no items', function () { + var body = getBasicBody(); + var itemset = new this.vis.timeline.components.ItemSet(body, {}); + assert.equal(itemset.getItems(), null); + }); + + it('should store items correctly', function() { + var body = getBasicBody(); + body.range = this.testrange; + var DateUtil = this.vis.timeline.DateUtil; + body.util.toScreen = function(time) { + return DateUtil.toScreen({ + body: { + hiddenDates: [] + }, + range: { + conversion: function() { + return {offset: 0, scale: 100}; + } + } + }, time, 900) + }; + var itemset = new this.vis.timeline.components.ItemSet(body, {}); + itemset.setItems(this.testitems); + assert.equal(itemset.getItems().length, 2); + assert.deepEqual(itemset.getItems(), this.testitems); + }); +});