diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 8d3e5881f621..c167e2c5d70a 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -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); diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index 479ce359bc92..1aef34f2dc5b 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -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(); @@ -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(); @@ -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']); }); diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index a86c2f555003..b7f87570edc1 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -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); @@ -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 !'); });