refactor(router): Remove `defer` as it's not needed anymore (#38781)

This change contains the test from #38780 and also removes `defer` from
the `apply_redirects` logic because the change that introduced
`concatMap` instead of `map`...`concatAll` makes `defer` unnecessary.

PR Close #38781
This commit is contained in:
Andrew Scott 2020-09-09 16:07:50 -07:00 committed by Andrew Kushnir
parent 3406ec15a4
commit 281865bbcf
2 changed files with 33 additions and 9 deletions

View File

@ -7,7 +7,7 @@
*/ */
import {Injector, NgModuleRef} from '@angular/core'; 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 {catchError, combineAll, concatMap, first, map, mergeMap, tap} from 'rxjs/operators';
import {LoadedRouterConfig, Route, Routes} from './config'; import {LoadedRouterConfig, Route, Routes} from './config';
@ -274,12 +274,11 @@ class ApplyRedirects {
segments: UrlSegment[]): Observable<UrlSegmentGroup> { segments: UrlSegment[]): Observable<UrlSegmentGroup> {
if (route.path === '**') { if (route.path === '**') {
if (route.loadChildren) { if (route.loadChildren) {
return defer( return this.configLoader.load(ngModule.injector, route)
() => this.configLoader.load(ngModule.injector, route) .pipe(map((cfg: LoadedRouterConfig) => {
.pipe(map((cfg: LoadedRouterConfig) => { route._loadedConfig = cfg;
route._loadedConfig = cfg; return new UrlSegmentGroup(segments, {});
return new UrlSegmentGroup(segments, {}); }));
})));
} }
return of(new UrlSegmentGroup(segments, {})); return of(new UrlSegmentGroup(segments, {}));

View File

@ -4090,7 +4090,7 @@ describe('Integration', () => {
return of(delayMs).pipe(delay(delayMs), mapTo(true)); return of(delayMs).pipe(delay(delayMs), mapTo(true));
} }
@NgModule() @NgModule({imports: [RouterModule.forChild([{path: '', component: BlankCmp}])]})
class LoadedModule { class LoadedModule {
} }
@ -4119,6 +4119,15 @@ describe('Integration', () => {
return false; return false;
} }
}, },
{
provide: 'returnFalseAndNavigate',
useFactory: (router: Router) => () => {
log.push('returnFalseAndNavigate');
router.navigateByUrl('/redirected');
return false;
},
deps: [Router]
},
{ {
provide: 'returnUrlTree', provide: 'returnUrlTree',
useFactory: (router: Router) => () => { 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( fakeAsync(inject(
[Router, NgModuleFactoryLoader], [Router, NgModuleFactoryLoader],
(router: Router, loader: SpyNgModuleFactoryLoader) => { (router: Router, loader: SpyNgModuleFactoryLoader) => {