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

feat($httpBackend): flush requests in desired order #14967

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1788,24 +1788,30 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
* @ngdoc method
* @name $httpBackend#flush
* @description
* Flushes all pending requests using the trained responses.
*
* @param {number=} count Number of responses to flush (in the order they arrived). If undefined,
* all pending requests will be flushed. If there are no pending requests when the flush method
* is called an exception is thrown (as this typically a sign of programming error).
* Flushes pending requests in the order they arrived beginning at specified request using the trained responses.
* If there are no pending requests to flush when the method is called
* an exception is thrown (as this typically a sign of programming error).
*
* @param {number=} count Number of responses to flush. If undefined,
* all pending requests from `skip` will be flushed.
* @param {number=} [skip=0] Number of pending requests to skip before flushing.
* So it specifies the first request to flush.
*/
$httpBackend.flush = function(count, digest) {
$httpBackend.flush = function(count, skip, digest) {
if (digest !== false) $rootScope.$digest();
if (!responses.length) throw new Error('No pending request to flush !');

skip = skip || 0;
if (skip >= responses.length) throw new Error('No pending request to flush !');

if (angular.isDefined(count) && count !== null) {
while (count--) {
if (!responses.length) throw new Error('No more pending request to flush !');
responses.shift()();
var part = responses.splice(skip, 1);
if (!part.length) throw new Error('No more pending request to flush !');
part[0]();
}
} else {
while (responses.length) {
responses.shift()();
while (responses.length > skip) {
responses.splice(skip, 1)[0]();
}
}
$httpBackend.verifyNoOutstandingExpectation(digest);
Expand Down
6 changes: 3 additions & 3 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2210,7 +2210,7 @@ describe('$http with $applyAsync', function() {
// Ensure requests are sent
$rootScope.$digest();

$httpBackend.flush(null, false);
$httpBackend.flush(null, null, false);
expect($rootScope.$applyAsync).toHaveBeenCalledOnce();
expect(handler).not.toHaveBeenCalled();

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

$httpBackend.flush(null, false);
$httpBackend.flush(null, null, false);
expect(log).toEqual([]);

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

// Finally, third response is received, and a second coalesced $apply is started
$httpBackend.flush(null, false);
$httpBackend.flush(null, null, false);
$browser.defer.flush();
expect(log).toEqual(['response 1', 'response 2', 'response 3']);
});
Expand Down
33 changes: 32 additions & 1 deletion test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,36 @@ describe('ngMock', function() {
});


it('should flush given number of pending requests beginning at specified request', function() {
var dontCallMe = jasmine.createSpy('dontCallMe');

hb.when('GET').respond(200, '');
hb('GET', '/some', null, dontCallMe);
hb('GET', '/some', null, callback);
hb('GET', '/some', null, callback);
hb('GET', '/some', null, dontCallMe);

hb.flush(2, 1);
expect(dontCallMe).not.toHaveBeenCalled();
expect(callback).toHaveBeenCalledTimes(2);
});


it('should flush all pending requests beginning at specified request', function() {
var dontCallMe = jasmine.createSpy('dontCallMe');

hb.when('GET').respond(200, '');
hb('GET', '/some', null, dontCallMe);
hb('GET', '/some', null, dontCallMe);
hb('GET', '/some', null, callback);
hb('GET', '/some', null, callback);

hb.flush(null, 2);
expect(dontCallMe).not.toHaveBeenCalled();
expect(callback).toHaveBeenCalledTimes(2);
});


it('should throw exception when flushing more requests than pending', function() {
hb.when('GET').respond(200, '');
hb('GET', '/url', null, callback);
Expand All @@ -1546,8 +1576,9 @@ describe('ngMock', function() {

hb.when('GET').respond(200, '');
hb('GET', '/some', null, callback);
hb.flush();
expect(function() {hb.flush(null, 1);}).toThrowError('No pending request to flush !');

hb.flush();
expect(function() {hb.flush();}).toThrowError('No pending request to flush !');
});

Expand Down