2016-07-21 20:12:00 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-11-22 17:50:52 -05:00
|
|
|
import {Location} from '@angular/common';
|
2016-12-01 18:46:22 -05:00
|
|
|
import {TestBed, inject} from '@angular/core/testing';
|
2016-09-21 14:37:43 -04:00
|
|
|
|
2016-10-25 17:33:18 -04:00
|
|
|
import {ResolveData} from '../src/config';
|
2017-07-25 14:13:15 -04:00
|
|
|
import {PreActivation} from '../src/pre_activation';
|
|
|
|
import {Router} from '../src/router';
|
2017-05-17 20:47:34 -04:00
|
|
|
import {ChildrenOutletContexts} from '../src/router_outlet_context';
|
2016-10-25 17:33:18 -04:00
|
|
|
import {ActivatedRouteSnapshot, RouterStateSnapshot, createEmptyStateSnapshot} from '../src/router_state';
|
2016-08-10 18:53:57 -04:00
|
|
|
import {DefaultUrlSerializer} from '../src/url_tree';
|
|
|
|
import {TreeNode} from '../src/utils/tree';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {RouterTestingModule} from '../testing/src/router_testing_module';
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2017-07-25 14:13:15 -04:00
|
|
|
import {Logger, createActivatedRouteSnapshot, provideTokenLogger} from './helpers';
|
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
describe('Router', () => {
|
2016-09-21 14:37:43 -04:00
|
|
|
describe('resetRootComponentType', () => {
|
|
|
|
class NewRootComponent {}
|
|
|
|
|
|
|
|
beforeEach(() => { TestBed.configureTestingModule({imports: [RouterTestingModule]}); });
|
|
|
|
|
|
|
|
it('should not change root route when updating the root component', () => {
|
|
|
|
const r: Router = TestBed.get(Router);
|
|
|
|
const root = r.routerState.root;
|
|
|
|
|
|
|
|
r.resetRootComponentType(NewRootComponent);
|
|
|
|
|
|
|
|
expect(r.routerState.root).toBe(root);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-22 17:50:52 -05:00
|
|
|
describe('setUpLocationChangeListener', () => {
|
|
|
|
beforeEach(() => { TestBed.configureTestingModule({imports: [RouterTestingModule]}); });
|
|
|
|
|
2017-07-20 12:32:34 -04:00
|
|
|
it('should be idempotent', inject([Router, Location], (r: Router, location: Location) => {
|
2016-12-01 18:46:22 -05:00
|
|
|
r.setUpLocationChangeListener();
|
|
|
|
const a = (<any>r).locationSubscription;
|
|
|
|
r.setUpLocationChangeListener();
|
|
|
|
const b = (<any>r).locationSubscription;
|
2016-11-22 17:50:52 -05:00
|
|
|
|
2016-12-01 18:46:22 -05:00
|
|
|
expect(a).toBe(b);
|
2016-11-22 17:50:52 -05:00
|
|
|
|
2016-12-01 18:46:22 -05:00
|
|
|
r.dispose();
|
|
|
|
r.setUpLocationChangeListener();
|
|
|
|
const c = (<any>r).locationSubscription;
|
2016-11-22 17:50:52 -05:00
|
|
|
|
2016-12-01 18:46:22 -05:00
|
|
|
expect(c).not.toBe(b);
|
|
|
|
}));
|
2016-11-22 17:50:52 -05:00
|
|
|
});
|
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
describe('PreActivation', () => {
|
|
|
|
const serializer = new DefaultUrlSerializer();
|
|
|
|
const inj = {get: (token: any) => () => `${token}_value`};
|
|
|
|
let empty: RouterStateSnapshot;
|
2017-07-25 14:13:15 -04:00
|
|
|
let logger: Logger;
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2017-07-25 14:13:15 -04:00
|
|
|
const CA_CHILD = 'canActivate_child';
|
|
|
|
const CA_CHILD_FALSE = 'canActivate_child_false';
|
|
|
|
const CAC_CHILD = 'canActivateChild_child';
|
|
|
|
const CAC_CHILD_FALSE = 'canActivateChild_child_false';
|
|
|
|
const CA_GRANDCHILD = 'canActivate_grandchild';
|
|
|
|
const CA_GRANDCHILD_FALSE = 'canActivate_grandchild_false';
|
|
|
|
const CDA_CHILD = 'canDeactivate_child';
|
|
|
|
const CDA_CHILD_FALSE = 'canDeactivate_child_false';
|
|
|
|
const CDA_GRANDCHILD = 'canDeactivate_grandchild';
|
|
|
|
const CDA_GRANDCHILD_FALSE = 'canDeactivate_grandchild_false';
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2017-07-25 14:13:15 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [
|
|
|
|
Logger, provideTokenLogger(CA_CHILD), provideTokenLogger(CA_CHILD_FALSE, false),
|
|
|
|
provideTokenLogger(CAC_CHILD), provideTokenLogger(CAC_CHILD_FALSE, false),
|
|
|
|
provideTokenLogger(CA_GRANDCHILD), provideTokenLogger(CA_GRANDCHILD_FALSE, false),
|
|
|
|
provideTokenLogger(CDA_CHILD), provideTokenLogger(CDA_CHILD_FALSE, false),
|
|
|
|
provideTokenLogger(CDA_GRANDCHILD), provideTokenLogger(CDA_GRANDCHILD_FALSE, false)
|
|
|
|
]
|
2016-07-13 18:25:48 -04:00
|
|
|
});
|
2017-07-25 14:13:15 -04:00
|
|
|
|
2016-06-01 17:32:15 -04:00
|
|
|
});
|
2016-06-02 17:44:57 -04:00
|
|
|
|
2017-07-25 14:13:15 -04:00
|
|
|
beforeEach(inject([Logger], (_logger: Logger) => {
|
|
|
|
empty = createEmptyStateSnapshot(serializer.parse('/'), null !);
|
|
|
|
logger = _logger;
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('guards', () => {
|
|
|
|
it('should run CanActivate checks', () => {
|
|
|
|
/**
|
|
|
|
* R --> R
|
|
|
|
* \
|
|
|
|
* child (CA, CAC)
|
|
|
|
* \
|
|
|
|
* grandchild (CA)
|
|
|
|
*/
|
|
|
|
|
|
|
|
const childSnapshot = createActivatedRouteSnapshot({
|
|
|
|
component: 'child',
|
|
|
|
routeConfig: {
|
|
|
|
|
|
|
|
canActivate: [CA_CHILD],
|
|
|
|
canActivateChild: [CAC_CHILD]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const grandchildSnapshot = createActivatedRouteSnapshot(
|
|
|
|
{component: 'grandchild', routeConfig: {canActivate: [CA_GRANDCHILD]}});
|
|
|
|
|
|
|
|
const futureState = new RouterStateSnapshot(
|
|
|
|
'url',
|
|
|
|
new TreeNode(
|
|
|
|
empty.root, [new TreeNode(childSnapshot, [new TreeNode(grandchildSnapshot, [])])]));
|
|
|
|
|
|
|
|
checkGuards(futureState, empty, TestBed, (result) => {
|
|
|
|
expect(result).toBe(true);
|
|
|
|
expect(logger.logs).toEqual([CA_CHILD, CAC_CHILD, CA_GRANDCHILD]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not run grandchild guards if child fails', () => {
|
|
|
|
/**
|
|
|
|
* R --> R
|
|
|
|
* \
|
|
|
|
* child (CA: x, CAC)
|
|
|
|
* \
|
|
|
|
* grandchild (CA)
|
|
|
|
*/
|
|
|
|
|
|
|
|
const childSnapshot = createActivatedRouteSnapshot({
|
|
|
|
component: 'child',
|
|
|
|
routeConfig: {canActivate: [CA_CHILD_FALSE], canActivateChild: [CAC_CHILD]}
|
|
|
|
});
|
|
|
|
const grandchildSnapshot = createActivatedRouteSnapshot(
|
|
|
|
{component: 'grandchild', routeConfig: {canActivate: [CA_GRANDCHILD]}});
|
|
|
|
|
|
|
|
const futureState = new RouterStateSnapshot(
|
|
|
|
'url',
|
|
|
|
new TreeNode(
|
|
|
|
empty.root, [new TreeNode(childSnapshot, [new TreeNode(grandchildSnapshot, [])])]));
|
|
|
|
|
|
|
|
checkGuards(futureState, empty, TestBed, (result) => {
|
|
|
|
expect(result).toBe(false);
|
|
|
|
expect(logger.logs).toEqual([CA_CHILD_FALSE]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not run grandchild guards if child canActivateChild fails', () => {
|
|
|
|
/**
|
|
|
|
* R --> R
|
|
|
|
* \
|
|
|
|
* child (CA, CAC: x)
|
|
|
|
* \
|
|
|
|
* grandchild (CA)
|
|
|
|
*/
|
|
|
|
|
|
|
|
const childSnapshot = createActivatedRouteSnapshot({
|
|
|
|
component: 'child',
|
|
|
|
routeConfig: {canActivate: [CA_CHILD], canActivateChild: [CAC_CHILD_FALSE]}
|
|
|
|
});
|
|
|
|
const grandchildSnapshot = createActivatedRouteSnapshot(
|
|
|
|
{component: 'grandchild', routeConfig: {canActivate: [CA_GRANDCHILD]}});
|
|
|
|
|
|
|
|
const futureState = new RouterStateSnapshot(
|
|
|
|
'url',
|
|
|
|
new TreeNode(
|
|
|
|
empty.root, [new TreeNode(childSnapshot, [new TreeNode(grandchildSnapshot, [])])]));
|
|
|
|
|
|
|
|
checkGuards(futureState, empty, TestBed, (result) => {
|
|
|
|
expect(result).toBe(false);
|
|
|
|
expect(logger.logs).toEqual([CA_CHILD, CAC_CHILD_FALSE]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should run deactivate guards before activate guards', () => {
|
|
|
|
/**
|
|
|
|
* R --> R
|
|
|
|
* / \
|
|
|
|
* prev (CDA) child (CA)
|
|
|
|
* \
|
|
|
|
* grandchild (CA)
|
|
|
|
*/
|
|
|
|
|
|
|
|
const prevSnapshot = createActivatedRouteSnapshot(
|
|
|
|
{component: 'prev', routeConfig: {canDeactivate: [CDA_CHILD]}});
|
|
|
|
|
|
|
|
const childSnapshot = createActivatedRouteSnapshot({
|
|
|
|
component: 'child',
|
|
|
|
routeConfig: {canActivate: [CA_CHILD], canActivateChild: [CAC_CHILD]}
|
|
|
|
});
|
|
|
|
|
|
|
|
const grandchildSnapshot = createActivatedRouteSnapshot(
|
|
|
|
{component: 'grandchild', routeConfig: {canActivate: [CA_GRANDCHILD]}});
|
2016-10-05 17:43:26 -04:00
|
|
|
|
2017-07-25 14:13:15 -04:00
|
|
|
const currentState = new RouterStateSnapshot(
|
|
|
|
'prev', new TreeNode(empty.root, [new TreeNode(prevSnapshot, [])]));
|
2016-10-05 17:43:26 -04:00
|
|
|
|
2017-07-25 14:13:15 -04:00
|
|
|
const futureState = new RouterStateSnapshot(
|
|
|
|
'url',
|
|
|
|
new TreeNode(
|
|
|
|
empty.root, [new TreeNode(childSnapshot, [new TreeNode(grandchildSnapshot, [])])]));
|
2016-10-05 17:43:26 -04:00
|
|
|
|
2017-07-25 14:13:15 -04:00
|
|
|
checkGuards(futureState, currentState, TestBed, (result) => {
|
|
|
|
expect(logger.logs).toEqual([CDA_CHILD, CA_CHILD, CAC_CHILD, CA_GRANDCHILD]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not run activate if deactivate fails guards', () => {
|
|
|
|
/**
|
|
|
|
* R --> R
|
|
|
|
* / \
|
|
|
|
* prev (CDA) child (CA)
|
|
|
|
* \
|
|
|
|
* grandchild (CA)
|
|
|
|
*/
|
|
|
|
|
|
|
|
const prevSnapshot = createActivatedRouteSnapshot(
|
|
|
|
{component: 'prev', routeConfig: {canDeactivate: [CDA_CHILD_FALSE]}});
|
|
|
|
const childSnapshot = createActivatedRouteSnapshot({
|
|
|
|
component: 'child',
|
|
|
|
routeConfig: {canActivate: [CA_CHILD], canActivateChild: [CAC_CHILD]}
|
|
|
|
});
|
|
|
|
const grandchildSnapshot = createActivatedRouteSnapshot(
|
|
|
|
{component: 'grandchild', routeConfig: {canActivate: [CA_GRANDCHILD]}});
|
|
|
|
|
|
|
|
const currentState = new RouterStateSnapshot(
|
|
|
|
'prev', new TreeNode(empty.root, [new TreeNode(prevSnapshot, [])]));
|
|
|
|
const futureState = new RouterStateSnapshot(
|
|
|
|
'url',
|
|
|
|
new TreeNode(
|
|
|
|
empty.root, [new TreeNode(childSnapshot, [new TreeNode(grandchildSnapshot, [])])]));
|
|
|
|
|
|
|
|
checkGuards(futureState, currentState, TestBed, (result) => {
|
|
|
|
expect(result).toBe(false);
|
|
|
|
expect(logger.logs).toEqual([CDA_CHILD_FALSE]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should deactivate from bottom up, then activate top down', () => {
|
|
|
|
/**
|
|
|
|
* R --> R
|
|
|
|
* / \
|
|
|
|
* prevChild (CDA) child (CA)
|
|
|
|
* / \
|
|
|
|
* prevGrandchild(CDA) grandchild (CA)
|
|
|
|
*/
|
2016-10-05 17:43:26 -04:00
|
|
|
|
2017-07-25 14:13:15 -04:00
|
|
|
const prevChildSnapshot = createActivatedRouteSnapshot(
|
|
|
|
{component: 'prev_child', routeConfig: {canDeactivate: [CDA_CHILD]}});
|
|
|
|
const prevGrandchildSnapshot = createActivatedRouteSnapshot(
|
|
|
|
{component: 'prev_grandchild', routeConfig: {canDeactivate: [CDA_GRANDCHILD]}});
|
|
|
|
const childSnapshot = createActivatedRouteSnapshot({
|
|
|
|
component: 'child',
|
|
|
|
routeConfig: {canActivate: [CA_CHILD], canActivateChild: [CAC_CHILD]}
|
|
|
|
});
|
|
|
|
const grandchildSnapshot = createActivatedRouteSnapshot(
|
|
|
|
{component: 'grandchild', routeConfig: {canActivate: [CA_GRANDCHILD]}});
|
|
|
|
|
|
|
|
const currentState = new RouterStateSnapshot(
|
|
|
|
'prev', new TreeNode(empty.root, [
|
|
|
|
new TreeNode(prevChildSnapshot, [new TreeNode(prevGrandchildSnapshot, [])])
|
|
|
|
]));
|
|
|
|
|
|
|
|
const futureState = new RouterStateSnapshot(
|
|
|
|
'url',
|
|
|
|
new TreeNode(
|
|
|
|
empty.root, [new TreeNode(childSnapshot, [new TreeNode(grandchildSnapshot, [])])]));
|
|
|
|
|
|
|
|
checkGuards(futureState, currentState, TestBed, (result) => {
|
|
|
|
expect(result).toBe(true);
|
|
|
|
expect(logger.logs).toEqual([
|
|
|
|
CDA_GRANDCHILD, CDA_CHILD, CA_CHILD, CAC_CHILD, CA_GRANDCHILD
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
logger.empty();
|
|
|
|
checkGuards(currentState, futureState, TestBed, (result) => {
|
|
|
|
expect(result).toBe(true);
|
|
|
|
expect(logger.logs).toEqual([]);
|
|
|
|
});
|
2016-10-05 17:43:26 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-07-25 14:13:15 -04:00
|
|
|
describe('resolve', () => {
|
|
|
|
|
|
|
|
it('should resolve data', () => {
|
|
|
|
/**
|
|
|
|
* R --> R
|
|
|
|
* \
|
|
|
|
* a
|
|
|
|
*/
|
|
|
|
const r = {data: 'resolver'};
|
|
|
|
const n = createActivatedRouteSnapshot({component: 'a', resolve: r});
|
|
|
|
const s = new RouterStateSnapshot('url', new TreeNode(empty.root, [new TreeNode(n, [])]));
|
|
|
|
|
|
|
|
checkResolveData(s, empty, inj, () => {
|
|
|
|
expect(s.root.firstChild !.data).toEqual({data: 'resolver_value'});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should wait for the parent resolve to complete', () => {
|
|
|
|
/**
|
|
|
|
* R --> R
|
|
|
|
* \
|
|
|
|
* null (resolve: parentResolve)
|
|
|
|
* \
|
|
|
|
* b (resolve: childResolve)
|
|
|
|
*/
|
|
|
|
const parentResolve = {data: 'resolver'};
|
|
|
|
const childResolve = {};
|
|
|
|
|
|
|
|
const parent = createActivatedRouteSnapshot({component: null !, resolve: parentResolve});
|
|
|
|
const child = createActivatedRouteSnapshot({component: 'b', resolve: childResolve});
|
|
|
|
|
|
|
|
const s = new RouterStateSnapshot(
|
|
|
|
'url', new TreeNode(empty.root, [new TreeNode(parent, [new TreeNode(child, [])])]));
|
|
|
|
|
|
|
|
const inj = {get: (token: any) => () => Promise.resolve(`${token}_value`)};
|
|
|
|
|
|
|
|
checkResolveData(s, empty, inj, () => {
|
|
|
|
expect(s.root.firstChild !.firstChild !.data).toEqual({data: 'resolver_value'});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should copy over data when creating a snapshot', () => {
|
|
|
|
/**
|
|
|
|
* R --> R --> R
|
|
|
|
* \ \
|
|
|
|
* n1 (resolve: r1) n21 (resolve: r1)
|
|
|
|
* \
|
|
|
|
* n22 (resolve: r2)
|
|
|
|
*/
|
|
|
|
const r1 = {data: 'resolver1'};
|
|
|
|
const r2 = {data: 'resolver2'};
|
|
|
|
|
|
|
|
const n1 = createActivatedRouteSnapshot({component: 'a', resolve: r1});
|
|
|
|
const s1 = new RouterStateSnapshot('url', new TreeNode(empty.root, [new TreeNode(n1, [])]));
|
|
|
|
checkResolveData(s1, empty, inj, () => {});
|
|
|
|
|
|
|
|
const n21 = createActivatedRouteSnapshot({component: 'a', resolve: r1});
|
|
|
|
const n22 = createActivatedRouteSnapshot({component: 'b', resolve: r2});
|
|
|
|
const s2 = new RouterStateSnapshot(
|
|
|
|
'url', new TreeNode(empty.root, [new TreeNode(n21, [new TreeNode(n22, [])])]));
|
|
|
|
checkResolveData(s2, s1, inj, () => {
|
|
|
|
expect(s2.root.firstChild !.data).toEqual({data: 'resolver1_value'});
|
|
|
|
expect(s2.root.firstChild !.firstChild !.data).toEqual({data: 'resolver2_value'});
|
|
|
|
});
|
2016-07-26 17:39:02 -04:00
|
|
|
});
|
2016-06-06 13:55:12 -04:00
|
|
|
});
|
2016-06-01 17:32:15 -04:00
|
|
|
});
|
2016-05-20 16:22:57 -04:00
|
|
|
});
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
function checkResolveData(
|
|
|
|
future: RouterStateSnapshot, curr: RouterStateSnapshot, injector: any, check: any): void {
|
|
|
|
const p = new PreActivation(future, curr, injector);
|
2017-07-25 14:13:15 -04:00
|
|
|
p.initalize(new ChildrenOutletContexts());
|
2016-08-10 18:53:57 -04:00
|
|
|
p.resolveData().subscribe(check, (e) => { throw e; });
|
2016-06-21 14:49:42 -04:00
|
|
|
}
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2017-07-25 14:13:15 -04:00
|
|
|
function checkGuards(
|
|
|
|
future: RouterStateSnapshot, curr: RouterStateSnapshot, injector: any,
|
|
|
|
check: (result: boolean) => void): void {
|
|
|
|
const p = new PreActivation(future, curr, injector);
|
|
|
|
p.initalize(new ChildrenOutletContexts());
|
|
|
|
p.checkGuards().subscribe(check, (e) => { throw e; });
|
2017-03-02 15:12:46 -05:00
|
|
|
}
|