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-06-16 17:36:51 -04:00
|
|
|
import {validateConfig} from '../src/config';
|
2016-10-20 18:00:15 -04:00
|
|
|
import {PRIMARY_OUTLET} from '../src/shared';
|
2016-06-16 17:36:51 -04:00
|
|
|
|
|
|
|
describe('config', () => {
|
2016-06-21 14:49:42 -04:00
|
|
|
describe('validateConfig', () => {
|
|
|
|
it('should not throw when no errors', () => {
|
2016-12-02 17:09:09 -05:00
|
|
|
expect(
|
|
|
|
() => validateConfig([{path: 'a', redirectTo: 'b'}, {path: 'b', component: ComponentA}]))
|
|
|
|
.not.toThrow();
|
|
|
|
});
|
2016-12-02 17:17:27 -05:00
|
|
|
|
|
|
|
it('should not throw when a matcher is provided', () => {
|
|
|
|
expect(() => validateConfig([{matcher: <any>'someFunc', component: ComponentA}]))
|
|
|
|
.not.toThrow();
|
2016-06-16 17:36:51 -04:00
|
|
|
});
|
|
|
|
|
2016-11-10 17:55:10 -05:00
|
|
|
it('should throw for undefined route', () => {
|
|
|
|
expect(() => {
|
2017-04-17 14:13:13 -04:00
|
|
|
validateConfig(
|
|
|
|
[{path: 'a', component: ComponentA}, , {path: 'b', component: ComponentB}] as any);
|
2016-12-06 13:41:01 -05:00
|
|
|
}).toThrowError(/Invalid configuration of route ''/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw for undefined route in children', () => {
|
|
|
|
expect(() => {
|
|
|
|
validateConfig([{
|
|
|
|
path: 'a',
|
|
|
|
children: [
|
|
|
|
{path: 'b', component: ComponentB},
|
|
|
|
,
|
|
|
|
]
|
2017-04-17 14:13:13 -04:00
|
|
|
}] as any);
|
2016-12-06 13:41:01 -05:00
|
|
|
}).toThrowError(/Invalid configuration of route 'a'/);
|
2016-11-10 17:55:10 -05:00
|
|
|
});
|
|
|
|
|
2016-07-14 11:28:31 -04:00
|
|
|
it('should throw when Array is passed', () => {
|
|
|
|
expect(() => {
|
|
|
|
validateConfig([
|
|
|
|
{path: 'a', component: ComponentA},
|
2017-08-02 20:32:02 -04:00
|
|
|
[{path: 'b', component: ComponentB}, {path: 'c', component: ComponentC}] as any
|
2016-07-14 11:28:31 -04:00
|
|
|
]);
|
2016-12-06 13:41:01 -05:00
|
|
|
}).toThrowError(`Invalid configuration of route '': Array cannot be specified`);
|
2016-07-14 11:28:31 -04:00
|
|
|
});
|
|
|
|
|
2016-06-21 14:49:42 -04:00
|
|
|
it('should throw when redirectTo and children are used together', () => {
|
2016-06-16 17:36:51 -04:00
|
|
|
expect(() => {
|
2016-06-21 14:49:42 -04:00
|
|
|
validateConfig(
|
|
|
|
[{path: 'a', redirectTo: 'b', children: [{path: 'b', component: ComponentA}]}]);
|
|
|
|
})
|
|
|
|
.toThrowError(
|
|
|
|
`Invalid configuration of route 'a': redirectTo and children cannot be used together`);
|
2016-06-16 17:36:51 -04:00
|
|
|
});
|
|
|
|
|
2016-12-06 13:41:01 -05:00
|
|
|
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`);
|
|
|
|
});
|
|
|
|
|
2016-07-06 19:19:52 -04:00
|
|
|
it('should throw when redirectTo and loadChildren are used together', () => {
|
|
|
|
expect(() => { validateConfig([{path: 'a', redirectTo: 'b', loadChildren: 'value'}]); })
|
2016-07-06 14:02:16 -04:00
|
|
|
.toThrowError(
|
2016-07-06 19:19:52 -04:00
|
|
|
`Invalid configuration of route 'a': redirectTo and loadChildren cannot be used together`);
|
2016-07-06 14:02:16 -04:00
|
|
|
});
|
|
|
|
|
2016-07-06 19:19:52 -04:00
|
|
|
it('should throw when children and loadChildren are used together', () => {
|
|
|
|
expect(() => { validateConfig([{path: 'a', children: [], loadChildren: 'value'}]); })
|
2016-07-06 14:02:16 -04:00
|
|
|
.toThrowError(
|
2016-07-06 19:19:52 -04:00
|
|
|
`Invalid configuration of route 'a': children and loadChildren cannot be used together`);
|
2016-07-06 14:02:16 -04:00
|
|
|
});
|
|
|
|
|
2016-06-21 14:49:42 -04:00
|
|
|
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`);
|
2016-06-16 17:36:51 -04:00
|
|
|
});
|
|
|
|
|
2016-12-02 17:09:09 -05:00
|
|
|
it('should throw when path and matcher are used together', () => {
|
2016-11-09 18:25:47 -05:00
|
|
|
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', () => {
|
2017-04-17 14:13:13 -04:00
|
|
|
expect(() => { validateConfig([{component: null, redirectTo: 'b'}] as any); })
|
2016-12-02 17:17:27 -05:00
|
|
|
.toThrowError(
|
2016-12-06 13:41:01 -05:00
|
|
|
`Invalid configuration of route '': routes must have either a path or a matcher specified`);
|
2016-06-16 17:36:51 -04:00
|
|
|
});
|
|
|
|
|
2016-06-21 14:49:42 -04:00
|
|
|
it('should throw when none of component and children or direct are missing', () => {
|
|
|
|
expect(() => { validateConfig([{path: 'a'}]); })
|
|
|
|
.toThrowError(
|
2016-12-06 13:41:01 -05:00
|
|
|
`Invalid configuration of route 'a'. One of the following must be provided: component, redirectTo, children or loadChildren`);
|
2016-06-19 17:44:20 -04:00
|
|
|
});
|
|
|
|
|
2016-06-21 14:49:42 -04:00
|
|
|
it('should throw when path starts with a slash', () => {
|
2016-06-16 17:36:51 -04:00
|
|
|
expect(() => {
|
2016-06-27 23:10:36 -04:00
|
|
|
validateConfig([<any>{path: '/a', redirectTo: 'b'}]);
|
2016-12-06 13:41:01 -05:00
|
|
|
}).toThrowError(`Invalid configuration of route '/a': path cannot start with a slash`);
|
2016-06-16 17:36:51 -04:00
|
|
|
});
|
2016-06-27 23:10:36 -04:00
|
|
|
|
|
|
|
it('should throw when emptyPath is used with redirectTo without explicitly providing matching',
|
|
|
|
() => {
|
|
|
|
expect(() => {
|
|
|
|
validateConfig([<any>{path: '', redirectTo: 'b'}]);
|
2016-12-06 13:41:01 -05:00
|
|
|
}).toThrowError(/Invalid configuration of route '{path: "", redirectTo: "b"}'/);
|
2016-06-27 23:10:36 -04:00
|
|
|
});
|
2016-07-29 12:59:50 -04:00
|
|
|
|
2018-04-06 18:56:36 -04:00
|
|
|
it('should throw when pathMatch is invalid', () => {
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(() => { validateConfig([{path: 'a', pathMatch: 'invalid', component: ComponentB}]); })
|
2016-07-29 12:59:50 -04:00
|
|
|
.toThrowError(
|
|
|
|
/Invalid configuration of route 'a': pathMatch can only be set to 'prefix' or 'full'/);
|
|
|
|
});
|
2016-10-20 18:00:15 -04:00
|
|
|
|
2018-04-06 18:56:36 -04:00
|
|
|
it('should throw when path/outlet combination is invalid', () => {
|
|
|
|
expect(() => { validateConfig([{path: 'a', outlet: 'aux'}]); })
|
2016-10-20 18:00:15 -04:00
|
|
|
.toThrowError(
|
2018-04-06 18:56:36 -04:00
|
|
|
/Invalid configuration of route 'a': a componentless route without children or loadChildren cannot have a named outlet set/);
|
2016-10-20 18:00:15 -04:00
|
|
|
expect(() => validateConfig([{path: 'a', outlet: '', children: []}])).not.toThrow();
|
|
|
|
expect(() => validateConfig([{path: 'a', outlet: PRIMARY_OUTLET, children: []}]))
|
|
|
|
.not.toThrow();
|
|
|
|
});
|
2018-04-06 18:56:36 -04:00
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
2016-06-16 17:36:51 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-21 14:49:42 -04:00
|
|
|
class ComponentA {}
|
|
|
|
class ComponentB {}
|
|
|
|
class ComponentC {}
|