Skip to content

Commit

Permalink
test(router): Add test for canLoad prioritized evaluation
Browse files Browse the repository at this point in the history
This change contains the test from angular#38780. The fix is already present in master
  • Loading branch information
atscott committed Sep 9, 2020
1 parent a69507a commit c6ea4d4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
13 changes: 6 additions & 7 deletions packages/router/src/apply_redirects.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -274,12 +274,11 @@ class ApplyRedirects {
segments: UrlSegment[]): Observable<UrlSegmentGroup> {
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, {}));
Expand Down
29 changes: 27 additions & 2 deletions packages/router/test/integration.spec.ts
Expand Up @@ -4090,7 +4090,7 @@ describe('Integration', () => {
return of(delayMs).pipe(delay(delayMs), mapTo(true));
}

@NgModule()
@NgModule({imports: [RouterModule.forChild([{path: '', component: BlankCmp}])]})
class LoadedModule {
}

Expand Down Expand Up @@ -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) => () => {
Expand All @@ -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) => {
Expand Down

0 comments on commit c6ea4d4

Please sign in to comment.