forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathconfig.spec.ts
170 lines (149 loc) · 6.39 KB
/
config.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
/**
* @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 {PRIMARY_OUTLET} from '../src/shared';
import {validateConfig} from '../src/utils/config';
describe('config', () => {
describe('validateConfig', () => {
it('should not throw when no errors', () => {
expect(
() => validateConfig([{path: 'a', redirectTo: 'b'}, {path: 'b', component: ComponentA}]))
.not.toThrow();
});
it('should not throw when a matcher is provided', () => {
expect(() => validateConfig([{matcher: <any>'someFunc', component: ComponentA}]))
.not.toThrow();
});
it('should throw for undefined route', () => {
expect(() => {
validateConfig(
[{path: 'a', component: ComponentA}, , {path: 'b', component: ComponentB}] as any);
}).toThrowError(/Invalid configuration of route ''/);
});
it('should throw for undefined route in children', () => {
expect(() => {
validateConfig([{
path: 'a',
children: [
{path: 'b', component: ComponentB},
,
]
}] as any);
}).toThrowError(/Invalid configuration of route 'a'/);
});
it('should throw when Array is passed', () => {
expect(() => {
validateConfig([
{path: 'a', component: ComponentA},
[{path: 'b', component: ComponentB}, {path: 'c', component: ComponentC}] as any
]);
}).toThrowError(`Invalid configuration of route '': Array cannot be specified`);
});
it('should throw when redirectTo and children are used together', () => {
expect(() => {
validateConfig(
[{path: 'a', redirectTo: 'b', children: [{path: 'b', component: ComponentA}]}]);
})
.toThrowError(
`Invalid configuration of route 'a': redirectTo and children cannot be used together`);
});
it('should validate children and report full path', () => {
expect(() => validateConfig([{path: 'a', children: [{path: 'b'}]}]))
.toThrowError(
`Invalid configuration of route 'a/b'. One of the following must be provided: component, redirectTo, children or loadChildren`);
});
it('should properly report deeply nested path', () => {
expect(
() => validateConfig([
{path: 'a', children: [{path: 'b', children: [{path: 'c', children: [{path: 'd'}]}]}]}
]))
.toThrowError(
`Invalid configuration of route 'a/b/c/d'. One of the following must be provided: component, redirectTo, children or loadChildren`);
});
it('should throw when redirectTo and loadChildren are used together', () => {
expect(() => {
validateConfig([{path: 'a', redirectTo: 'b', loadChildren: 'value'}]);
})
.toThrowError(
`Invalid configuration of route 'a': redirectTo and loadChildren cannot be used together`);
});
it('should throw when children and loadChildren are used together', () => {
expect(() => {
validateConfig([{path: 'a', children: [], loadChildren: 'value'}]);
})
.toThrowError(
`Invalid configuration of route 'a': children and loadChildren cannot be used together`);
});
it('should throw when component and redirectTo are used together', () => {
expect(() => {
validateConfig([{path: 'a', component: ComponentA, redirectTo: 'b'}]);
})
.toThrowError(
`Invalid configuration of route 'a': redirectTo and component cannot be used together`);
});
it('should throw when path and matcher are used together', () => {
expect(() => {
validateConfig([{path: 'a', matcher: <any>'someFunc', children: []}]);
})
.toThrowError(
`Invalid configuration of route 'a': path and matcher cannot be used together`);
});
it('should throw when path and matcher are missing', () => {
expect(() => {
validateConfig([{component: null, redirectTo: 'b'}] as any);
})
.toThrowError(
`Invalid configuration of route '': routes must have either a path or a matcher specified`);
});
it('should throw when none of component and children or direct are missing', () => {
expect(() => {
validateConfig([{path: 'a'}]);
})
.toThrowError(
`Invalid configuration of route 'a'. One of the following must be provided: component, redirectTo, children or loadChildren`);
});
it('should throw when path starts with a slash', () => {
expect(() => {
validateConfig([<any>{path: '/a', redirectTo: 'b'}]);
}).toThrowError(`Invalid configuration of route '/a': path cannot start with a slash`);
});
it('should throw when emptyPath is used with redirectTo without explicitly providing matching',
() => {
expect(() => {
validateConfig([<any>{path: '', redirectTo: 'b'}]);
}).toThrowError(/Invalid configuration of route '{path: "", redirectTo: "b"}'/);
});
it('should throw when pathMatch is invalid', () => {
expect(() => {
validateConfig([{path: 'a', pathMatch: 'invalid', component: ComponentB}]);
})
.toThrowError(
/Invalid configuration of route 'a': pathMatch can only be set to 'prefix' or 'full'/);
});
it('should throw when path/outlet combination is invalid', () => {
expect(() => {
validateConfig([{path: 'a', outlet: 'aux'}]);
})
.toThrowError(
/Invalid configuration of route 'a': a componentless route without children or loadChildren cannot have a named outlet set/);
expect(() => validateConfig([{path: 'a', outlet: '', children: []}])).not.toThrow();
expect(() => validateConfig([{path: 'a', outlet: PRIMARY_OUTLET, children: []}]))
.not.toThrow();
});
it('should not throw when path/outlet combination is valid', () => {
expect(() => {
validateConfig([{path: 'a', outlet: 'aux', children: []}]);
}).not.toThrow();
expect(() => {
validateConfig([{path: 'a', outlet: 'aux', loadChildren: 'child'}]);
}).not.toThrow();
});
});
});
class ComponentA {}
class ComponentB {}
class ComponentC {}