From c6ea4d4c5bffa85d4f78280c1aa015e7ac14597c Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Wed, 9 Sep 2020 16:07:50 -0700 Subject: [PATCH] test(router): Add test for canLoad prioritized evaluation This change contains the test from #38780. The fix is already present in master --- packages/router/src/apply_redirects.ts | 13 +++++------ packages/router/test/integration.spec.ts | 29 ++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/router/src/apply_redirects.ts b/packages/router/src/apply_redirects.ts index dcf44e727ac8d..f984a93c07972 100644 --- a/packages/router/src/apply_redirects.ts +++ b/packages/router/src/apply_redirects.ts @@ -7,7 +7,7 @@ */ import {Injector, NgModuleRef} from '@angular/core'; -import {defer, EmptyError, from, Observable, Observer, of} from 'rxjs'; +import {EmptyError, from, Observable, Observer, of} from 'rxjs'; import {catchError, combineAll, concatMap, first, map, mergeMap, tap} from 'rxjs/operators'; import {LoadedRouterConfig, Route, Routes} from './config'; @@ -274,12 +274,11 @@ class ApplyRedirects { segments: UrlSegment[]): Observable { if (route.path === '**') { if (route.loadChildren) { - return defer( - () => this.configLoader.load(ngModule.injector, route) - .pipe(map((cfg: LoadedRouterConfig) => { - route._loadedConfig = cfg; - return new UrlSegmentGroup(segments, {}); - }))); + return this.configLoader.load(ngModule.injector, route) + .pipe(map((cfg: LoadedRouterConfig) => { + route._loadedConfig = cfg; + return new UrlSegmentGroup(segments, {}); + })); } return of(new UrlSegmentGroup(segments, {})); diff --git a/packages/router/test/integration.spec.ts b/packages/router/test/integration.spec.ts index 52a568b6b1574..580360f333439 100644 --- a/packages/router/test/integration.spec.ts +++ b/packages/router/test/integration.spec.ts @@ -4090,7 +4090,7 @@ describe('Integration', () => { return of(delayMs).pipe(delay(delayMs), mapTo(true)); } - @NgModule() + @NgModule({imports: [RouterModule.forChild([{path: '', component: BlankCmp}])]}) class LoadedModule { } @@ -4119,6 +4119,15 @@ describe('Integration', () => { return false; } }, + { + provide: 'returnFalseAndNavigate', + useFactory: (router: Router) => () => { + log.push('returnFalseAndNavigate'); + router.navigateByUrl('/redirected'); + return false; + }, + deps: [Router] + }, { provide: 'returnUrlTree', useFactory: (router: Router) => () => { @@ -4132,7 +4141,23 @@ describe('Integration', () => { }); }); - it('should wait for higher priority guards to be resolved', + it('should only execute canLoad guards of routes being activated', fakeAsync(() => { + const router = TestBed.inject(Router); + + router.resetConfig([ + {path: 'lazy', canLoad: ['guard1'], loadChildren: () => of(LoadedModule)}, + {path: 'redirected', component: SimpleCmp}, + // canLoad should not run for this route because 'lazy' activates first + {path: '', canLoad: ['returnFalseAndNavigate'], loadChildren: () => of(LoadedModule)}, + ]); + + router.navigateByUrl('/lazy'); + tick(5); + expect(log.length).toEqual(1); + expect(log).toEqual(['guard1']); + })); + + it('should execute canLoad guards', fakeAsync(inject( [Router, NgModuleFactoryLoader], (router: Router, loader: SpyNgModuleFactoryLoader) => {