This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix(ngRoute): make route.resolve count as a pending request #14159
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html ng-app="lettersApp"> | ||
<body> | ||
<div ng-view></div> | ||
|
||
<script src="angular.js"></script> | ||
<script src="angular-route.js"></script> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
angular. | ||
module('lettersApp', ['ngRoute']). | ||
config(function($routeProvider) { | ||
$routeProvider. | ||
otherwise(resolveRedirectTo('/foo1')). | ||
when('/foo1', resolveRedirectTo('/bar1')). | ||
when('/bar1', resolveRedirectTo('/baz1')). | ||
when('/baz1', resolveRedirectTo('/qux1')). | ||
when('/qux1', { | ||
template: '<ul><li ng-repeat="letter in $resolve.letters">{{ letter }}</li></ul>', | ||
resolve: resolveLetters() | ||
}). | ||
when('/foo2', resolveRedirectTo('/bar2')). | ||
when('/bar2', resolveRedirectTo('/baz2')). | ||
when('/baz2', resolveRedirectTo('/qux2')). | ||
when('/qux2', { | ||
template: '{{ $resolve.letters.length }}', | ||
resolve: resolveLetters() | ||
}); | ||
|
||
// Helpers | ||
function resolveLetters() { | ||
return { | ||
letters: function($q) { | ||
return $q(function(resolve) { | ||
window.setTimeout(resolve, 1000, ['a', 'b', 'c', 'd', 'e']); | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
function resolveRedirectTo(path) { | ||
return { | ||
resolveRedirectTo: function($q) { | ||
return $q(function(resolve) { | ||
window.setTimeout(resolve, 250, path); | ||
}); | ||
} | ||
}; | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
describe('ngRoute promises', function() { | ||
beforeEach(function() { | ||
loadFixture('ng-route-promise'); | ||
}); | ||
|
||
it('should wait for route promises', function() { | ||
expect(element.all(by.tagName('li')).count()).toBe(5); | ||
}); | ||
|
||
it('should time out if the promise takes long enough', function() { | ||
// Don't try this at home kids, I'm a protractor dev | ||
browser.manage().timeouts().setScriptTimeout(1500); | ||
browser.waitForAngular().then(function() { | ||
fail('waitForAngular() should have timed out, but didn\'t'); | ||
}, function(error) { | ||
expect(error.message).toContain('Timed out waiting for asynchronous Angular tasks to finish'); | ||
}); | ||
}); | ||
|
||
it('should wait for route promises when navigating to another route', function() { | ||
browser.setLocation('/foo2'); | ||
expect(element(by.tagName('body')).getText()).toBe('5'); | ||
}); | ||
|
||
afterEach(function(done) { | ||
// Restore old timeout limit | ||
browser.getProcessedConfig().then(function(config) { | ||
return browser.manage().timeouts().setScriptTimeout(config.allScriptsTimeout); | ||
}).then(done); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2082,4 +2082,188 @@ describe('$route', function() { | |
expect(function() { $route.updateParams(); }).toThrowMinErr('ngRoute', 'norout'); | ||
})); | ||
}); | ||
|
||
describe('testability', function() { | ||
it('should wait for $resolve promises before calling callbacks', function() { | ||
var deferred; | ||
|
||
module(function($provide, $routeProvider) { | ||
$routeProvider.when('/path', { | ||
template: '', | ||
resolve: { | ||
a: function($q) { | ||
deferred = $q.defer(); | ||
return deferred.promise; | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
inject(function($location, $route, $rootScope, $httpBackend, $$testability) { | ||
$location.path('/path'); | ||
$rootScope.$digest(); | ||
|
||
var callback = jasmine.createSpy('callback'); | ||
$$testability.whenStable(callback); | ||
expect(callback).not.toHaveBeenCalled(); | ||
|
||
deferred.resolve(); | ||
$rootScope.$digest(); | ||
expect(callback).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should call callback after $resolve promises are rejected', function() { | ||
var deferred; | ||
|
||
module(function($provide, $routeProvider) { | ||
$routeProvider.when('/path', { | ||
template: '', | ||
resolve: { | ||
a: function($q) { | ||
deferred = $q.defer(); | ||
return deferred.promise; | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
inject(function($location, $route, $rootScope, $httpBackend, $$testability) { | ||
$location.path('/path'); | ||
$rootScope.$digest(); | ||
|
||
var callback = jasmine.createSpy('callback'); | ||
$$testability.whenStable(callback); | ||
expect(callback).not.toHaveBeenCalled(); | ||
|
||
deferred.reject(); | ||
$rootScope.$digest(); | ||
expect(callback).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should wait for resolveRedirectTo promises before calling callbacks', function() { | ||
var deferred; | ||
|
||
module(function($provide, $routeProvider) { | ||
$routeProvider.when('/path', { | ||
resolveRedirectTo: function($q) { | ||
deferred = $q.defer(); | ||
return deferred.promise; | ||
} | ||
}); | ||
}); | ||
|
||
inject(function($location, $route, $rootScope, $httpBackend, $$testability) { | ||
$location.path('/path'); | ||
$rootScope.$digest(); | ||
|
||
var callback = jasmine.createSpy('callback'); | ||
$$testability.whenStable(callback); | ||
expect(callback).not.toHaveBeenCalled(); | ||
|
||
deferred.resolve(); | ||
$rootScope.$digest(); | ||
expect(callback).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should call callback after resolveRedirectTo promises are rejected', function() { | ||
var deferred; | ||
|
||
module(function($provide, $routeProvider) { | ||
$routeProvider.when('/path', { | ||
resolveRedirectTo: function($q) { | ||
deferred = $q.defer(); | ||
return deferred.promise; | ||
} | ||
}); | ||
}); | ||
|
||
inject(function($location, $route, $rootScope, $httpBackend, $$testability) { | ||
$location.path('/path'); | ||
$rootScope.$digest(); | ||
|
||
var callback = jasmine.createSpy('callback'); | ||
$$testability.whenStable(callback); | ||
expect(callback).not.toHaveBeenCalled(); | ||
|
||
deferred.reject(); | ||
$rootScope.$digest(); | ||
expect(callback).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should wait for all route promises before calling callbacks', function() { | ||
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. @gkalpak This is the unit test which I wrote to test for the race condition. This unit tests fails, but this doesn't appear to be a problem with real browsers. I did essentially the same thing in a real browser, and it correctly called the callback only after both promises were resolved. I'll look into the error in the unit testing setup after lunch |
||
var deferreds = {}; | ||
|
||
module(function($provide, $routeProvider) { | ||
// While normally `$browser.defer()` modifies the `outstandingRequestCount`, the mocked | ||
// version (provided by `ngMock`) does not. This doesn't matter in most tests, but in this | ||
// case we need the `outstandingRequestCount` logic to ensure that we don't call the | ||
// `$$testability.whenStable()` callbacks part way through a `$rootScope.$evalAsync` block. | ||
// See ngRoute's commitRoute()'s finally() block for details. | ||
$provide.decorator('$browser', function($delegate) { | ||
var oldDefer = $delegate.defer; | ||
var newDefer = function(fn, delay) { | ||
var requestCountAwareFn = function() { $delegate.$$completeOutstandingRequest(fn); }; | ||
$delegate.$$incOutstandingRequestCount(); | ||
return oldDefer.call($delegate, requestCountAwareFn, delay); | ||
}; | ||
|
||
$delegate.defer = angular.extend(newDefer, oldDefer); | ||
|
||
return $delegate; | ||
}); | ||
|
||
addRouteWithAsyncRedirect('/foo', '/bar'); | ||
addRouteWithAsyncRedirect('/bar', '/baz'); | ||
addRouteWithAsyncRedirect('/baz', '/qux'); | ||
$routeProvider.when('/qux', { | ||
template: '', | ||
resolve: { | ||
a: function($q) { | ||
var deferred = deferreds['/qux'] = $q.defer(); | ||
return deferred.promise; | ||
} | ||
} | ||
}); | ||
|
||
// Helpers | ||
function addRouteWithAsyncRedirect(fromPath, toPath) { | ||
$routeProvider.when(fromPath, { | ||
resolveRedirectTo: function($q) { | ||
var deferred = deferreds[fromPath] = $q.defer(); | ||
return deferred.promise.then(function() { return toPath; }); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
inject(function($browser, $location, $rootScope, $route, $$testability) { | ||
$location.path('/foo'); | ||
$rootScope.$digest(); | ||
|
||
var callback = jasmine.createSpy('callback'); | ||
$$testability.whenStable(callback); | ||
expect(callback).not.toHaveBeenCalled(); | ||
|
||
deferreds['/foo'].resolve(); | ||
$browser.defer.flush(); | ||
expect(callback).not.toHaveBeenCalled(); | ||
|
||
deferreds['/bar'].resolve(); | ||
$browser.defer.flush(); | ||
expect(callback).not.toHaveBeenCalled(); | ||
|
||
deferreds['/baz'].resolve(); | ||
$browser.defer.flush(); | ||
expect(callback).not.toHaveBeenCalled(); | ||
|
||
deferreds['/qux'].resolve(); | ||
$browser.defer.flush(); | ||
expect(callback).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
😆