forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathrouter_state.spec.ts
217 lines (176 loc) · 7.21 KB
/
router_state.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* @license
* Copyright Google LLC 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
*/
import {BehaviorSubject} from 'rxjs';
import {ActivatedRoute, ActivatedRouteSnapshot, advanceActivatedRoute, equalParamsAndUrlSegments, RouterState, RouterStateSnapshot} from '../src/router_state';
import {Params} from '../src/shared';
import {UrlSegment} from '../src/url_tree';
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, [])]);
state = new (RouterStateSnapshot as any)('url', root);
});
it('should return first child', () => {
expect(state.root.firstChild).toBe(b);
});
it('should return children', () => {
const cc = state.root.children;
expect(cc[0]).toBe(b);
expect(cc[1]).toBe(c);
});
it('should return root', () => {
const b = state.root.firstChild!;
expect(b.root).toBe(state.root);
});
it('should return parent', () => {
const b = state.root.firstChild!;
expect(b.parent).toBe(state.root);
});
it('should return path from root', () => {
const b = state.root.firstChild!;
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, [])]);
state = new (RouterState as any)(root, <any>null);
});
it('should return first child', () => {
expect(state.root.firstChild).toBe(b);
});
it('should return children', () => {
const cc = state.root.children;
expect(cc[0]).toBe(b);
expect(cc[1]).toBe(c);
});
it('should return root', () => {
const b = state.root.firstChild!;
expect(b.root).toBe(state.root);
});
it('should return parent', () => {
const b = state.root.firstChild!;
expect(b.parent).toBe(state.root);
});
it('should return path from root', () => {
const b = state.root.firstChild!;
const p = b.pathFromRoot;
expect(p[0]).toBe(state.root);
expect(p[1]).toBe(b);
});
});
describe('equalParamsAndUrlSegments', () => {
function createSnapshot(params: Params, url: UrlSegment[]): ActivatedRouteSnapshot {
const snapshot = new (ActivatedRouteSnapshot as any)(
url, params, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null,
-1, null!);
snapshot._routerState = new (RouterStateSnapshot as any)('', new TreeNode(snapshot, []));
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], {})]);
(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, [])]));
return [snapshot1, snapshot2];
}
it('should return false when params are different', () => {
expect(equalParamsAndUrlSegments(createSnapshot({a: '1'}, []), createSnapshot({a: '2'}, [])))
.toEqual(false);
});
it('should return false when urls are different', () => {
expect(equalParamsAndUrlSegments(
createSnapshot({a: '1'}, [new UrlSegment('a', {})]),
createSnapshot({a: '1'}, [new UrlSegment('b', {})])))
.toEqual(false);
});
it('should return true othewise', () => {
expect(equalParamsAndUrlSegments(
createSnapshot({a: '1'}, [new UrlSegment('a', {})]),
createSnapshot({a: '1'}, [new UrlSegment('a', {})])))
.toEqual(true);
});
it('should return false when upstream params are different', () => {
const [snapshot1, snapshot2] =
createSnapshotPairWithParent([{a: '1'}, {a: '1'}], [{b: '1'}, {c: '1'}], ['a', 'a']);
expect(equalParamsAndUrlSegments(snapshot1, snapshot2)).toEqual(false);
});
it('should return false when upstream urls are different', () => {
const [snapshot1, snapshot2] =
createSnapshotPairWithParent([{a: '1'}, {a: '1'}], [{b: '1'}, {b: '1'}], ['a', 'b']);
expect(equalParamsAndUrlSegments(snapshot1, snapshot2)).toEqual(false);
});
it('should return true when upstream urls and params are equal', () => {
const [snapshot1, snapshot2] =
createSnapshotPairWithParent([{a: '1'}, {a: '1'}], [{b: '1'}, {b: '1'}], ['a', 'a']);
expect(equalParamsAndUrlSegments(snapshot1, snapshot2)).toEqual(true);
});
});
describe('advanceActivatedRoute', () => {
let route: ActivatedRoute;
beforeEach(() => {
route = createActivatedRoute('a');
});
function createSnapshot(params: Params, url: UrlSegment[]): ActivatedRouteSnapshot {
const queryParams = {};
const fragment = '';
const data = {};
const snapshot = new (ActivatedRouteSnapshot as any)(
url, params, queryParams, fragment, data, <any>null, <any>null, <any>null, <any>null, -1,
null!);
const state = new (RouterStateSnapshot as any)('', new TreeNode(snapshot, []));
snapshot._routerState = state;
return snapshot;
}
it('should call change observers', () => {
const firstPlace = createSnapshot({a: '1'}, []);
const secondPlace = createSnapshot({a: '2'}, []);
route.snapshot = firstPlace;
(route as any)._futureSnapshot = secondPlace;
let hasSeenDataChange = false;
route.data.forEach((data) => {
hasSeenDataChange = true;
});
advanceActivatedRoute(route);
expect(hasSeenDataChange).toEqual(true);
});
});
});
function createActivatedRouteSnapshot(cmp: string) {
return new (ActivatedRouteSnapshot as any)(
<any>[], <any>null, <any>null, <any>null, <any>null, <any>null, <any>cmp, <any>null,
<any>null, -1, null!);
}
function createActivatedRoute(cmp: string) {
return new (ActivatedRoute as any)(
new BehaviorSubject([new UrlSegment('', {})]), new BehaviorSubject({}), <any>null, <any>null,
new BehaviorSubject({}), <any>null, <any>cmp, <any>null);
}