Skip to content

Commit

Permalink
fix(router): fix CanActivate redirect to the root on initial load (#1…
Browse files Browse the repository at this point in the history
…3929)

Closes #13530

PR Close #13929
  • Loading branch information
Dzmitry Shylovich authored and mhevery committed Feb 3, 2017
1 parent 09e2d20 commit a047124
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/@angular/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,8 @@ export class Router {
})
.then(
() => {
this.navigated = true;
if (navigationIsSuccessful) {
this.navigated = true;
this.routerEvents.next(new NavigationEnd(
id, this.serializeUrl(url), this.serializeUrl(this.currentUrlTree)));
resolvePromise(true);
Expand Down
29 changes: 29 additions & 0 deletions modules/@angular/router/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,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', () => {
Expand Down

0 comments on commit a047124

Please sign in to comment.