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

Commit 1bcb28f

Browse files
committed
feat($httpBackend): flush requests in desired order
Made changes according to results of PR review.
1 parent 1b5025e commit 1bcb28f

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/ngMock/angular-mocks.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,29 +1788,30 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
17881788
* @ngdoc method
17891789
* @name $httpBackend#flush
17901790
* @description
1791-
* Flushes pending requests using the trained responses in the order they arrived.
1792-
* If there are no pending requests when the flush method is called
1791+
* Flushes pending requests in the order they arrived beginning at specified request using the trained responses.
1792+
* If there are no pending requests to flush when the method is called
17931793
* an exception is thrown (as this typically a sign of programming error).
17941794
*
17951795
* @param {number=} count Number of responses to flush. If undefined,
1796-
* all pending requests will be flushed beginning at `start`.
1797-
* @param {number=} [start=0] Sequential request number at which to begin to flush.
1796+
* all pending requests from `skip` will be flushed.
1797+
* @param {number=} [skip=0] Number of pending requests to skip before flushing.
1798+
* So it specifies the first request to flush.
17981799
*/
1799-
$httpBackend.flush = function(count, start, digest) {
1800+
$httpBackend.flush = function(count, skip, digest) {
18001801
if (digest !== false) $rootScope.$digest();
1801-
if (!responses.length) throw new Error('No pending request to flush !');
18021802

1803-
start = angular.isDefined(start) && start !== null ? start : 0;
1803+
skip = skip || 0;
1804+
if (skip >= responses.length) throw new Error('No pending request to flush !');
18041805

18051806
if (angular.isDefined(count) && count !== null) {
18061807
while (count--) {
1807-
var part = responses.splice(start, 1);
1808+
var part = responses.splice(skip, 1);
18081809
if (!part.length) throw new Error('No more pending request to flush !');
18091810
part[0]();
18101811
}
18111812
} else {
1812-
while (responses.length > start) {
1813-
responses.splice(start, 1)[0]();
1813+
while (responses.length > skip) {
1814+
responses.splice(skip, 1)[0]();
18141815
}
18151816
}
18161817
$httpBackend.verifyNoOutstandingExpectation(digest);

test/ngMock/angular-mocksSpec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,7 +1543,6 @@ describe('ngMock', function() {
15431543

15441544
hb.flush(2, 1);
15451545
expect(dontCallMe).not.toHaveBeenCalled();
1546-
expect(callback).toHaveBeenCalled();
15471546
expect(callback).toHaveBeenCalledTimes(2);
15481547
});
15491548

@@ -1559,7 +1558,6 @@ describe('ngMock', function() {
15591558

15601559
hb.flush(null, 2);
15611560
expect(dontCallMe).not.toHaveBeenCalled();
1562-
expect(callback).toHaveBeenCalled();
15631561
expect(callback).toHaveBeenCalledTimes(2);
15641562
});
15651563

@@ -1578,8 +1576,9 @@ describe('ngMock', function() {
15781576

15791577
hb.when('GET').respond(200, '');
15801578
hb('GET', '/some', null, callback);
1581-
hb.flush();
1579+
expect(function() {hb.flush(null, 1);}).toThrowError('No pending request to flush !');
15821580

1581+
hb.flush();
15831582
expect(function() {hb.flush();}).toThrowError('No pending request to flush !');
15841583
});
15851584

0 commit comments

Comments
 (0)