diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 964bd78ceae1..be9e064257d3 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -106,19 +106,29 @@ angular.mock.$Browser = function() { * @param {number=} number of milliseconds to flush. See {@link #defer.now} */ self.defer.flush = function(delay) { + var nextTime; + if (angular.isDefined(delay)) { - self.defer.now += delay; + // A delay was passed so compute the next time + nextTime = self.defer.now + delay; } else { if (self.deferredFns.length) { - self.defer.now = self.deferredFns[self.deferredFns.length - 1].time; + // No delay was passed so set the next time so that it clears the deferred queue + nextTime = self.deferredFns[self.deferredFns.length - 1].time; } else { + // No delay passed, but there are no deferred tasks so flush - indicates an error! throw new Error('No deferred tasks to be flushed'); } } - while (self.deferredFns.length && self.deferredFns[0].time <= self.defer.now) { + while (self.deferredFns.length && self.deferredFns[0].time <= nextTime) { + // Increment the time and call the next deferred function + self.defer.now = self.deferredFns[0].time; self.deferredFns.shift().fn(); } + + // Ensure that the current time is correct + self.defer.now = nextTime; }; self.$$baseHref = '/'; diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index bf75adf05d75..1cfe423020f9 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -296,8 +296,10 @@ describe('ngMock', function() { expect(counter).toBe(1); $interval.flush(1000); - expect(counter).toBe(2); + + $interval.flush(2000); + expect(counter).toBe(4); })); @@ -692,6 +694,30 @@ describe('ngMock', function() { $timeout.flush(123); expect(count).toBe(2); })); + + it('should resolve timeout functions following the timeline', inject(function($timeout) { + var count1 = 0, count2 = 0; + var iterate1 = function() { + count1++; + $timeout(iterate1, 100); + }; + var iterate2 = function() { + count2++; + $timeout(iterate2, 150); + }; + + $timeout(iterate1, 100); + $timeout(iterate2, 150); + $timeout.flush(150); + expect(count1).toBe(1); + expect(count2).toBe(1); + $timeout.flush(50); + expect(count1).toBe(2); + expect(count2).toBe(1); + $timeout.flush(400); + expect(count1).toBe(6); + expect(count2).toBe(4); + })); });