From 9d0664f133bf74bad9415b07fdebcd36e403ee48 Mon Sep 17 00:00:00 2001 From: etimberg Date: Wed, 21 Jun 2017 21:16:19 -0400 Subject: [PATCH] Ensure deprecated unitStepSize property of time scale is repsected + add tests --- src/scales/scale.time.js | 3 +- test/specs/global.deprecations.tests.js | 37 +++++++++++++++++++++++++ test/specs/scale.time.tests.js | 18 ++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 94f7cad6229..377133637b9 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -146,7 +146,8 @@ module.exports = function(Chart) { me.unit = unit; me.majorUnit = majorUnit; - var stepSize = timeOpts.stepSize || timeHelpers.determineStepSize(minTimestamp || dataMin, maxTimestamp || dataMax, unit, maxTicks); + var optionStepSize = helpers.getValueOrDefault(timeOpts.stepSize, timeOpts.unitStepSize); + var stepSize = optionStepSize || timeHelpers.determineStepSize(minTimestamp || dataMin, maxTimestamp || dataMax, unit, maxTicks); me.ticks = timeHelpers.generateTicks({ maxTicks: maxTicks, min: minTimestamp, diff --git a/test/specs/global.deprecations.tests.js b/test/specs/global.deprecations.tests.js index 917c1529a09..9eac9121218 100644 --- a/test/specs/global.deprecations.tests.js +++ b/test/specs/global.deprecations.tests.js @@ -163,6 +163,43 @@ describe('Deprecations', function() { }); }); }); + + describe('Time Axis: unitStepSize option', function() { + function createScale(data, options) { + var scaleID = 'myScale'; + var mockContext = window.createMockContext(); + var Constructor = Chart.scaleService.getScaleConstructor('time'); + var scale = new Constructor({ + ctx: mockContext, + options: options, + chart: { + data: data + }, + id: scaleID + }); + + scale.update(400, 50); + return scale; + } + + it('should use the stepSize property', function() { + var mockData = { + labels: ['2015-01-01T20:00:00', '2015-01-01T21:00:00'], + }; + + var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('time')); + config.time.unit = 'hour'; + config.time.unitStepSize = 2; + + var scale = createScale(mockData, config); + scale.update(2500, 200); + var ticks = scale.ticks.map(function(tick) { + return tick.value; + }); + + expect(ticks).toEqual(['8PM', '10PM']); + }); + }); }); describe('Version 2.5.0', function() { diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 86e0216e330..2b8c50ed969 100755 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -282,6 +282,24 @@ describe('Time scale tests', function() { expect(ticks).toEqual(['Dec 28, 2014', 'Jan 4, 2015', 'Jan 11, 2015', 'Jan 18, 2015', 'Jan 25, 2015', 'Feb 2015', 'Feb 8, 2015', 'Feb 15, 2015']); }); + describe('config step size', function() { + it('should use the stepSize property', function() { + var mockData = { + labels: ['2015-01-01T20:00:00', '2015-01-01T21:00:00'], + }; + + var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('time')); + config.time.unit = 'hour'; + config.time.stepSize = 2; + + var scale = createScale(mockData, config); + scale.update(2500, 200); + var ticks = getTicksValues(scale.ticks); + + expect(ticks).toEqual(['8PM', '10PM']); + }); + }); + describe('when specifying limits', function() { var mockData = { labels: ['2015-01-01T20:00:00', '2015-01-02T20:00:00', '2015-01-03T20:00:00'],