From 1c9b06504b45280a30df937b99bc26256732b55e Mon Sep 17 00:00:00 2001 From: Jason Aden Date: Mon, 15 Oct 2018 11:18:52 -0700 Subject: [PATCH] fix(router): fix regression where navigateByUrl promise didn't resolve on CanLoad failure (#26455) Fixes #26284 PR Close #26455 --- packages/router/src/router.ts | 1 + packages/router/test/integration.spec.ts | 39 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index 57febc1227..7f57b7ec83 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -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 { diff --git a/packages/router/test/integration.spec.ts b/packages/router/test/integration.spec.ts index d4bb090b0d..03804df35c 100644 --- a/packages/router/test/integration.spec.ts +++ b/packages/router/test/integration.spec.ts @@ -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],