Skip to content

Commit

Permalink
fix(router): canDeactivate guards should run from bottom to top
Browse files Browse the repository at this point in the history
Closes #15657.
  • Loading branch information
royling authored and alxhub committed Jul 18, 2017
1 parent eb6fb5f commit e20cfe1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,11 +839,10 @@ export class PreActivation {

// reusing the node
if (curr && future._routeConfig === curr._routeConfig) {
if (this.shouldRunGuardsAndResolvers(
curr, future, future._routeConfig !.runGuardsAndResolvers)) {
const shouldRunGuardsAndResolvers = this.shouldRunGuardsAndResolvers(
curr, future, future._routeConfig !.runGuardsAndResolvers);
if (shouldRunGuardsAndResolvers) {
this.canActivateChecks.push(new CanActivate(futurePath));
const outlet = context !.outlet !;
this.canDeactivateChecks.push(new CanDeactivate(outlet.component, curr));
} else {
// we need to set the data
future.data = curr.data;
Expand All @@ -859,6 +858,11 @@ export class PreActivation {
} else {
this.traverseChildRoutes(futureNode, currNode, parentContexts, futurePath);
}

if (shouldRunGuardsAndResolvers) {
const outlet = context !.outlet !;
this.canDeactivateChecks.push(new CanDeactivate(outlet.component, curr));
}
} else {
if (curr) {
this.deactivateRouteAndItsChildren(currNode, context);
Expand Down
30 changes: 30 additions & 0 deletions packages/router/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2435,6 +2435,11 @@ describe('Integration', () => {
provide: 'canDeactivate_team',
useFactory: (logger: Logger) => () => (logger.add('canDeactivate_team'), true),
deps: [Logger]
},
{
provide: 'canDeactivate_simple',
useFactory: (logger: Logger) => () => (logger.add('canDeactivate_simple'), true),
deps: [Logger]
}
]
});
Expand Down Expand Up @@ -2468,6 +2473,31 @@ describe('Integration', () => {
'canDeactivate_team', 'canActivateChild_parent', 'canActivate_team'
]);
})));

it('should call deactivate guards from bottom to top',
fakeAsync(inject(
[Router, Location, Logger], (router: Router, location: Location, logger: Logger) => {
const fixture = createRoot(router, RootCmp);

router.resetConfig([{
path: '',
children: [{
path: 'team/:id',
canDeactivate: ['canDeactivate_team'],
children:
[{path: '', component: SimpleCmp, canDeactivate: ['canDeactivate_simple']}],
component: TeamCmp
}]
}]);

router.navigateByUrl('/team/22');
advance(fixture);

router.navigateByUrl('/team/33');
advance(fixture);

expect(logger.logs).toEqual(['canDeactivate_simple', 'canDeactivate_team']);
})));
});
});

Expand Down

0 comments on commit e20cfe1

Please sign in to comment.