Skip to content

Commit 9254758

Browse files
committed
fix($$testability): Call notifiyWhenNoOutstandingRequests async
Services such as $location and $http will initiate async calls. Thus, we need to wrap the call to $browser.notifyWhenNoOutstandingRequest in $timeout() in order to give any potentially outstanding async calls a chance to run. Fixes angular#13782
1 parent 637f5ae commit 9254758

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/ng/testability.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33

44
function $$TestabilityProvider() {
5-
this.$get = ['$rootScope', '$browser', '$location',
6-
function($rootScope, $browser, $location) {
5+
this.$get = ['$rootScope', '$browser', '$location', '$window',
6+
function($rootScope, $browser, $location, $window) {
77

88
/**
99
* @name $testability
@@ -109,7 +109,9 @@ function $$TestabilityProvider() {
109109
* @param {function} callback
110110
*/
111111
testability.whenStable = function(callback) {
112-
$browser.notifyWhenNoOutstandingRequests(callback);
112+
$window.setTimeout(function() {
113+
$browser.notifyWhenNoOutstandingRequests(callback);
114+
}, 0);
113115
};
114116

115117
return testability;

test/ng/testabilitySpec.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,33 @@ describe('$$testability', function() {
189189

190190
describe('waiting for stability', function() {
191191
it('should process callbacks immediately with no outstanding requests',
192-
inject(function($$testability) {
192+
inject(function($$testability, $timeout) {
193193
var callback = jasmine.createSpy('callback');
194-
$$testability.whenStable(callback);
195-
expect(callback).toHaveBeenCalled();
194+
runs(function() {
195+
$$testability.whenStable(callback);
196+
});
197+
198+
waitsFor(function() {
199+
return callback.calls.length > 0
200+
}, "The callback should be called.", 500);
201+
}));
202+
203+
it('should wait for new $http calls asynchronously',
204+
inject(function($$testability, $httpBackend, $http, $timeout) {
205+
var callback = jasmine.createSpy('callback');
206+
runs(function() {
207+
$httpBackend.when('GET').respond(200);
208+
209+
$http.get('');
210+
$$testability.whenStable(callback);
211+
expect(callback).not.toHaveBeenCalled();
212+
213+
$httpBackend.flush();
214+
});
215+
216+
waitsFor(function() {
217+
return callback.calls.length > 0
218+
}, "The callback should be called.", 500);
196219
}));
197220
});
198221
});

0 commit comments

Comments
 (0)