fix(router): fix CanActivate redirect to the root on initial load (#13929)

Closes #13530

PR Close #13929
This commit is contained in:
Dzmitry Shylovich 2017-01-14 22:56:08 +03:00 committed by Miško Hevery
parent 029f558d45
commit e075b1ba83
2 changed files with 30 additions and 1 deletions

View File

@ -757,8 +757,8 @@ export class Router {
}) })
.then( .then(
() => { () => {
this.navigated = true;
if (navigationIsSuccessful) { if (navigationIsSuccessful) {
this.navigated = true;
this.routerEvents.next(new NavigationEnd( this.routerEvents.next(new NavigationEnd(
id, this.serializeUrl(url), this.serializeUrl(this.currentUrlTree))); id, this.serializeUrl(url), this.serializeUrl(this.currentUrlTree)));
resolvePromise(true); resolvePromise(true);

View File

@ -1442,6 +1442,35 @@ describe('Integration', () => {
}))); })));
}); });
describe('should redirect to / when guard returns false', () => {
beforeEach(() => TestBed.configureTestingModule({
providers: [{
provide: 'returnFalseAndNavigate',
useFactory: (router: Router) => () => {
router.navigate(['/']);
return false;
},
deps: [Router]
}]
}));
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
router.resetConfig([
{
path: '',
component: SimpleCmp,
},
{path: 'one', component: RouteCmp, canActivate: ['returnFalseAndNavigate']}
]);
const fixture = TestBed.createComponent(RootCmp);
router.navigateByUrl('/one');
advance(fixture);
expect(location.path()).toEqual('/');
expect(fixture.nativeElement).toHaveText('simple');
})));
});
}); });
describe('CanDeactivate', () => { describe('CanDeactivate', () => {