feat(router): enforce usage of ... syntax for parent to child component routes

This commit is contained in:
Matias Niemelä
2015-06-17 11:57:38 -07:00
parent fa7a3e3449
commit 2d2ae9b8d8
8 changed files with 103 additions and 27 deletions

View File

@ -100,7 +100,7 @@ export function main() {
it('should work with child routers', inject([AsyncTestCompleter], (async) => {
compile('outer { <router-outlet></router-outlet> }')
.then((_) => rtr.config({'path': '/a', 'component': ParentCmp}))
.then((_) => rtr.config({'path': '/a/...', 'component': ParentCmp}))
.then((_) => rtr.navigate('/a/b'))
.then((_) => {
view.detectChanges();
@ -153,7 +153,7 @@ export function main() {
it('should reuse common parent components', inject([AsyncTestCompleter], (async) => {
compile()
.then((_) => rtr.config({'path': '/team/:id', 'component': TeamCmp}))
.then((_) => rtr.config({'path': '/team/:id/...', 'component': TeamCmp}))
.then((_) => rtr.navigate('/team/angular/user/rado'))
.then((_) => {
view.detectChanges();

View File

@ -87,21 +87,24 @@ export function main() {
expect(solution.matchedUrl).toEqual('/bar');
});
it('should perform a valid redirect when a slash or an empty string is being processed', () => {
recognizer.addRedirect('/', '/matias');
recognizer.addRedirect('', '/fatias');
it('should perform a root URL redirect when only a slash or an empty string is being processed',
() => {
recognizer.addRedirect('/', '/matias');
recognizer.addConfig('/matias', handler);
recognizer.addConfig('/matias', handler);
recognizer.addConfig('/fatias', handler);
recognizer.addConfig('/fatias', handler);
var solutions;
var solutions;
solutions = recognizer.recognize('/');
expect(solutions[0].matchedUrl).toBe('/matias');
solutions = recognizer.recognize('/');
expect(solutions[0].matchedUrl).toBe('/matias');
solutions = recognizer.recognize('');
expect(solutions[0].matchedUrl).toBe('/fatias');
});
solutions = recognizer.recognize('/fatias');
expect(solutions[0].matchedUrl).toBe('/fatias');
solutions = recognizer.recognize('');
expect(solutions[0].matchedUrl).toBe('/matias');
});
it('should generate URLs', () => {
recognizer.addConfig('/app/user/:name', handler, 'user');

View File

@ -91,7 +91,7 @@ export function main() {
}));
it('should match the full URL using child components', inject([AsyncTestCompleter], (async) => {
registry.config(rootHostComponent, {'path': '/first', 'component': DummyParentComp});
registry.config(rootHostComponent, {'path': '/first/...', 'component': DummyParentComp});
registry.recognize('/first/second', rootHostComponent)
.then((instruction) => {
@ -103,7 +103,7 @@ export function main() {
it('should match the URL using async child components',
inject([AsyncTestCompleter], (async) => {
registry.config(rootHostComponent, {'path': '/first', 'component': DummyAsyncComp});
registry.config(rootHostComponent, {'path': '/first/...', 'component': DummyAsyncComp});
registry.recognize('/first/second', rootHostComponent)
.then((instruction) => {
@ -117,7 +117,7 @@ export function main() {
inject([AsyncTestCompleter], (async) => {
registry.config(
rootHostComponent,
{'path': '/first', 'component': {'loader': AsyncParentLoader, 'type': 'loader'}});
{'path': '/first/...', 'component': {'loader': AsyncParentLoader, 'type': 'loader'}});
registry.recognize('/first/second', rootHostComponent)
.then((instruction) => {
@ -139,6 +139,21 @@ export function main() {
{'path': '/some/path', 'component': {'type': 'intentionallyWrongComponentType'}}))
.toThrowError('Invalid component type \'intentionallyWrongComponentType\'');
});
it('should throw when a parent config is missing the `...` suffix any of its children add routes',
() => {
expect(() =>
registry.config(rootHostComponent, {'path': '/', 'component': DummyParentComp}))
.toThrowError(
'Child routes are not allowed for "/". Use "..." on the parent\'s route path.');
});
it('should throw when a parent config is missing the `...` suffix any of its children add routes',
() => {
expect(() => registry.config(rootHostComponent,
{'path': '/home/.../fun/', 'component': DummyParentComp}))
.toThrowError('Unexpected "..." before the end of the path for "home/.../fun/".');
});
});
}

View File

@ -122,7 +122,7 @@ class ParentCmp {
@Component({selector: 'app-cmp'})
@View({template: `root { <router-outlet></router-outlet> }`, directives: routerDirectives})
@RouteConfig([{path: '/parent', component: ParentCmp}])
@RouteConfig([{path: '/parent/...', component: ParentCmp}])
class HierarchyAppCmp {
constructor(public router: Router, public location: BrowserLocation) {}
}