fix(router): canDeactivate guards are not triggered for componentless routes

Closes #12375
This commit is contained in:
vsavkin
2016-10-20 13:32:38 -07:00
parent 7221632228
commit b74185369f
2 changed files with 45 additions and 33 deletions

View File

@ -1055,7 +1055,11 @@ describe('Integration', () => {
describe('CanDeactivate', () => {
describe('should not deactivate a route when CanDeactivate returns false', () => {
let log: any;
beforeEach(() => {
log = [];
TestBed.configureTestingModule({
providers: [
{
@ -1076,6 +1080,13 @@ describe('Integration', () => {
return a.params['name'] === 'victor';
}
},
{
provide: 'RecordingDeactivate',
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
log.push(['Deactivate', a.routeConfig.path]);
return true;
}
},
]
});
});
@ -1103,27 +1114,37 @@ describe('Integration', () => {
expect(canceledStatus).toEqual(false);
})));
it('works (componentless route)',
it('works with componentless routes',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
const fixture = createRoot(router, RootCmp);
router.resetConfig([{
path: 'parent/:id',
canDeactivate: ['CanDeactivateParent'],
children: [{path: 'simple', component: SimpleCmp}]
}]);
router.resetConfig([
{
path: 'grandparent',
canDeactivate: ['RecordingDeactivate'],
children: [{
path: 'parent',
canDeactivate: ['RecordingDeactivate'],
children: [{
path: 'child',
canDeactivate: ['RecordingDeactivate'],
children: [{path: 'simple', component: SimpleCmp}]
}]
}]
},
{path: 'simple', component: SimpleCmp}
]);
router.navigateByUrl('/parent/22/simple');
router.navigateByUrl('/grandparent/parent/child/simple');
advance(fixture);
expect(location.path()).toEqual('/parent/22/simple');
expect(location.path()).toEqual('/grandparent/parent/child/simple');
router.navigateByUrl('/parent/33/simple');
router.navigateByUrl('/simple');
advance(fixture);
expect(location.path()).toEqual('/parent/33/simple');
router.navigateByUrl('/parent/44/simple');
advance(fixture);
expect(location.path()).toEqual('/parent/33/simple');
expect(log).toEqual([
['Deactivate', 'child'], ['Deactivate', 'parent'], ['Deactivate', 'grandparent']
]);
})));
it('works with a nested route',