-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat($httpBackend): flush requests in desired order #14967
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1788,24 +1788,29 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) { | |
* @ngdoc method | ||
* @name $httpBackend#flush | ||
* @description | ||
* Flushes all pending requests using the trained responses. | ||
* Flushes pending requests using the trained responses in the order they arrived. | ||
* If there are no pending requests when the flush method is called | ||
* an exception is thrown (as this typically a sign of programming error). | ||
* | ||
* @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). | ||
* @param {number=} count Number of responses to flush. If undefined, | ||
* all pending requests will be flushed beginning at `start`. | ||
* @param {number=} [start=0] Sequential request number at which to begin to flush. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think (Also, have you checked how the resulting docs look like? Can dgeni really understand the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about |
||
*/ | ||
$httpBackend.flush = function(count, digest) { | ||
$httpBackend.flush = function(count, start, digest) { | ||
if (digest !== false) $rootScope.$digest(); | ||
if (!responses.length) throw new Error('No pending request to flush !'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to account for |
||
|
||
start = angular.isDefined(start) && start !== null ? start : 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: |
||
|
||
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(start, 1); | ||
if (!part.length) throw new Error('No more pending request to flush !'); | ||
part[0](); | ||
} | ||
} else { | ||
while (responses.length) { | ||
responses.shift()(); | ||
while (responses.length > start) { | ||
responses.splice(start, 1)[0](); | ||
} | ||
} | ||
$httpBackend.verifyNoOutstandingExpectation(digest); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1532,6 +1532,38 @@ 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).toHaveBeenCalled(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is redundant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #Yes it is. |
||
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).toHaveBeenCalled(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is redundant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you are right. |
||
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); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would mention the
start/skip
parameter in the description.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll do.
(I am not a native speaker so I will be thankful to get any idea about the text.)