diff --git a/platform/features/timeline/src/controllers/TimelineZoomController.js b/platform/features/timeline/src/controllers/TimelineZoomController.js index 0ba5abbb28..1c21cbfe8c 100644 --- a/platform/features/timeline/src/controllers/TimelineZoomController.js +++ b/platform/features/timeline/src/controllers/TimelineZoomController.js @@ -66,7 +66,7 @@ define( var duration = timespan.getDuration(); zoomIndex = 0; while (toMillis(bounds.width) < duration && - zoomIndex < zoomLevels.length) { + zoomIndex < zoomLevels.length - 1) { zoomIndex += 1; } bounds.x = toPixels(timespan.getStart()); diff --git a/platform/features/timeline/test/controllers/TimelineZoomControllerSpec.js b/platform/features/timeline/test/controllers/TimelineZoomControllerSpec.js index 35b5bad8b4..b5f8e32f6f 100644 --- a/platform/features/timeline/test/controllers/TimelineZoomControllerSpec.js +++ b/platform/features/timeline/test/controllers/TimelineZoomControllerSpec.js @@ -32,11 +32,7 @@ define( beforeEach(function () { testConfiguration = { - levels: [ - 1000, - 2000, - 3500 - ], + levels: [ 1000, 2000, 3500 ], width: 12321 }; mockScope = jasmine.createSpyObj("$scope", ['$watch']); @@ -74,6 +70,63 @@ define( expect(controller.zoom()).toEqual(3500); }); + it("observes scroll bounds", function () { + expect(mockScope.$watch) + .toHaveBeenCalledWith("scroll", jasmine.any(Function)); + }); + + describe("when watches have fired", function () { + var mockDomainObject, + mockPromise, + mockTimespan, + testStart, + testEnd; + + beforeEach(function () { + testStart = 3000; + testEnd = 5500; + + mockDomainObject = jasmine.createSpyObj('domainObject', [ + 'getId', + 'getModel', + 'getCapability', + 'useCapability' + ]); + mockPromise = jasmine.createSpyObj('promise', ['then']); + mockTimespan = jasmine.createSpyObj('timespan', [ + 'getStart', + 'getEnd', + 'getDuration' + ]); + + mockDomainObject.useCapability.andCallFake(function (c) { + return c === 'timespan' && mockPromise; + }); + mockPromise.then.andCallFake(function (callback) { + callback(mockTimespan); + }); + mockTimespan.getStart.andReturn(testStart); + mockTimespan.getEnd.andReturn(testEnd); + mockTimespan.getDuration.andReturn(testEnd - testStart); + + mockScope.scroll = { x: 0, width: 20000 }; + mockScope.domainObject = mockDomainObject; + + mockScope.$watch.calls.forEach(function (call) { + call.args[1](mockScope[call.args[0]]); + }); + }); + + it("zooms to fit the timeline", function () { + var x1 = mockScope.scroll.x, + x2 = mockScope.scroll.x + mockScope.scroll.width; + expect(Math.round(controller.toMillis(x1))) + .toEqual(testStart); + expect(Math.round(controller.toMillis(x2))) + .toBeGreaterThan(testEnd); + }); + }); + }); }