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

Commit 462ed03

Browse files
matskoIgorMinar
authored andcommitted
feat(ngMock): $timeout.flushNext can expect specific timeout delays
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.
1 parent b7fdabc commit 462ed03

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/ngMock/angular-mocks.js

+30
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ angular.mock.$Browser = function() {
118118
self.deferredFns.shift().fn();
119119
}
120120
};
121+
122+
/**
123+
* @name ngMock.$browser#defer.flushNext
124+
* @methodOf ngMock.$browser
125+
*
126+
* @description
127+
* Flushes next pending request and compares it to the provided delay
128+
*
129+
* @param {number=} expectedDelay the delay value that will be asserted against the delay of the next timeout function
130+
*/
131+
self.defer.flushNext = function(expectedDelay) {
132+
var tick = self.deferredFns.shift();
133+
expect(tick.time).toEqual(expectedDelay);
134+
tick.fn();
135+
};
136+
121137
/**
122138
* @name ngMock.$browser#defer.now
123139
* @propertyOf ngMock.$browser
@@ -1494,6 +1510,20 @@ angular.mock.$TimeoutDecorator = function($delegate, $browser) {
14941510
$browser.defer.flush(delay);
14951511
};
14961512

1513+
/**
1514+
* @ngdoc method
1515+
* @name ngMock.$timeout#flushNext
1516+
* @methodOf ngMock.$timeout
1517+
* @description
1518+
*
1519+
* Flushes the next timeout in the queue and compares it to the provided delay
1520+
*
1521+
* @param {number=} expectedDelay the delay value that will be asserted against the delay of the next timeout function
1522+
*/
1523+
$delegate.flushNext = function(expectedDelay) {
1524+
$browser.defer.flushNext(expectedDelay);
1525+
};
1526+
14971527
/**
14981528
* @ngdoc method
14991529
* @name ngMock.$timeout#verifyNoPendingTasks

test/ngMock/angular-mocksSpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,21 @@ describe('ngMock', function() {
365365
$timeout.flush(900);
366366
expect(function() {$timeout.verifyNoPendingTasks();}).not.toThrow();
367367
}));
368+
369+
370+
it('should assert against the delay value', inject(function($timeout) {
371+
var count = 0;
372+
var iterate = function() {
373+
count++;
374+
};
375+
376+
$timeout(iterate, 100);
377+
$timeout(iterate, 123);
378+
$timeout.flushNext(100);
379+
expect(count).toBe(1);
380+
$timeout.flushNext(123);
381+
expect(count).toBe(2);
382+
}));
368383
});
369384

370385

0 commit comments

Comments
 (0)