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-08-10 18:53:57 -04:00
|
|
|
import {PreActivation} from '../src/router';
|
|
|
|
import {RouterOutletMap} from '../src/router_outlet_map';
|
|
|
|
import {ActivatedRouteSnapshot, InheritedResolve, RouterStateSnapshot, createEmptyStateSnapshot} from '../src/router_state';
|
|
|
|
import {DefaultUrlSerializer} from '../src/url_tree';
|
|
|
|
import {TreeNode} from '../src/utils/tree';
|
|
|
|
|
|
|
|
describe('Router', () => {
|
|
|
|
describe('PreActivation', () => {
|
|
|
|
const serializer = new DefaultUrlSerializer();
|
|
|
|
const inj = {get: (token: any) => () => `${token}_value`};
|
|
|
|
let empty: RouterStateSnapshot;
|
|
|
|
|
|
|
|
beforeEach(() => { empty = createEmptyStateSnapshot(serializer.parse('/'), null); });
|
|
|
|
|
|
|
|
it('should resolve data', () => {
|
|
|
|
const r = new InheritedResolve(InheritedResolve.empty, {data: 'resolver'});
|
|
|
|
const n = createActivatedRouteSnapshot('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'});
|
2016-07-13 18:25:48 -04:00
|
|
|
});
|
2016-06-01 17:32:15 -04:00
|
|
|
});
|
2016-06-02 17:44:57 -04:00
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
it('should copy over data when creating a snapshot', () => {
|
|
|
|
const r1 = new InheritedResolve(InheritedResolve.empty, {data: 'resolver1'});
|
|
|
|
const r2 = new InheritedResolve(InheritedResolve.empty, {data: 'resolver2'});
|
|
|
|
|
|
|
|
const n1 = createActivatedRouteSnapshot('a', {resolve: r1});
|
|
|
|
const s1 = new RouterStateSnapshot('url', new TreeNode(empty.root, [new TreeNode(n1, [])]));
|
|
|
|
checkResolveData(s1, empty, inj, () => {});
|
|
|
|
|
|
|
|
const n21 = createActivatedRouteSnapshot('a', {resolve: r1});
|
|
|
|
const n22 = createActivatedRouteSnapshot('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);
|
|
|
|
p.traverse(new RouterOutletMap());
|
|
|
|
p.resolveData().subscribe(check, (e) => { throw e; });
|
2016-06-21 14:49:42 -04:00
|
|
|
}
|
2016-05-24 16:23:27 -04:00
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
function createActivatedRouteSnapshot(cmp: string, extra: any = {}): ActivatedRouteSnapshot {
|
|
|
|
return new ActivatedRouteSnapshot(
|
|
|
|
<any>null, {}, <any>null, <any>null, <any>null, <any>null, <any>cmp, <any>null, <any>null, -1,
|
|
|
|
extra.resolve);
|
|
|
|
}
|