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

Commit acb066e

Browse files
pkozlowski-opensourcelgalfaso
authored andcommitted
fix(ngMock): allow numeric timeouts in $httpBackend mock
Fixes #4891
1 parent ed1243f commit acb066e

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/ngMock/angular-mocks.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ angular.mock.dump = function(object) {
11121112
```
11131113
*/
11141114
angular.mock.$HttpBackendProvider = function() {
1115-
this.$get = ['$rootScope', createHttpBackendMock];
1115+
this.$get = ['$rootScope', '$timeout', createHttpBackendMock];
11161116
};
11171117

11181118
/**
@@ -1129,7 +1129,7 @@ angular.mock.$HttpBackendProvider = function() {
11291129
* @param {Object=} $browser Auto-flushing enabled if specified
11301130
* @return {Object} Instance of $httpBackend mock
11311131
*/
1132-
function createHttpBackendMock($rootScope, $delegate, $browser) {
1132+
function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
11331133
var definitions = [],
11341134
expectations = [],
11351135
responses = [],
@@ -1159,7 +1159,9 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
11591159
}
11601160

11611161
function wrapResponse(wrapped) {
1162-
if (!$browser && timeout && timeout.then) timeout.then(handleTimeout);
1162+
if (!$browser && timeout) {
1163+
timeout.then ? timeout.then(handleTimeout) : $timeout(handleTimeout, timeout);
1164+
}
11631165

11641166
return handleResponse;
11651167

@@ -2033,7 +2035,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
20332035
*/
20342036
angular.mock.e2e = {};
20352037
angular.mock.e2e.$httpBackendDecorator =
2036-
['$rootScope', '$delegate', '$browser', createHttpBackendMock];
2038+
['$rootScope', '$timeout', '$delegate', '$browser', createHttpBackendMock];
20372039

20382040

20392041
/**

test/ngMock/angular-mocksSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,18 @@ describe('ngMock', function() {
13101310
});
13111311

13121312

1313+
it('should abort requests when timeout passed as a numeric value', inject(function($timeout) {
1314+
hb.expect('GET', '/url1').respond(200);
1315+
1316+
hb('GET', '/url1', null, callback, null, 200);
1317+
$timeout.flush(300);
1318+
1319+
expect(callback).toHaveBeenCalledWith(-1, undefined, '');
1320+
hb.verifyNoOutstandingExpectation();
1321+
hb.verifyNoOutstandingRequest();
1322+
}));
1323+
1324+
13131325
it('should throw an exception if no response defined', function() {
13141326
hb.when('GET', '/test');
13151327
expect(function() {

0 commit comments

Comments
 (0)