diff --git a/test/PointItem.test.js b/test/PointItem.test.js new file mode 100644 index 00000000..1f88ea38 --- /dev/null +++ b/test/PointItem.test.js @@ -0,0 +1,65 @@ +var assert = require('assert'); +var vis = require('../dist/vis'); +var jsdom = require('mocha-jsdom'); +var moment = vis.moment; +var timeline = vis.timeline; +var PointItem = require("../lib/timeline/component/item/PointItem"); +var Range = timeline.Range; +var TestSupport = require('./TestSupport'); + +describe('Timeline PointItem', function () { + + jsdom(); + + it('should initialize with minimal data', function() { + var now = moment().toDate(); + var pointItem = new PointItem({start: now}, null, null); + assert.equal(pointItem.props.content.height, 0); + assert.equal(pointItem.data.start, now); + }); + + it('should have a default width of 0', function() { + var now = moment().toDate(); + var pointItem = new PointItem({start: now}, null, null); + assert.equal(pointItem.getWidthRight(), 0); + assert.equal(pointItem.getWidthLeft(), 0); + }); + + it('should error if there is missing data', function () { + assert.throws(function () { new PointItem({}, null, null)}, Error); + }); + + it('should be visible if the range is during', function() { + var now = moment(); + var range = new Range(TestSupport.buildSimpleTimelineRangeBody()); + range.start = now.clone().add(-1, 'second'); + range.end = range.start.clone().add(1, 'hour'); + var pointItem = new PointItem({start: now.toDate()}, null, null); + assert(pointItem.isVisible(range)); + }); + + it('should not be visible if the range is after', function() { + var now = moment(); + var range = new Range(TestSupport.buildSimpleTimelineRangeBody()); + range.start = now.clone().add(1, 'second'); + range.end = range.start.clone().add(1, 'hour'); + var pointItem = new PointItem({start: now.toDate()}, null, null); + assert(!pointItem.isVisible(range)); + }); + + it('should not be visible if the range is before', function() { + var now = moment(); + var range = new Range(TestSupport.buildSimpleTimelineRangeBody()); + range.end = now.clone().add(-1, 'second'); + range.start = range.end.clone().add(-1, 'hour'); + var pointItem = new PointItem({start: now.toDate()}, null, null); + assert(!pointItem.isVisible(range)); + }); + + it('should be visible for a "now" point with a default range', function() { + var range = new Range(TestSupport.buildSimpleTimelineRangeBody()); + var now = moment().toDate(); + var pointItem = new PointItem({start: now}, null, null); + assert(pointItem.isVisible(range)); + }); +}); \ No newline at end of file diff --git a/test/TestSupport.js b/test/TestSupport.js new file mode 100644 index 00000000..06d67852 --- /dev/null +++ b/test/TestSupport.js @@ -0,0 +1,22 @@ + +module.exports = { + buildSimpleTimelineRangeBody: function () { + var body = { + dom: { + center: { + clientWidth: 1000 + } + }, + domProps: this.props, + emitter: { + on: function () {}, + off: function () {}, + emit: function () {} + }, + hiddenDates: [], + util: {} + } + body.dom.rollingModeBtn = document.createElement('div') + return body + } +} diff --git a/test/TimelineRange.test.js b/test/TimelineRange.test.js index ba79142d..f73d84df 100644 --- a/test/TimelineRange.test.js +++ b/test/TimelineRange.test.js @@ -4,26 +4,7 @@ var jsdom = require('mocha-jsdom') var moment = vis.moment; var timeline = vis.timeline; var Range = timeline.Range; - -function buildSimpleBody() { - var body = { - dom: { - center: { - clientWidth: 1000 - } - }, - domProps: this.props, - emitter: { - on: function() {}, - off: function() {}, - emit: function() {} - }, - hiddenDates: [], - util: {} - }; - body.dom.rollingModeBtn = document.createElement('div'); - return body; -} +var TestSupport = require('./TestSupport'); describe('Timeline Range', function () { @@ -31,30 +12,30 @@ describe('Timeline Range', function () { it('should have start default before now', function () { var now = moment().hours(0).minutes(0).seconds(0).milliseconds(0).valueOf(); - var range = new Range(buildSimpleBody()); + var range = new Range(TestSupport.buildSimpleTimelineRangeBody()); assert(range.start < now, "Default start is before now"); }); it('should have end default after now', function () { var now = moment().hours(0).minutes(0).seconds(0).milliseconds(0).valueOf(); - var range = new Range(buildSimpleBody()); + var range = new Range(TestSupport.buildSimpleTimelineRangeBody()); assert(range.end > now, "Default end is after now"); }); it('should support custom start and end dates', function () { - var range = new Range(buildSimpleBody()); + var range = new Range(TestSupport.buildSimpleTimelineRangeBody()); range.setRange(new Date(2017, 0, 26, 13, 26, 3, 320), new Date(2017, 3, 11, 0, 23, 35, 0), false, false, null); assert.equal(range.start, new Date(2017, 0, 26, 13, 26, 3, 320).valueOf(), "start is as expected"); assert.equal(range.end, new Date(2017, 3, 11, 0, 23, 35, 0).valueOf(), "end is as expected"); }); it('should calculate milliseconds per pixel', function () { - var range = new Range(buildSimpleBody()); + var range = new Range(TestSupport.buildSimpleTimelineRangeBody()); assert(range.getMillisecondsPerPixel() > 0, "positive value for milliseconds per pixel"); }); it('should calculate 1 millisecond per pixel for simple range', function () { - var range = new Range(buildSimpleBody()); + var range = new Range(TestSupport.buildSimpleTimelineRangeBody()); range.setRange(new Date(2017, 0, 26, 13, 26, 3, 320), new Date(2017, 0, 26, 13, 26, 4, 320), false, false, null); assert.equal(range.getMillisecondsPerPixel(), 1, "one second over 1000 pixels"); });