fix(router): fix CanActivateChild guard provided in a lazy loaded module (#13989)

Closes #12275

PR Close #13989
This commit is contained in:
Dzmitry Shylovich 2017-01-18 18:07:25 +03:00 committed by Miško Hevery
parent 47d41d492b
commit 579567ca79
2 changed files with 42 additions and 1 deletions

View File

@ -961,7 +961,7 @@ export class PreActivation {
return andObservables(map.call(from(canActivateChildGuards), (d: any) => { return andObservables(map.call(from(canActivateChildGuards), (d: any) => {
const obs = map.call(from(d.guards), (c: 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<boolean>; let observable: Observable<boolean>;
if (guard.canActivateChild) { if (guard.canActivateChild) {
observable = wrapIntoObservable(guard.canActivateChild(future, this.future)); observable = wrapIntoObservable(guard.canActivateChild(future, this.future));

View File

@ -1857,6 +1857,47 @@ describe('Integration', () => {
expect(location.path()).toEqual('/team/22'); 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: '<router-outlet></router-outlet>'})
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', () => { describe('CanLoad', () => {