fix(router): fix regression where navigateByUrl promise didn't resolve on CanLoad failure (#26455)

Fixes #26284

PR Close #26455
This commit is contained in:
Jason Aden 2018-10-15 11:18:52 -07:00 committed by Miško Hevery
parent dd8a85158e
commit 1c9b06504b
2 changed files with 40 additions and 0 deletions

View File

@ -606,6 +606,7 @@ export class Router {
const navCancel =
new NavigationCancel(t.id, this.serializeUrl(t.extractedUrl), e.message);
eventsSubject.next(navCancel);
t.resolve(false);
/* All other errors should reset to the router's internal URL reference to the
* pre-error state. */
} else {

View File

@ -2796,6 +2796,45 @@ describe('Integration', () => {
]);
})));
// Regression where navigateByUrl with false CanLoad no longer resolved `false` value on
// navigateByUrl promise: https://github.com/angular/angular/issues/26284
it('should resolve navigateByUrl promise after CanLoad executes',
fakeAsync(inject(
[Router, Location, NgModuleFactoryLoader],
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {
@Component({selector: 'lazy', template: 'lazy-loaded'})
class LazyLoadedComponent {
}
@NgModule({
declarations: [LazyLoadedComponent],
imports:
[RouterModule.forChild([{path: 'loaded', component: LazyLoadedComponent}])]
})
class LazyLoadedModule {
}
loader.stubbedModules = {lazy: LazyLoadedModule};
const fixture = createRoot(router, RootCmp);
router.resetConfig([
{path: 'lazy-false', canLoad: ['alwaysFalse'], loadChildren: 'lazy'},
{path: 'lazy-true', canLoad: ['alwaysTrue'], loadChildren: 'lazy'},
]);
let navFalseResult: any;
let navTrueResult: any;
router.navigateByUrl('/lazy-false').then(v => { navFalseResult = v; });
advance(fixture);
router.navigateByUrl('/lazy-true').then(v => { navTrueResult = v; });
advance(fixture);
expect(navFalseResult).toBe(false);
expect(navTrueResult).toBe(true);
})));
it('should execute CanLoad only once',
fakeAsync(inject(
[Router, Location, NgModuleFactoryLoader],