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

feat(ngMock): add flushByIndex function #6412

Closed
wants to merge 1 commit 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
26 changes: 26 additions & 0 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,32 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
$httpBackend.verifyNoOutstandingExpectation();
};

/**
* @ngdoc method
* @name $httpBackend#flushByIndex
* @description
* Flushes any number of pending requests starting at the given index using the trained responses.
*
* @param {number=} index The index of the first response to flush, where 0 corresponds to the
* earliest submitted request. If there is no pending request at the given index when this
* method is called, an exception as thrown.
* @param {number=} count Number of responses to flush (in the order they arrived). If undefined,
* only one pending request 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).
*/
$httpBackend.flushByIndex = function(index, count) {
$rootScope.$digest();
if (!responses.length) throw new Error('No pending request to flush !');
if (!angular.isDefined(index)) throw new Error('Requires an index.');
if (!angular.isDefined(count)) count = 1;

while(count--) {
if (index >= responses.length) throw new Error('No more pending request to flush !');
responses.splice(index, 1)[0]();
}

$httpBackend.verifyNoOutstandingExpectation();
};

/**
* @ngdoc method
Expand Down
46 changes: 46 additions & 0 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,52 @@ describe('ngMock', function() {
});
});

describe('flushByIndex()', function() {
it('should flush given number of pending requests, starting at a given index', function() {
var callback2 = jasmine.createSpy('callback2');
var callback3 = jasmine.createSpy('callback3');

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

hb.flushByIndex(1, 2);
expect(callback).not.toHaveBeenCalled();
expect(callback2).toHaveBeenCalled();
expect(callback3).toHaveBeenCalled();
});


it('should throw exception when flushing more requests than pending', function() {
hb.when('GET').respond(200, '');
hb('GET', '/url', null, callback);

expect(function() {hb.flushByIndex(0, 3);}).toThrow('No more pending request to flush !');
expect(callback).toHaveBeenCalledOnce();
});


it('should throw exception when no request to flush', function() {
expect(function() {hb.flushByIndex(0);}).toThrow('No pending request to flush !');

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

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


it('should throw exception if not all expectations satisfied', function() {
hb.expect('GET', '/url1').respond();
hb.expect('GET', '/url2').respond();

hb('GET', '/url1', null, angular.noop);
expect(function() {hb.flushByIndex(0);}).toThrow('Unsatisfied requests: GET /url2');
});
});


it('should abort requests when timeout promise resolves', function() {
hb.expect('GET', '/url1').respond(200);
Expand Down