2016-08-02 17:34:00 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-08-02 17:34:00 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2018-02-27 17:06:06 -05:00
|
|
|
import {BehaviorSubject} from 'rxjs';
|
2016-10-31 16:11:36 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
import {ActivatedRoute, ActivatedRouteSnapshot, advanceActivatedRoute, equalParamsAndUrlSegments, RouterState, RouterStateSnapshot} from '../src/router_state';
|
2016-10-28 18:17:00 -04:00
|
|
|
import {Params} from '../src/shared';
|
|
|
|
import {UrlSegment} from '../src/url_tree';
|
2016-08-02 17:34:00 -04:00
|
|
|
import {TreeNode} from '../src/utils/tree';
|
|
|
|
|
|
|
|
describe('RouterState & Snapshot', () => {
|
|
|
|
describe('RouterStateSnapshot', () => {
|
|
|
|
let state: RouterStateSnapshot;
|
|
|
|
let a: ActivatedRouteSnapshot;
|
|
|
|
let b: ActivatedRouteSnapshot;
|
|
|
|
let c: ActivatedRouteSnapshot;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
a = createActivatedRouteSnapshot('a');
|
|
|
|
b = createActivatedRouteSnapshot('b');
|
|
|
|
c = createActivatedRouteSnapshot('c');
|
|
|
|
|
|
|
|
const root = new TreeNode(a, [new TreeNode(b, []), new TreeNode(c, [])]);
|
|
|
|
|
2017-12-17 18:10:54 -05:00
|
|
|
state = new (RouterStateSnapshot as any)('url', root);
|
2016-08-02 17:34:00 -04:00
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('should return first child', () => {
|
|
|
|
expect(state.root.firstChild).toBe(b);
|
|
|
|
});
|
2016-08-02 17:34:00 -04:00
|
|
|
|
|
|
|
it('should return children', () => {
|
|
|
|
const cc = state.root.children;
|
|
|
|
expect(cc[0]).toBe(b);
|
|
|
|
expect(cc[1]).toBe(c);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return root', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const b = state.root.firstChild!;
|
2016-08-07 23:09:04 -04:00
|
|
|
expect(b.root).toBe(state.root);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return parent', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const b = state.root.firstChild!;
|
2016-08-02 17:34:00 -04:00
|
|
|
expect(b.parent).toBe(state.root);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return path from root', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const b = state.root.firstChild!;
|
2016-08-02 17:34:00 -04:00
|
|
|
const p = b.pathFromRoot;
|
|
|
|
expect(p[0]).toBe(state.root);
|
|
|
|
expect(p[1]).toBe(b);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('RouterState', () => {
|
|
|
|
let state: RouterState;
|
|
|
|
let a: ActivatedRoute;
|
|
|
|
let b: ActivatedRoute;
|
|
|
|
let c: ActivatedRoute;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
a = createActivatedRoute('a');
|
|
|
|
b = createActivatedRoute('b');
|
|
|
|
c = createActivatedRoute('c');
|
|
|
|
|
|
|
|
const root = new TreeNode(a, [new TreeNode(b, []), new TreeNode(c, [])]);
|
|
|
|
|
2017-12-17 18:10:54 -05:00
|
|
|
state = new (RouterState as any)(root, <any>null);
|
2016-08-02 17:34:00 -04:00
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('should return first child', () => {
|
|
|
|
expect(state.root.firstChild).toBe(b);
|
|
|
|
});
|
2016-08-02 17:34:00 -04:00
|
|
|
|
|
|
|
it('should return children', () => {
|
|
|
|
const cc = state.root.children;
|
|
|
|
expect(cc[0]).toBe(b);
|
|
|
|
expect(cc[1]).toBe(c);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return root', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const b = state.root.firstChild!;
|
2016-08-07 23:09:04 -04:00
|
|
|
expect(b.root).toBe(state.root);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return parent', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const b = state.root.firstChild!;
|
2016-08-02 17:34:00 -04:00
|
|
|
expect(b.parent).toBe(state.root);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return path from root', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const b = state.root.firstChild!;
|
2016-08-02 17:34:00 -04:00
|
|
|
const p = b.pathFromRoot;
|
|
|
|
expect(p[0]).toBe(state.root);
|
|
|
|
expect(p[1]).toBe(b);
|
|
|
|
});
|
|
|
|
});
|
2016-10-28 18:17:00 -04:00
|
|
|
|
|
|
|
describe('equalParamsAndUrlSegments', () => {
|
|
|
|
function createSnapshot(params: Params, url: UrlSegment[]): ActivatedRouteSnapshot {
|
2017-12-17 18:10:54 -05:00
|
|
|
const snapshot = new (ActivatedRouteSnapshot as any)(
|
2016-10-28 18:17:00 -04:00
|
|
|
url, params, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null,
|
2020-04-13 19:40:21 -04:00
|
|
|
-1, null!);
|
2017-12-17 18:10:54 -05:00
|
|
|
snapshot._routerState = new (RouterStateSnapshot as any)('', new TreeNode(snapshot, []));
|
2016-12-14 07:21:45 -05:00
|
|
|
return snapshot;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createSnapshotPairWithParent(
|
|
|
|
params: [Params, Params], parentParams: [Params, Params],
|
|
|
|
urls: [string, string]): [ActivatedRouteSnapshot, ActivatedRouteSnapshot] {
|
|
|
|
const snapshot1 = createSnapshot(params[0], []);
|
|
|
|
const snapshot2 = createSnapshot(params[1], []);
|
|
|
|
|
|
|
|
const snapshot1Parent = createSnapshot(parentParams[0], [new UrlSegment(urls[0], {})]);
|
|
|
|
const snapshot2Parent = createSnapshot(parentParams[1], [new UrlSegment(urls[1], {})]);
|
|
|
|
|
2017-12-17 18:10:54 -05:00
|
|
|
(snapshot1 as any)._routerState = new (RouterStateSnapshot as any)(
|
|
|
|
'', new TreeNode(snapshot1Parent, [new TreeNode(snapshot1, [])]));
|
|
|
|
(snapshot2 as any)._routerState = new (RouterStateSnapshot as any)(
|
|
|
|
'', new TreeNode(snapshot2Parent, [new TreeNode(snapshot2, [])]));
|
2016-12-14 07:21:45 -05:00
|
|
|
|
|
|
|
return [snapshot1, snapshot2];
|
2016-10-28 18:17:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
it('should return false when params are different', () => {
|
2017-10-24 07:54:08 -04:00
|
|
|
expect(equalParamsAndUrlSegments(createSnapshot({a: '1'}, []), createSnapshot({a: '2'}, [])))
|
2016-10-28 18:17:00 -04:00
|
|
|
.toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false when urls are different', () => {
|
|
|
|
expect(equalParamsAndUrlSegments(
|
2017-10-24 07:54:08 -04:00
|
|
|
createSnapshot({a: '1'}, [new UrlSegment('a', {})]),
|
|
|
|
createSnapshot({a: '1'}, [new UrlSegment('b', {})])))
|
2016-10-28 18:17:00 -04:00
|
|
|
.toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true othewise', () => {
|
|
|
|
expect(equalParamsAndUrlSegments(
|
2017-10-24 07:54:08 -04:00
|
|
|
createSnapshot({a: '1'}, [new UrlSegment('a', {})]),
|
|
|
|
createSnapshot({a: '1'}, [new UrlSegment('a', {})])))
|
2016-10-28 18:17:00 -04:00
|
|
|
.toEqual(true);
|
|
|
|
});
|
2016-12-14 07:21:45 -05:00
|
|
|
|
|
|
|
it('should return false when upstream params are different', () => {
|
|
|
|
const [snapshot1, snapshot2] =
|
2017-10-24 07:54:08 -04:00
|
|
|
createSnapshotPairWithParent([{a: '1'}, {a: '1'}], [{b: '1'}, {c: '1'}], ['a', 'a']);
|
2016-12-14 07:21:45 -05:00
|
|
|
|
|
|
|
expect(equalParamsAndUrlSegments(snapshot1, snapshot2)).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false when upstream urls are different', () => {
|
|
|
|
const [snapshot1, snapshot2] =
|
2017-10-24 07:54:08 -04:00
|
|
|
createSnapshotPairWithParent([{a: '1'}, {a: '1'}], [{b: '1'}, {b: '1'}], ['a', 'b']);
|
2016-12-14 07:21:45 -05:00
|
|
|
|
|
|
|
expect(equalParamsAndUrlSegments(snapshot1, snapshot2)).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true when upstream urls and params are equal', () => {
|
|
|
|
const [snapshot1, snapshot2] =
|
2017-10-24 07:54:08 -04:00
|
|
|
createSnapshotPairWithParent([{a: '1'}, {a: '1'}], [{b: '1'}, {b: '1'}], ['a', 'a']);
|
2016-12-14 07:21:45 -05:00
|
|
|
|
|
|
|
expect(equalParamsAndUrlSegments(snapshot1, snapshot2)).toEqual(true);
|
|
|
|
});
|
2016-10-28 18:17:00 -04:00
|
|
|
});
|
2016-10-31 16:11:36 -04:00
|
|
|
|
|
|
|
describe('advanceActivatedRoute', () => {
|
|
|
|
let route: ActivatedRoute;
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
route = createActivatedRoute('a');
|
|
|
|
});
|
2016-10-31 16:11:36 -04:00
|
|
|
|
|
|
|
function createSnapshot(params: Params, url: UrlSegment[]): ActivatedRouteSnapshot {
|
|
|
|
const queryParams = {};
|
|
|
|
const fragment = '';
|
|
|
|
const data = {};
|
2017-12-17 18:10:54 -05:00
|
|
|
const snapshot = new (ActivatedRouteSnapshot as any)(
|
2016-10-31 16:11:36 -04:00
|
|
|
url, params, queryParams, fragment, data, <any>null, <any>null, <any>null, <any>null, -1,
|
2020-04-13 19:40:21 -04:00
|
|
|
null!);
|
2017-12-17 18:10:54 -05:00
|
|
|
const state = new (RouterStateSnapshot as any)('', new TreeNode(snapshot, []));
|
2016-12-14 07:21:45 -05:00
|
|
|
snapshot._routerState = state;
|
|
|
|
return snapshot;
|
2016-10-31 16:11:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
it('should call change observers', () => {
|
2017-10-24 07:54:08 -04:00
|
|
|
const firstPlace = createSnapshot({a: '1'}, []);
|
|
|
|
const secondPlace = createSnapshot({a: '2'}, []);
|
2016-10-31 16:11:36 -04:00
|
|
|
route.snapshot = firstPlace;
|
2017-12-17 18:10:54 -05:00
|
|
|
(route as any)._futureSnapshot = secondPlace;
|
2016-10-31 16:11:36 -04:00
|
|
|
|
|
|
|
let hasSeenDataChange = false;
|
2020-04-13 19:40:21 -04:00
|
|
|
route.data.forEach((data) => {
|
|
|
|
hasSeenDataChange = true;
|
|
|
|
});
|
2016-10-31 16:11:36 -04:00
|
|
|
advanceActivatedRoute(route);
|
|
|
|
expect(hasSeenDataChange).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
2016-08-02 17:34:00 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
function createActivatedRouteSnapshot(cmp: string) {
|
2017-12-17 18:10:54 -05:00
|
|
|
return new (ActivatedRouteSnapshot as any)(
|
2019-03-19 14:44:35 -04:00
|
|
|
<any>[], <any>null, <any>null, <any>null, <any>null, <any>null, <any>cmp, <any>null,
|
2020-04-13 19:40:21 -04:00
|
|
|
<any>null, -1, null!);
|
2016-08-02 17:34:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function createActivatedRoute(cmp: string) {
|
2017-12-17 18:10:54 -05:00
|
|
|
return new (ActivatedRoute as any)(
|
2016-10-31 16:11:36 -04:00
|
|
|
new BehaviorSubject([new UrlSegment('', {})]), new BehaviorSubject({}), <any>null, <any>null,
|
|
|
|
new BehaviorSubject({}), <any>null, <any>cmp, <any>null);
|
2016-08-02 17:34:00 -04:00
|
|
|
}
|