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

Commit 1b5025e

Browse files
committed
feat($httpBackend): flush requests in desired order
It was impossible to flush pending requests in desired order. Now it is possible because you can specify a `start` number.
1 parent 8ddfa2a commit 1b5025e

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

src/ngMock/angular-mocks.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,24 +1788,29 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
17881788
* @ngdoc method
17891789
* @name $httpBackend#flush
17901790
* @description
1791-
* Flushes all pending requests using the trained responses.
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
1793+
* an exception is thrown (as this typically a sign of programming error).
17921794
*
1793-
* @param {number=} count Number of responses to flush (in the order they arrived). If undefined,
1794-
* all pending requests will be flushed. If there are no pending requests when the flush method
1795-
* is called an exception is thrown (as this typically a sign of programming error).
1795+
* @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.
17961798
*/
1797-
$httpBackend.flush = function(count, digest) {
1799+
$httpBackend.flush = function(count, start, digest) {
17981800
if (digest !== false) $rootScope.$digest();
17991801
if (!responses.length) throw new Error('No pending request to flush !');
18001802

1803+
start = angular.isDefined(start) && start !== null ? start : 0;
1804+
18011805
if (angular.isDefined(count) && count !== null) {
18021806
while (count--) {
1803-
if (!responses.length) throw new Error('No more pending request to flush !');
1804-
responses.shift()();
1807+
var part = responses.splice(start, 1);
1808+
if (!part.length) throw new Error('No more pending request to flush !');
1809+
part[0]();
18051810
}
18061811
} else {
1807-
while (responses.length) {
1808-
responses.shift()();
1812+
while (responses.length > start) {
1813+
responses.splice(start, 1)[0]();
18091814
}
18101815
}
18111816
$httpBackend.verifyNoOutstandingExpectation(digest);

test/ng/httpSpec.js

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

2213-
$httpBackend.flush(null, false);
2213+
$httpBackend.flush(null, null, false);
22142214
expect($rootScope.$applyAsync).toHaveBeenCalledOnce();
22152215
expect(handler).not.toHaveBeenCalled();
22162216

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

2231-
$httpBackend.flush(null, false);
2231+
$httpBackend.flush(null, null, false);
22322232
expect(log).toEqual([]);
22332233

22342234
$browser.defer.flush();
@@ -2253,7 +2253,7 @@ describe('$http with $applyAsync', function() {
22532253
expect(log).toEqual(['response 1', 'response 2']);
22542254

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

test/ngMock/angular-mocksSpec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,38 @@ 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).toHaveBeenCalled();
1547+
expect(callback).toHaveBeenCalledTimes(2);
1548+
});
1549+
1550+
1551+
it('should flush all pending requests beginning at specified request', function() {
1552+
var dontCallMe = jasmine.createSpy('dontCallMe');
1553+
1554+
hb.when('GET').respond(200, '');
1555+
hb('GET', '/some', null, dontCallMe);
1556+
hb('GET', '/some', null, dontCallMe);
1557+
hb('GET', '/some', null, callback);
1558+
hb('GET', '/some', null, callback);
1559+
1560+
hb.flush(null, 2);
1561+
expect(dontCallMe).not.toHaveBeenCalled();
1562+
expect(callback).toHaveBeenCalled();
1563+
expect(callback).toHaveBeenCalledTimes(2);
1564+
});
1565+
1566+
15351567
it('should throw exception when flushing more requests than pending', function() {
15361568
hb.when('GET').respond(200, '');
15371569
hb('GET', '/url', null, callback);

0 commit comments

Comments
 (0)