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

Commit 72b6632

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 fdf8e0f commit 72b6632

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
@@ -1789,24 +1789,30 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
17891789
* @ngdoc method
17901790
* @name $httpBackend#flush
17911791
* @description
1792-
* Flushes all pending requests using the trained responses.
1793-
*
1794-
* @param {number=} count Number of responses to flush (in the order they arrived). If undefined,
1795-
* all pending requests will be flushed. If there are no pending requests when the flush method
1796-
* is called an exception is thrown (as this typically a sign of programming error).
1792+
* Flushes pending requests in the order they arrived beginning at specified request using the trained responses.
1793+
* If there are no pending requests to flush when the method is called
1794+
* an exception is thrown (as this typically a sign of programming error).
1795+
*
1796+
* @param {number=} count Number of responses to flush. If undefined,
1797+
* all pending requests from `skip` will be flushed.
1798+
* @param {number=} [skip=0] Number of pending requests to skip before flushing.
1799+
* So it specifies the first request to flush.
17971800
*/
1798-
$httpBackend.flush = function(count, digest) {
1801+
$httpBackend.flush = function(count, skip, digest) {
17991802
if (digest !== false) $rootScope.$digest();
1800-
if (!responses.length) throw new Error('No pending request to flush !');
1803+
1804+
skip = skip || 0;
1805+
if (skip >= responses.length) throw new Error('No pending request to flush !');
18011806

18021807
if (angular.isDefined(count) && count !== null) {
18031808
while (count--) {
1804-
if (!responses.length) throw new Error('No more pending request to flush !');
1805-
responses.shift()();
1809+
var part = responses.splice(skip, 1);
1810+
if (!part.length) throw new Error('No more pending request to flush !');
1811+
part[0]();
18061812
}
18071813
} else {
1808-
while (responses.length) {
1809-
responses.shift()();
1814+
while (responses.length > skip) {
1815+
responses.splice(skip, 1)[0]();
18101816
}
18111817
}
18121818
$httpBackend.verifyNoOutstandingExpectation(digest);

test/ng/httpSpec.js

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

2215-
$httpBackend.flush(null, false);
2215+
$httpBackend.flush(null, null, false);
22162216
expect($rootScope.$applyAsync).toHaveBeenCalledOnce();
22172217
expect(handler).not.toHaveBeenCalled();
22182218

@@ -2230,7 +2230,7 @@ describe('$http with $applyAsync', function() {
22302230
// Ensure requests are sent
22312231
$rootScope.$digest();
22322232

2233-
$httpBackend.flush(null, false);
2233+
$httpBackend.flush(null, null, false);
22342234
expect(log).toEqual([]);
22352235

22362236
$browser.defer.flush();
@@ -2255,7 +2255,7 @@ describe('$http with $applyAsync', function() {
22552255
expect(log).toEqual(['response 1', 'response 2']);
22562256

22572257
// Finally, third response is received, and a second coalesced $apply is started
2258-
$httpBackend.flush(null, false);
2258+
$httpBackend.flush(null, null, false);
22592259
$browser.defer.flush();
22602260
expect(log).toEqual(['response 1', 'response 2', 'response 3']);
22612261
});

test/ngMock/angular-mocksSpec.js

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

15341534

1535+
it('should flush given number of pending requests beginning at specified request', function() {
1536+
var dontCallMe = jasmine.createSpy('dontCallMe');
1537+
1538+
hb.when('GET').respond(200, '');
1539+
hb('GET', '/some', null, dontCallMe);
1540+
hb('GET', '/some', null, callback);
1541+
hb('GET', '/some', null, callback);
1542+
hb('GET', '/some', null, dontCallMe);
1543+
1544+
hb.flush(2, 1);
1545+
expect(dontCallMe).not.toHaveBeenCalled();
1546+
expect(callback).toHaveBeenCalledTimes(2);
1547+
});
1548+
1549+
1550+
it('should flush all pending requests beginning at specified request', function() {
1551+
var dontCallMe = jasmine.createSpy('dontCallMe');
1552+
1553+
hb.when('GET').respond(200, '');
1554+
hb('GET', '/some', null, dontCallMe);
1555+
hb('GET', '/some', null, dontCallMe);
1556+
hb('GET', '/some', null, callback);
1557+
hb('GET', '/some', null, callback);
1558+
1559+
hb.flush(null, 2);
1560+
expect(dontCallMe).not.toHaveBeenCalled();
1561+
expect(callback).toHaveBeenCalledTimes(2);
1562+
});
1563+
1564+
15351565
it('should throw exception when flushing more requests than pending', function() {
15361566
hb.when('GET').respond(200, '');
15371567
hb('GET', '/url', null, callback);
@@ -1546,8 +1576,9 @@ describe('ngMock', function() {
15461576

15471577
hb.when('GET').respond(200, '');
15481578
hb('GET', '/some', null, callback);
1549-
hb.flush();
1579+
expect(function() {hb.flush(null, 1);}).toThrowError('No pending request to flush !');
15501580

1581+
hb.flush();
15511582
expect(function() {hb.flush();}).toThrowError('No pending request to flush !');
15521583
});
15531584

0 commit comments

Comments
 (0)