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

Commit 098b6f5

Browse files
dizel3dgkalpak
authored andcommitted
feat(ngMock/$httpBackend): flush requests in any order
Previously, requests were flushed in the order in which they were made. With this change, it is possible to flush requests in any order. This is useful for simulating more realistic scenarios, where parallel requests may be completed in any order. Partially addresses #13717. Closes #14967
1 parent 549edc9 commit 098b6f5

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

src/ngMock/angular-mocks.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,24 +1782,30 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
17821782
* @ngdoc method
17831783
* @name $httpBackend#flush
17841784
* @description
1785-
* Flushes all pending requests using the trained responses.
1786-
*
1787-
* @param {number=} count Number of responses to flush (in the order they arrived). If undefined,
1788-
* all pending requests will be flushed. If there are no pending requests when the flush method
1789-
* is called an exception is thrown (as this typically a sign of programming error).
1785+
* Flushes pending requests in the order they arrived beginning at specified request using the trained responses.
1786+
* If there are no pending requests to flush when the method is called
1787+
* an exception is thrown (as this typically a sign of programming error).
1788+
*
1789+
* @param {number=} count Number of responses to flush. If undefined,
1790+
* all pending requests from `skip` will be flushed.
1791+
* @param {number=} [skip=0] Number of pending requests to skip before flushing.
1792+
* So it specifies the first request to flush.
17901793
*/
1791-
$httpBackend.flush = function(count, digest) {
1794+
$httpBackend.flush = function(count, skip, digest) {
17921795
if (digest !== false) $rootScope.$digest();
1793-
if (!responses.length) throw new Error('No pending request to flush !');
1796+
1797+
skip = skip || 0;
1798+
if (skip >= responses.length) throw new Error('No pending request to flush !');
17941799

17951800
if (angular.isDefined(count) && count !== null) {
17961801
while (count--) {
1797-
if (!responses.length) throw new Error('No more pending request to flush !');
1798-
responses.shift()();
1802+
var part = responses.splice(skip, 1);
1803+
if (!part.length) throw new Error('No more pending request to flush !');
1804+
part[0]();
17991805
}
18001806
} else {
1801-
while (responses.length) {
1802-
responses.shift()();
1807+
while (responses.length > skip) {
1808+
responses.splice(skip, 1)[0]();
18031809
}
18041810
}
18051811
$httpBackend.verifyNoOutstandingExpectation(digest);

test/ng/httpSpec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,7 @@ describe('$http with $applyAsync', function() {
19851985
// Ensure requests are sent
19861986
$rootScope.$digest();
19871987

1988-
$httpBackend.flush(null, false);
1988+
$httpBackend.flush(null, null, false);
19891989
expect($rootScope.$applyAsync).toHaveBeenCalledOnce();
19901990
expect(handler).not.toHaveBeenCalled();
19911991

@@ -2003,7 +2003,7 @@ describe('$http with $applyAsync', function() {
20032003
// Ensure requests are sent
20042004
$rootScope.$digest();
20052005

2006-
$httpBackend.flush(null, false);
2006+
$httpBackend.flush(null, null, false);
20072007
expect(log).toEqual([]);
20082008

20092009
$browser.defer.flush();
@@ -2028,7 +2028,7 @@ describe('$http with $applyAsync', function() {
20282028
expect(log).toEqual(['response 1', 'response 2']);
20292029

20302030
// Finally, third response is received, and a second coalesced $apply is started
2031-
$httpBackend.flush(null, false);
2031+
$httpBackend.flush(null, null, false);
20322032
$browser.defer.flush();
20332033
expect(log).toEqual(['response 1', 'response 2', 'response 3']);
20342034
});

test/ngMock/angular-mocksSpec.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,36 @@ describe('ngMock', function() {
14391439
});
14401440

14411441

1442+
it('should flush given number of pending requests beginning at specified request', function() {
1443+
var dontCallMe = jasmine.createSpy('dontCallMe');
1444+
1445+
hb.when('GET').respond(200, '');
1446+
hb('GET', '/some', null, dontCallMe);
1447+
hb('GET', '/some', null, callback);
1448+
hb('GET', '/some', null, callback);
1449+
hb('GET', '/some', null, dontCallMe);
1450+
1451+
hb.flush(2, 1);
1452+
expect(dontCallMe).not.toHaveBeenCalled();
1453+
expect(callback).toHaveBeenCalledTimes(2);
1454+
});
1455+
1456+
1457+
it('should flush all pending requests beginning at specified request', function() {
1458+
var dontCallMe = jasmine.createSpy('dontCallMe');
1459+
1460+
hb.when('GET').respond(200, '');
1461+
hb('GET', '/some', null, dontCallMe);
1462+
hb('GET', '/some', null, dontCallMe);
1463+
hb('GET', '/some', null, callback);
1464+
hb('GET', '/some', null, callback);
1465+
1466+
hb.flush(null, 2);
1467+
expect(dontCallMe).not.toHaveBeenCalled();
1468+
expect(callback).toHaveBeenCalledTimes(2);
1469+
});
1470+
1471+
14421472
it('should throw exception when flushing more requests than pending', function() {
14431473
hb.when('GET').respond(200, '');
14441474
hb('GET', '/url', null, callback);
@@ -1453,8 +1483,9 @@ describe('ngMock', function() {
14531483

14541484
hb.when('GET').respond(200, '');
14551485
hb('GET', '/some', null, callback);
1456-
hb.flush();
1486+
expect(function() {hb.flush(null, 1);}).toThrowError('No pending request to flush !');
14571487

1488+
hb.flush();
14581489
expect(function() {hb.flush();}).toThrowError('No pending request to flush !');
14591490
});
14601491

0 commit comments

Comments
 (0)