From 0965636735e63e850c39cd098c88f5e2fb401b3d Mon Sep 17 00:00:00 2001 From: Dzmitry Shylovich Date: Wed, 18 Jan 2017 18:07:25 +0300 Subject: [PATCH] fix(router): fix CanActivateChild guard provided in a lazy loaded module (#13989) Closes #12275 PR Close #13989 --- modules/@angular/router/src/router.ts | 2 +- .../@angular/router/test/integration.spec.ts | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/modules/@angular/router/src/router.ts b/modules/@angular/router/src/router.ts index a926b52f6d..7a8109f796 100644 --- a/modules/@angular/router/src/router.ts +++ b/modules/@angular/router/src/router.ts @@ -961,7 +961,7 @@ export class PreActivation { return andObservables(map.call(from(canActivateChildGuards), (d: any) => { const obs = map.call(from(d.guards), (c: any) => { - const guard = this.getToken(c, c.node); + const guard = this.getToken(c, d.node); let observable: Observable; if (guard.canActivateChild) { observable = wrapIntoObservable(guard.canActivateChild(future, this.future)); diff --git a/modules/@angular/router/test/integration.spec.ts b/modules/@angular/router/test/integration.spec.ts index ac14a1e9b9..116842f551 100644 --- a/modules/@angular/router/test/integration.spec.ts +++ b/modules/@angular/router/test/integration.spec.ts @@ -1793,6 +1793,47 @@ describe('Integration', () => { expect(location.path()).toEqual('/team/22'); }))); }); + + it('should find the guard provided in lazy loaded module', + fakeAsync(inject( + [Router, Location, NgModuleFactoryLoader], + (router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => { + + @Component({selector: 'admin', template: ''}) + class AdminComponent { + } + + @Component({selector: 'lazy', template: 'lazy-loaded'}) + class LazyLoadedComponent { + } + + @NgModule({ + declarations: [AdminComponent, LazyLoadedComponent], + imports: [RouterModule.forChild([{ + path: '', + component: AdminComponent, + children: [{ + path: '', + canActivateChild: ['alwaysTrue'], + children: [{path: '', component: LazyLoadedComponent}] + }] + }])], + providers: [{provide: 'alwaysTrue', useValue: () => true}], + }) + class LazyLoadedModule { + } + + loader.stubbedModules = {lazy: LazyLoadedModule}; + const fixture = createRoot(router, RootCmp); + + router.resetConfig([{path: 'admin', loadChildren: 'lazy'}]); + + router.navigateByUrl('/admin'); + advance(fixture); + + expect(location.path()).toEqual('/admin'); + expect(fixture.nativeElement).toHaveText('lazy-loaded'); + }))); }); describe('CanLoad', () => {