Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat(ngMock): $timeout.flushNext can expect specific timeout delays
Browse files Browse the repository at this point in the history
the $timeout mock's flush method allows flushing queued up requests
but doesn't allow to for checking with what delay a task was queued
up. flushNext flushes the next queued up task and can asserts the
scheduled delay.
  • Loading branch information
matsko authored and IgorMinar committed Jul 26, 2013
1 parent b7fdabc commit 462ed03
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ angular.mock.$Browser = function() {
self.deferredFns.shift().fn();
}
};

/**
* @name ngMock.$browser#defer.flushNext
* @methodOf ngMock.$browser
*
* @description
* Flushes next pending request and compares it to the provided delay
*
* @param {number=} expectedDelay the delay value that will be asserted against the delay of the next timeout function
*/
self.defer.flushNext = function(expectedDelay) {
var tick = self.deferredFns.shift();
expect(tick.time).toEqual(expectedDelay);
tick.fn();
};

/**
* @name ngMock.$browser#defer.now
* @propertyOf ngMock.$browser
Expand Down Expand Up @@ -1494,6 +1510,20 @@ angular.mock.$TimeoutDecorator = function($delegate, $browser) {
$browser.defer.flush(delay);
};

/**
* @ngdoc method
* @name ngMock.$timeout#flushNext
* @methodOf ngMock.$timeout
* @description
*
* Flushes the next timeout in the queue and compares it to the provided delay
*
* @param {number=} expectedDelay the delay value that will be asserted against the delay of the next timeout function
*/
$delegate.flushNext = function(expectedDelay) {
$browser.defer.flushNext(expectedDelay);
};

/**
* @ngdoc method
* @name ngMock.$timeout#verifyNoPendingTasks
Expand Down
15 changes: 15 additions & 0 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,21 @@ describe('ngMock', function() {
$timeout.flush(900);
expect(function() {$timeout.verifyNoPendingTasks();}).not.toThrow();
}));


it('should assert against the delay value', inject(function($timeout) {
var count = 0;
var iterate = function() {
count++;
};

$timeout(iterate, 100);
$timeout(iterate, 123);
$timeout.flushNext(100);
expect(count).toBe(1);
$timeout.flushNext(123);
expect(count).toBe(2);
}));
});


Expand Down

0 comments on commit 462ed03

Please sign in to comment.