fix(router): handle path:'' redirects and matches

This commit is contained in:
vsavkin
2016-06-24 11:17:17 -07:00
parent f463e09b9f
commit fbd2dd9ca2
7 changed files with 645 additions and 293 deletions

View File

@ -73,95 +73,37 @@ describe('applyRedirects', () => {
'c/b', (t: UrlTree) => { compareTrees(t, tree('a/b')); });
});
it('should redirect empty path', () => {
it('should support redirects with both main and aux', () => {
checkRedirect(
[
{
path: 'a',
component: ComponentA,
children: [
{path: 'b', component: ComponentB},
]
},
{path: '', redirectTo: 'a'}
],
'b', (t: UrlTree) => { compareTrees(t, tree('a/b')); });
[{
path: 'a',
children: [
{path: 'bb', component: ComponentB}, {path: 'b', redirectTo: 'bb'},
{path: 'cc', component: ComponentC, outlet: 'aux'},
{path: 'b', redirectTo: 'cc', outlet: 'aux'}
]
}],
'a/(b//aux:b)', (t: UrlTree) => { compareTrees(t, tree('a/(bb//aux:cc)')); });
});
it('should redirect empty path (global redirect)', () => {
it('should support redirects with both main and aux (with a nested redirect)', () => {
checkRedirect(
[
{
path: 'a',
component: ComponentA,
children: [
{path: 'b', component: ComponentB},
]
},
{path: '', redirectTo: '/a/b'}
],
'', (t: UrlTree) => { compareTrees(t, tree('a/b')); });
});
[{
path: 'a',
children: [
{path: 'bb', component: ComponentB}, {path: 'b', redirectTo: 'bb'},
xit('should support nested redirects', () => {
checkRedirect(
[
{
path: 'a',
component: ComponentA,
children: [{path: 'b', component: ComponentB}, {path: '', redirectTo: 'b'}]
},
{path: '', redirectTo: 'a'}
],
'', (t: UrlTree) => { compareTrees(t, tree('a/b')); });
});
xit('should support nested redirects (when redirected to an empty path)', () => {
checkRedirect(
[
{
path: '',
component: ComponentA,
children: [{path: 'b', component: ComponentB}, {path: '', redirectTo: 'b'}]
},
{path: 'a', redirectTo: ''}
],
'a', (t: UrlTree) => { compareTrees(t, tree('b')); });
});
xit('should support redirects with both main and aux', () => {
checkRedirect(
[
{
path: 'a',
children: [
{path: 'b', component: ComponentB}, {path: '', redirectTo: 'b'},
{path: 'c', component: ComponentC, outlet: 'aux'},
{path: '', redirectTo: 'c', outlet: 'aux'}
]
},
{path: 'a', redirectTo: ''}
],
'a', (t: UrlTree) => { compareTrees(t, tree('a/(b//aux:c)')); });
});
it('should redirect empty path route only when terminal', () => {
const config = [
{
path: 'a',
component: ComponentA,
children: [
{path: 'b', component: ComponentB},
]
},
{path: '', redirectTo: 'a', terminal: true}
];
applyRedirects(tree('b'), config)
.subscribe(
(_) => { throw 'Should not be reached'; },
e => { expect(e.message).toEqual('Cannot match any routes: \'b\''); });
{
path: 'cc',
component: ComponentC,
outlet: 'aux',
children: [{path: 'dd', component: ComponentC}, {path: 'd', redirectTo: 'dd'}]
},
{path: 'b', redirectTo: 'cc/d', outlet: 'aux'}
]
}],
'a/(b//aux:b)', (t: UrlTree) => { compareTrees(t, tree('a/(bb//aux:cc/dd)')); });
});
it('should redirect wild cards', () => {
@ -185,6 +127,187 @@ describe('applyRedirects', () => {
],
'/a/b/1', (t: UrlTree) => { compareTrees(t, tree('/global/1')); });
});
describe('empty paths', () => {
it('redirect from an empty path should work (local redirect)', () => {
checkRedirect(
[
{
path: 'a',
component: ComponentA,
children: [
{path: 'b', component: ComponentB},
]
},
{path: '', redirectTo: 'a'}
],
'b', (t: UrlTree) => { compareTrees(t, tree('a/b')); });
});
it('redirect from an empty path should work (global redirect)', () => {
checkRedirect(
[
{
path: 'a',
component: ComponentA,
children: [
{path: 'b', component: ComponentB},
]
},
{path: '', redirectTo: '/a/b'}
],
'', (t: UrlTree) => { compareTrees(t, tree('a/b')); });
});
it('should redirect empty path route only when terminal', () => {
const config = [
{
path: 'a',
component: ComponentA,
children: [
{path: 'b', component: ComponentB},
]
},
{path: '', redirectTo: 'a', terminal: true}
];
applyRedirects(tree('b'), config)
.subscribe(
(_) => { throw 'Should not be reached'; },
e => { expect(e.message).toEqual('Cannot match any routes: \'b\''); });
});
it('redirect from an empty path should work (nested case)', () => {
checkRedirect(
[
{
path: 'a',
component: ComponentA,
children: [{path: 'b', component: ComponentB}, {path: '', redirectTo: 'b'}]
},
{path: '', redirectTo: 'a'}
],
'', (t: UrlTree) => { compareTrees(t, tree('a/(b)')); });
});
it('redirect to an empty path should work', () => {
checkRedirect(
[
{path: '', component: ComponentA, children: [{path: 'b', component: ComponentB}]},
{path: 'a', redirectTo: ''}
],
'a/b', (t: UrlTree) => { compareTrees(t, tree('b')); });
});
describe('aux split is in the middle', () => {
it('should create a new url segment (non-terminal)', () => {
checkRedirect(
[{
path: 'a',
children: [
{path: 'b', component: ComponentB},
{path: 'c', component: ComponentC, outlet: 'aux'},
{path: '', redirectTo: 'c', outlet: 'aux'}
]
}],
'a/b', (t: UrlTree) => { compareTrees(t, tree('a/(b//aux:c)')); });
});
it('should create a new url segment (terminal)', () => {
checkRedirect(
[{
path: 'a',
children: [
{path: 'b', component: ComponentB},
{path: 'c', component: ComponentC, outlet: 'aux'},
{path: '', terminal: true, redirectTo: 'c', outlet: 'aux'}
]
}],
'a/b', (t: UrlTree) => { compareTrees(t, tree('a/b')); });
});
});
describe('split at the end (no right child)', () => {
it('should create a new child (non-terminal)', () => {
checkRedirect(
[{
path: 'a',
children: [
{path: 'b', component: ComponentB}, {path: '', redirectTo: 'b'},
{path: 'c', component: ComponentC, outlet: 'aux'},
{path: '', redirectTo: 'c', outlet: 'aux'}
]
}],
'a', (t: UrlTree) => { compareTrees(t, tree('a/(b//aux:c)')); });
});
it('should create a new child (terminal)', () => {
checkRedirect(
[{
path: 'a',
children: [
{path: 'b', component: ComponentB}, {path: '', redirectTo: 'b'},
{path: 'c', component: ComponentC, outlet: 'aux'},
{path: '', terminal: true, redirectTo: 'c', outlet: 'aux'}
]
}],
'a', (t: UrlTree) => { compareTrees(t, tree('a/(b//aux:c)')); });
});
it('should work only only primary outlet', () => {
checkRedirect(
[{
path: 'a',
children: [
{path: 'b', component: ComponentB}, {path: '', redirectTo: 'b'},
{path: 'c', component: ComponentC, outlet: 'aux'}
]
}],
'a/(aux:c)', (t: UrlTree) => { compareTrees(t, tree('a/(b//aux:c)')); });
});
});
describe('split at the end (right child)', () => {
it('should create a new child (non-terminal)', () => {
checkRedirect(
[{
path: 'a',
children: [
{path: 'b', component: ComponentB, children: [{path: 'd', component: ComponentB}]},
{path: '', redirectTo: 'b'}, {
path: 'c',
component: ComponentC,
outlet: 'aux',
children: [{path: 'e', component: ComponentC}]
},
{path: '', redirectTo: 'c', outlet: 'aux'}
]
}],
'a/(d//aux:e)', (t: UrlTree) => { compareTrees(t, tree('a/(b/d//aux:c/e)')); });
});
it('should not create a new child (terminal)', () => {
const config = [{
path: 'a',
children: [
{path: 'b', component: ComponentB, children: [{path: 'd', component: ComponentB}]},
{path: '', redirectTo: 'b'}, {
path: 'c',
component: ComponentC,
outlet: 'aux',
children: [{path: 'e', component: ComponentC}]
},
{path: '', terminal: true, redirectTo: 'c', outlet: 'aux'}
]
}];
applyRedirects(tree('a/(d//aux:e)'), config)
.subscribe(
(_) => { throw 'Should not be reached'; },
e => { expect(e.message).toEqual('Cannot match any routes: \'a\''); });
});
});
});
});
function checkRedirect(config: RouterConfig, url: string, callback: any): void {

View File

@ -107,20 +107,6 @@ describe('recognize', () => {
});
});
xit('should handle nested secondary routes', () => {
checkRecognize(
[
{path: 'a', component: ComponentA}, {path: 'b', component: ComponentB, outlet: 'left'},
{path: 'c', component: ComponentC, outlet: 'right'}
],
'a(left:b(right:c))', (s: RouterStateSnapshot) => {
const c = s.children(s.root);
checkActivatedRoute(c[0], 'a', {}, ComponentA);
checkActivatedRoute(c[1], 'b', {}, ComponentB, 'left');
checkActivatedRoute(c[2], 'c', {}, ComponentC, 'right');
});
});
it('should handle non top-level secondary routes', () => {
checkRecognize(
[
@ -168,86 +154,219 @@ describe('recognize', () => {
});
});
describe('matching empty url', () => {
it('should support root index routes', () => {
recognize(RootComponent, [{path: '', component: ComponentA}], tree(''), '')
.forEach((s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), '', {}, ComponentA);
});
});
describe('empty path', () => {
describe('root', () => {
it('should work', () => {
checkRecognize([{path: '', component: ComponentA}], '', (s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), '', {}, ComponentA);
});
});
it('should support nested root index routes', () => {
recognize(
RootComponent,
[{path: '', component: ComponentA, children: [{path: '', component: ComponentB}]}],
tree(''), '')
.forEach((s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), '', {}, ComponentA);
checkActivatedRoute(s.firstChild(<any>s.firstChild(s.root)), '', {}, ComponentB);
});
});
it('should match when terminal', () => {
checkRecognize(
[{path: '', terminal: true, component: ComponentA}], '', (s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), '', {}, ComponentA);
});
});
it('should set url segment and index properly', () => {
const url = tree('');
recognize(
RootComponent,
[{path: '', component: ComponentA, children: [{path: '', component: ComponentB}]}], url,
'')
.forEach((s: RouterStateSnapshot) => {
expect(s.root._urlSegment).toBe(url.root);
expect(s.root._lastPathIndex).toBe(-1);
const c = s.firstChild(s.root);
expect(c._urlSegment).toBe(url.root);
expect(c._lastPathIndex).toBe(-1);
const c2 = s.firstChild(<any>s.firstChild(s.root));
expect(c2._urlSegment).toBe(url.root);
expect(c2._lastPathIndex).toBe(-1);
});
});
it('should support index routes', () => {
recognize(
RootComponent,
[{path: 'a', component: ComponentA, children: [{path: '', component: ComponentB}]}],
tree('a'), 'a')
.forEach((s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), 'a', {}, ComponentA);
checkActivatedRoute(s.firstChild(<any>s.firstChild(s.root)), '', {}, ComponentB);
});
});
it('should support index routes with children', () => {
recognize(
RootComponent, [{
path: '',
component: ComponentA,
children: [{
it('should not match when terminal', () => {
recognize(
RootComponent, [{
path: '',
component: ComponentB,
children: [{path: 'c/:id', component: ComponentC}]
}]
}],
tree('c/10'), 'c/10')
.forEach((s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), '', {}, ComponentA);
checkActivatedRoute(s.firstChild(<any>s.firstChild(s.root)), '', {}, ComponentB);
checkActivatedRoute(
s.firstChild(<any>s.firstChild(<any>s.firstChild(s.root))), 'c/10', {id: '10'},
ComponentC);
});
terminal: true,
component: ComponentA,
children: [{path: 'b', component: ComponentB}]
}],
tree('b'), '')
.subscribe(
() => {}, (e) => { expect(e.message).toEqual('Cannot match any routes: \'b\''); });
});
it('should work (nested case)', () => {
checkRecognize(
[{path: '', component: ComponentA, children: [{path: '', component: ComponentB}]}], '',
(s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), '', {}, ComponentA);
checkActivatedRoute(s.firstChild(<any>s.firstChild(s.root)), '', {}, ComponentB);
});
});
it('should set url segment and index properly', () => {
const url = tree('');
recognize(
RootComponent,
[{path: '', component: ComponentA, children: [{path: '', component: ComponentB}]}], url,
'')
.forEach((s: RouterStateSnapshot) => {
expect(s.root._urlSegment).toBe(url.root);
expect(s.root._lastPathIndex).toBe(-1);
const c = s.firstChild(s.root);
expect(c._urlSegment).toBe(url.root);
expect(c._lastPathIndex).toBe(-1);
const c2 = s.firstChild(<any>s.firstChild(s.root));
expect(c2._urlSegment).toBe(url.root);
expect(c2._lastPathIndex).toBe(-1);
});
});
});
xit('should pass parameters to every nested index route (case with non-index route)', () => {
recognize(
RootComponent,
[{path: 'a', component: ComponentA, children: [{path: '', component: ComponentB}]}],
tree('/a;a=1'), '/a;a=1')
.forEach((s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), 'a', {a: '1'}, ComponentA);
checkActivatedRoute(s.firstChild(<any>s.firstChild(s.root)), '', {a: '1'}, ComponentB);
});
describe('aux split is in the middle', () => {
it('should match (non-terminal)', () => {
checkRecognize(
[{
path: 'a',
component: ComponentA,
children: [
{path: 'b', component: ComponentB},
{path: '', component: ComponentC, outlet: 'aux'}
]
}],
'a/b', (s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), 'a', {}, ComponentA);
const c = s.children(s.firstChild(s.root));
checkActivatedRoute(c[0], 'b', {}, ComponentB);
checkActivatedRoute(c[1], '', {}, ComponentC, 'aux');
});
});
it('should match (terminal)', () => {
checkRecognize(
[{
path: 'a',
component: ComponentA,
children: [
{path: 'b', component: ComponentB},
{path: '', terminal: true, component: ComponentC, outlet: 'aux'}
]
}],
'a/b', (s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), 'a', {}, ComponentA);
const c = s.children(s.firstChild(s.root));
expect(c.length).toEqual(1);
checkActivatedRoute(c[0], 'b', {}, ComponentB);
});
});
it('should set url segment and index properly', () => {
const url = tree('a/b');
recognize(
RootComponent, [{
path: 'a',
component: ComponentA,
children: [
{path: 'b', component: ComponentB},
{path: '', component: ComponentC, outlet: 'aux'}
]
}],
url, 'a/b')
.forEach((s: RouterStateSnapshot) => {
expect(s.root._urlSegment).toBe(url.root);
expect(s.root._lastPathIndex).toBe(-1);
const a = s.firstChild(s.root);
expect(a._urlSegment).toBe(url.root.children[PRIMARY_OUTLET]);
expect(a._lastPathIndex).toBe(0);
const b = s.firstChild(a);
expect(b._urlSegment).toBe(url.root.children[PRIMARY_OUTLET]);
expect(b._lastPathIndex).toBe(1);
const c = s.children(a)[1];
expect(c._urlSegment).toBe(url.root.children[PRIMARY_OUTLET]);
expect(c._lastPathIndex).toBe(0);
});
});
});
describe('aux split at the end (no right child)', () => {
it('should match (non-terminal)', () => {
checkRecognize(
[{
path: 'a',
component: ComponentA,
children: [
{path: '', component: ComponentB},
{path: '', component: ComponentC, outlet: 'aux'},
]
}],
'a', (s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), 'a', {}, ComponentA);
const c = s.children(s.firstChild(s.root));
checkActivatedRoute(c[0], '', {}, ComponentB);
checkActivatedRoute(c[1], '', {}, ComponentC, 'aux');
});
});
it('should match (terminal)', () => {
checkRecognize(
[{
path: 'a',
component: ComponentA,
children: [
{path: '', terminal: true, component: ComponentB},
{path: '', terminal: true, component: ComponentC, outlet: 'aux'},
]
}],
'a', (s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), 'a', {}, ComponentA);
const c = s.children(s.firstChild(s.root));
checkActivatedRoute(c[0], '', {}, ComponentB);
checkActivatedRoute(c[1], '', {}, ComponentC, 'aux');
});
});
it('should work only only primary outlet', () => {
checkRecognize(
[{
path: 'a',
component: ComponentA,
children: [
{path: '', component: ComponentB},
{path: 'c', component: ComponentC, outlet: 'aux'},
]
}],
'a/(aux:c)', (s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), 'a', {}, ComponentA);
const c = s.children(s.firstChild(s.root));
checkActivatedRoute(c[0], '', {}, ComponentB);
checkActivatedRoute(c[1], 'c', {}, ComponentC, 'aux');
});
});
});
describe('split at the end (right child)', () => {
it('should match (non-terminal)', () => {
checkRecognize(
[{
path: 'a',
component: ComponentA,
children: [
{path: '', component: ComponentB, children: [{path: 'd', component: ComponentD}]},
{
path: '',
component: ComponentC,
outlet: 'aux',
children: [{path: 'e', component: ComponentE}]
},
]
}],
'a/(d//aux:e)', (s: RouterStateSnapshot) => {
checkActivatedRoute(s.firstChild(s.root), 'a', {}, ComponentA);
const c = s.children(s.firstChild(s.root));
checkActivatedRoute(c[0], '', {}, ComponentB);
checkActivatedRoute(s.firstChild(c[0]), 'd', {}, ComponentD);
checkActivatedRoute(c[1], '', {}, ComponentC, 'aux');
checkActivatedRoute(s.firstChild(c[1]), 'e', {}, ComponentE);
});
});
});
});
@ -307,64 +426,6 @@ describe('recognize', () => {
checkActivatedRoute(c, 'c', {}, ComponentC);
});
});
xit('should work with empty paths', () => {
checkRecognize(
[{
path: 'p/:id',
children: [
{path: '', component: ComponentA},
{path: '', component: ComponentB, outlet: 'aux'}
]
}],
'p/11', (s: RouterStateSnapshot) => {
const p = s.firstChild(s.root);
checkActivatedRoute(p, 'p/11', {id: '11'}, undefined);
const c = s.children(p);
console.log('lsfs', c);
checkActivatedRoute(c[0], '', {}, ComponentA);
checkActivatedRoute(c[1], '', {}, ComponentB, 'aux');
});
});
xit('should work with empty paths and params', () => {
checkRecognize(
[{
path: 'p/:id',
children: [
{path: '', component: ComponentA},
{path: '', component: ComponentB, outlet: 'aux'}
]
}],
'p/11/(;pa=33//aux:;pb=44)', (s: RouterStateSnapshot) => {
const p = s.firstChild(s.root);
checkActivatedRoute(p, 'p/11', {id: '11'}, undefined);
const c = s.children(p);
checkActivatedRoute(c[0], '', {pa: '33'}, ComponentA);
checkActivatedRoute(c[1], '', {pb: '44'}, ComponentB, 'aux');
});
});
xit('should work with only aux path', () => {
checkRecognize(
[{
path: 'p/:id',
children: [
{path: '', component: ComponentA},
{path: '', component: ComponentB, outlet: 'aux'}
]
}],
'p/11', (s: RouterStateSnapshot) => {
const p = s.firstChild(s.root);
checkActivatedRoute(p, 'p/11(aux:;pb=44)', {id: '11'}, undefined);
const c = s.children(p);
checkActivatedRoute(c[0], '', {}, ComponentA);
checkActivatedRoute(c[1], '', {pb: '44'}, ComponentB, 'aux');
});
});
});
describe('query parameters', () => {
@ -441,3 +502,5 @@ class RootComponent {}
class ComponentA {}
class ComponentB {}
class ComponentC {}
class ComponentD {}
class ComponentE {}

View File

@ -27,31 +27,9 @@ describe('url serializer', () => {
expect(url.serialize(tree)).toEqual('/one/two(left:three//right:four)');
});
it('should parse segments with empty paths', () => {
const tree = url.parse('/one/two/(;a=1//right:;b=2)');
const c = tree.root.children[PRIMARY_OUTLET];
expectSegment(tree.root.children[PRIMARY_OUTLET], 'one/two', true);
expect(c.children[PRIMARY_OUTLET].pathsWithParams[0].path).toEqual('');
expect(c.children[PRIMARY_OUTLET].pathsWithParams[0].parameters).toEqual({a: '1'});
expect(c.children['right'].pathsWithParams[0].path).toEqual('');
expect(c.children['right'].pathsWithParams[0].parameters).toEqual({b: '2'});
expect(url.serialize(tree)).toEqual('/one/two/(;a=1//right:;b=2)');
});
it('should parse segments with empty paths (only aux)', () => {
const tree = url.parse('/one/two/(right:;b=2)');
const c = tree.root.children[PRIMARY_OUTLET];
expectSegment(tree.root.children[PRIMARY_OUTLET], 'one/two', true);
expect(c.children['right'].pathsWithParams[0].path).toEqual('');
expect(c.children['right'].pathsWithParams[0].parameters).toEqual({b: '2'});
expect(url.serialize(tree)).toEqual('/one/two/(right:;b=2)');
it('should not parse empty path segments with params', () => {
expect(() => url.parse('/one/two/(;a=1//right:;b=2)'))
.toThrowError(/Empty path url segment cannot have parameters/);
});
it('should parse scoped secondary segments', () => {