Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router): fix regression where navigateByUrl promise didn't resolve on CanLoad failure #26455

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ export class Router {
const navCancel =
new NavigationCancel(t.id, this.serializeUrl(t.extractedUrl), e.message);
eventsSubject.next(navCancel);
t.resolve(false);
/* All other errors should reset to the router's internal URL reference to the
* pre-error state. */
} else {
Expand Down
39 changes: 39 additions & 0 deletions packages/router/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2796,6 +2796,45 @@ describe('Integration', () => {
]);
})));

// Regression where navigateByUrl with false CanLoad no longer resolved `false` value on
// navigateByUrl promise: https://github.com/angular/angular/issues/26284
it('should resolve navigateByUrl promise after CanLoad executes',
fakeAsync(inject(
[Router, Location, NgModuleFactoryLoader],
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {

@Component({selector: 'lazy', template: 'lazy-loaded'})
class LazyLoadedComponent {
}

@NgModule({
declarations: [LazyLoadedComponent],
imports:
[RouterModule.forChild([{path: 'loaded', component: LazyLoadedComponent}])]
})
class LazyLoadedModule {
}

loader.stubbedModules = {lazy: LazyLoadedModule};
const fixture = createRoot(router, RootCmp);

router.resetConfig([
{path: 'lazy-false', canLoad: ['alwaysFalse'], loadChildren: 'lazy'},
{path: 'lazy-true', canLoad: ['alwaysTrue'], loadChildren: 'lazy'},
]);

let navFalseResult: any;
let navTrueResult: any;
router.navigateByUrl('/lazy-false').then(v => { navFalseResult = v; });
advance(fixture);
router.navigateByUrl('/lazy-true').then(v => { navTrueResult = v; });
advance(fixture);

expect(navFalseResult).toBe(false);
expect(navTrueResult).toBe(true);

})));

it('should execute CanLoad only once',
fakeAsync(inject(
[Router, Location, NgModuleFactoryLoader],
Expand Down