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

Commit 7d94d0a

Browse files
committed
fix(ngRoute): make route.resolve count as a pending request
Protractor users were having a problem where if they had asynchonous code in a route.resolve variable, Protractor was not waiting for that code to complete before continuing. See angular/protractor#789 (comment) for details Also enhanced ngMock to wait for pending requests before calling callbacks from `$browser.notifyWhenNoOutstandingRequests` Potentially breaking change if someone was relying on route.resolve not blocking `$browser.notifyWhenNoOutstandingRequests`
1 parent 0694af8 commit 7d94d0a

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

src/ngMock/angular-mocks.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,26 @@ angular.mock.$Browser = function() {
3737
self.$$lastUrl = self.$$url; // used by url polling fn
3838
self.pollFns = [];
3939

40-
// TODO(vojta): remove this temporary api
41-
self.$$completeOutstandingRequest = angular.noop;
42-
self.$$incOutstandingRequestCount = angular.noop;
43-
40+
// Testability API
41+
42+
var outstandingRequestCount = 0;
43+
var outstandingRequestCallbacks = [];
44+
self.$$incOutstandingRequestCount = function() { outstandingRequestCount++; };
45+
self.$$completeOutstandingRequest = function() {
46+
outstandingRequestCount--;
47+
if (!outstandingRequestCount) {
48+
while (outstandingRequestCallbacks.length) {
49+
outstandingRequestCallbacks.pop()();
50+
}
51+
}
52+
};
53+
self.notifyWhenNoOutstandingRequests = function(callback) {
54+
if (outstandingRequestCount) {
55+
outstandingRequestCallbacks.push(callback);
56+
} else {
57+
callback();
58+
}
59+
};
4460

4561
// register url polling fn
4662

@@ -166,10 +182,6 @@ angular.mock.$Browser.prototype = {
166182

167183
state: function() {
168184
return this.$$state;
169-
},
170-
171-
notifyWhenNoOutstandingRequests: function(fn) {
172-
fn();
173185
}
174186
};
175187

src/ngRoute/route.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ function $RouteProvider() {
350350
'$injector',
351351
'$templateRequest',
352352
'$sce',
353-
function($rootScope, $location, $routeParams, $q, $injector, $templateRequest, $sce) {
353+
'$browser',
354+
function($rootScope, $location, $routeParams, $q, $injector, $templateRequest, $sce, $browser) {
354355

355356
/**
356357
* @ngdoc service
@@ -776,6 +777,7 @@ function $RouteProvider() {
776777

777778
function resolveLocals(route) {
778779
if (route) {
780+
$browser.$$incOutstandingRequestCount();
779781
var locals = angular.extend({}, route.resolve);
780782
angular.forEach(locals, function(value, key) {
781783
locals[key] = angular.isString(value) ?
@@ -786,7 +788,10 @@ function $RouteProvider() {
786788
if (angular.isDefined(template)) {
787789
locals['$template'] = template;
788790
}
789-
return $q.all(locals);
791+
return $q.all(locals).then(function(locals) {
792+
$browser.$$completeOutstandingRequest(function() {});
793+
return locals;
794+
});
790795
}
791796
}
792797

test/ng/testabilitySpec.js

+28
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,33 @@ describe('$$testability', function() {
194194
$$testability.whenStable(callback);
195195
expect(callback).toHaveBeenCalled();
196196
}));
197+
198+
it('should wait for route promises before calling callbacks', function() {
199+
var deferred;
200+
201+
module('ngRoute');
202+
203+
module(function($provide, $routeProvider) {
204+
$routeProvider.when('/path', { template: '', resolve: {
205+
a: function($q) {
206+
deferred = $q.defer();
207+
return deferred.promise;
208+
}
209+
} });
210+
});
211+
212+
inject(function($location, $route, $rootScope, $httpBackend, $$testability) {
213+
$location.path('/path');
214+
$rootScope.$digest();
215+
216+
var callback = jasmine.createSpy('callback');
217+
$$testability.whenStable(callback);
218+
expect(callback).not.toHaveBeenCalled();
219+
220+
deferred.resolve();
221+
$rootScope.$digest();
222+
expect(callback).toHaveBeenCalled();
223+
});
224+
});
197225
});
198226
});

0 commit comments

Comments
 (0)