Skip to content

Commit b04416d

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 3c86212 commit b04416d

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/ng/testability.js

+7-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,11 @@ function $$TestabilityProvider() {
109109
* @param {function} callback
110110
*/
111111
testability.whenStable = function(callback) {
112-
$browser.notifyWhenNoOutstandingRequests(callback);
112+
// Register the callback asynchronously, so that services that trigger
113+
// async callbacks have a chance to run.
114+
$rootScope.$evalAsync(function() {
115+
$browser.notifyWhenNoOutstandingRequests(callback);
116+
});
113117
};
114118

115119
return testability;

test/ng/testabilitySpec.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,35 @@ 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+
$timeout.flush();
197+
});
198+
199+
waitsFor(function() {
200+
return callback.calls.length > 0;
201+
}, "The callback should be called.", 500);
202+
}));
203+
204+
it('should wait for new $http calls asynchronously',
205+
inject(function($$testability, $httpBackend, $http, $timeout) {
206+
var callback = jasmine.createSpy('callback');
207+
runs(function() {
208+
$httpBackend.when('GET').respond(200);
209+
210+
$http.get('');
211+
$$testability.whenStable(callback);
212+
expect(callback).not.toHaveBeenCalled();
213+
214+
$httpBackend.flush();
215+
$timeout.flush();
216+
});
217+
218+
waitsFor(function() {
219+
return callback.calls.length > 0;
220+
}, "The callback should be called.", 500);
196221
}));
197222
});
198223
});

0 commit comments

Comments
 (0)