fix(router): fix the order of guards, so canActivateChild runs before canActivate

This commit is contained in:
vsavkin 2016-08-25 03:57:39 -07:00 committed by Victor Berchet
parent 2ffecc0e14
commit 0bb516fae2
2 changed files with 61 additions and 1 deletions

View File

@ -572,7 +572,7 @@ export class PreActivation {
.map(s => { .map(s => {
if (s instanceof CanActivate) { if (s instanceof CanActivate) {
return andObservables( return andObservables(
from([this.runCanActivate(s.route), this.runCanActivateChild(s.path)])); from([this.runCanActivateChild(s.path), this.runCanActivate(s.route)]));
} else if (s instanceof CanDeactivate) { } else if (s instanceof CanDeactivate) {
// workaround https://github.com/Microsoft/TypeScript/issues/7271 // workaround https://github.com/Microsoft/TypeScript/issues/7271
const s2 = s as CanDeactivate; const s2 = s as CanDeactivate;

View File

@ -19,6 +19,7 @@ import {ActivatedRoute, ActivatedRouteSnapshot, CanActivate, CanDeactivate, Even
import {RouterTestingModule, SpyNgModuleFactoryLoader} from '../testing'; import {RouterTestingModule, SpyNgModuleFactoryLoader} from '../testing';
describe('Integration', () => { describe('Integration', () => {
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
@ -1271,6 +1272,65 @@ describe('Integration', () => {
}))); })));
}); });
}); });
describe('order', () => {
class Logger {
logs: string[] = [];
add(thing: string) { this.logs.push(thing); }
}
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
Logger, {
provide: 'canActivateChild_parent',
useFactory: (logger: Logger) => () => (logger.add('canActivateChild_parent'), true),
deps: [Logger]
},
{
provide: 'canActivate_team',
useFactory: (logger: Logger) => () => (logger.add('canActivate_team'), true),
deps: [Logger]
},
{
provide: 'canDeactivate_team',
useFactory: (logger: Logger) => () => (logger.add('canDeactivate_team'), true),
deps: [Logger]
}
]
});
});
it('should call guards in the right order',
fakeAsync(inject(
[Router, Location, Logger], (router: Router, location: Location, logger: Logger) => {
const fixture = createRoot(router, RootCmp);
router.resetConfig([{
path: '',
canActivateChild: ['canActivateChild_parent'],
children: [{
path: 'team/:id',
canActivate: ['canActivate_team'],
canDeactivate: ['canDeactivate_team'],
component: TeamCmp
}]
}]);
router.navigateByUrl('/team/22');
advance(fixture);
router.navigateByUrl('/team/33');
advance(fixture);
expect(logger.logs).toEqual([
'canActivateChild_parent', 'canActivate_team',
'canDeactivate_team', 'canActivateChild_parent', 'canActivate_team'
]);
})));
});
}); });
describe('routerActiveLink', () => { describe('routerActiveLink', () => {