2016-05-23 19:14:23 -04:00
|
|
|
import {DefaultUrlSerializer} from '../src/url_serializer';
|
|
|
|
import {UrlTree} from '../src/url_tree';
|
2016-05-26 19:51:44 -04:00
|
|
|
import {Params, PRIMARY_OUTLET} from '../src/shared';
|
2016-06-01 16:29:52 -04:00
|
|
|
import {ActivatedRouteCandidate} from '../src/router_state';
|
2016-05-23 19:14:23 -04:00
|
|
|
import {recognize} from '../src/recognize';
|
|
|
|
|
|
|
|
describe('recognize', () => {
|
|
|
|
it('should work', (done) => {
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, [
|
2016-05-23 19:14:23 -04:00
|
|
|
{
|
|
|
|
path: 'a', component: ComponentA
|
|
|
|
}
|
2016-06-01 16:29:52 -04:00
|
|
|
], tree("a")).forEach(s => {
|
2016-05-23 19:14:23 -04:00
|
|
|
checkActivatedRoute(s.root, "", {}, RootComponent);
|
|
|
|
checkActivatedRoute(s.firstChild(s.root), "a", {}, ComponentA);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-26 19:51:44 -04:00
|
|
|
it('should handle position args', () => {
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, [
|
2016-05-23 19:14:23 -04:00
|
|
|
{
|
|
|
|
path: 'a/:id', component: ComponentA, children: [
|
2016-05-26 19:51:44 -04:00
|
|
|
{ path: 'b/:id', component: ComponentB}
|
2016-05-23 19:14:23 -04:00
|
|
|
]
|
|
|
|
}
|
2016-06-01 16:29:52 -04:00
|
|
|
], tree("a/paramA/b/paramB")).forEach(s => {
|
2016-05-23 19:14:23 -04:00
|
|
|
checkActivatedRoute(s.root, "", {}, RootComponent);
|
|
|
|
checkActivatedRoute(s.firstChild(s.root), "a/paramA", {id: 'paramA'}, ComponentA);
|
|
|
|
checkActivatedRoute(s.firstChild(<any>s.firstChild(s.root)), "b/paramB", {id: 'paramB'}, ComponentB);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-26 19:51:44 -04:00
|
|
|
it('should support secondary routes', () => {
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, [
|
2016-05-26 19:51:44 -04:00
|
|
|
{ path: 'a', component: ComponentA },
|
|
|
|
{ path: 'b', component: ComponentB, outlet: 'left' },
|
|
|
|
{ path: 'c', component: ComponentC, outlet: 'right' }
|
2016-06-01 16:29:52 -04:00
|
|
|
], tree("a(left:b//right:c)")).forEach(s => {
|
2016-05-23 19:14:23 -04:00
|
|
|
const c = s.children(s.root);
|
|
|
|
checkActivatedRoute(c[0], "a", {}, ComponentA);
|
|
|
|
checkActivatedRoute(c[1], "b", {}, ComponentB, 'left');
|
|
|
|
checkActivatedRoute(c[2], "c", {}, ComponentC, 'right');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-26 19:51:44 -04:00
|
|
|
it('should use outlet name when matching secondary routes', () => {
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, [
|
2016-05-26 19:51:44 -04:00
|
|
|
{ path: 'a', component: ComponentA },
|
|
|
|
{ path: 'b', component: ComponentB, outlet: 'left' },
|
|
|
|
{ path: 'b', component: ComponentC, outlet: 'right' }
|
2016-06-01 16:29:52 -04:00
|
|
|
], tree("a(right:b)")).forEach(s => {
|
2016-05-26 19:51:44 -04:00
|
|
|
const c = s.children(s.root);
|
|
|
|
checkActivatedRoute(c[0], "a", {}, ComponentA);
|
|
|
|
checkActivatedRoute(c[1], "b", {}, ComponentC, 'right');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle nested secondary routes', () => {
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, [
|
2016-05-26 19:51:44 -04:00
|
|
|
{ path: 'a', component: ComponentA },
|
|
|
|
{ path: 'b', component: ComponentB, outlet: 'left' },
|
|
|
|
{ path: 'c', component: ComponentC, outlet: 'right' }
|
2016-06-01 16:29:52 -04:00
|
|
|
], tree("a(left:b(right:c))")).forEach(s => {
|
2016-05-23 19:14:23 -04:00
|
|
|
const c = s.children(s.root);
|
|
|
|
checkActivatedRoute(c[0], "a", {}, ComponentA);
|
|
|
|
checkActivatedRoute(c[1], "b", {}, ComponentB, 'left');
|
|
|
|
checkActivatedRoute(c[2], "c", {}, ComponentC, 'right');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-26 19:51:44 -04:00
|
|
|
it('should handle non top-level secondary routes', () => {
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, [
|
2016-05-26 19:51:44 -04:00
|
|
|
{ path: 'a', component: ComponentA, children: [
|
|
|
|
{ path: 'b', component: ComponentB },
|
|
|
|
{ path: 'c', component: ComponentC, outlet: 'left' }
|
2016-05-23 19:14:23 -04:00
|
|
|
] },
|
2016-06-01 16:29:52 -04:00
|
|
|
], tree("a/b(left:c))")).forEach(s => {
|
2016-05-23 19:14:23 -04:00
|
|
|
const c = s.children(<any>s.firstChild(s.root));
|
|
|
|
checkActivatedRoute(c[0], "b", {}, ComponentB, PRIMARY_OUTLET);
|
|
|
|
checkActivatedRoute(c[1], "c", {}, ComponentC, 'left');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-01 16:29:52 -04:00
|
|
|
it('should sort routes by outlet name', () => {
|
|
|
|
recognize(RootComponent, [
|
|
|
|
{ path: 'a', component: ComponentA },
|
|
|
|
{ path: 'c', component: ComponentC, outlet: 'c' },
|
|
|
|
{ path: 'b', component: ComponentB, outlet: 'b' }
|
|
|
|
], tree("a(c:c//b:b)")).forEach(s => {
|
|
|
|
const c = s.children(s.root);
|
|
|
|
checkActivatedRoute(c[0], "a", {}, ComponentA);
|
|
|
|
checkActivatedRoute(c[1], "b", {}, ComponentB, 'b');
|
|
|
|
checkActivatedRoute(c[2], "c", {}, ComponentC, 'c');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-26 19:51:44 -04:00
|
|
|
it('should support matrix parameters', () => {
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, [
|
2016-05-23 19:14:23 -04:00
|
|
|
{
|
|
|
|
path: 'a', component: ComponentA, children: [
|
2016-05-26 19:51:44 -04:00
|
|
|
{ path: 'b', component: ComponentB },
|
|
|
|
{ path: 'c', component: ComponentC, outlet: 'left' }
|
2016-05-23 19:14:23 -04:00
|
|
|
]
|
|
|
|
}
|
2016-06-01 16:29:52 -04:00
|
|
|
], tree("a;a1=11;a2=22/b;b1=111;b2=222(left:c;c1=1111;c2=2222)")).forEach(s => {
|
2016-05-23 19:14:23 -04:00
|
|
|
checkActivatedRoute(s.firstChild(s.root), "a", {a1: '11', a2: '22'}, ComponentA);
|
|
|
|
const c = s.children(<any>s.firstChild(s.root));
|
|
|
|
checkActivatedRoute(c[0], "b", {b1: '111', b2: '222'}, ComponentB);
|
|
|
|
checkActivatedRoute(c[1], "c", {c1: '1111', c2: '2222'}, ComponentC, 'left');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("index", () => {
|
2016-05-26 19:51:44 -04:00
|
|
|
it("should support index routes", () => {
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, [
|
2016-05-26 19:51:44 -04:00
|
|
|
{index: true, component: ComponentA}
|
2016-06-01 16:29:52 -04:00
|
|
|
], tree("")).forEach(s => {
|
2016-05-23 19:14:23 -04:00
|
|
|
checkActivatedRoute(s.firstChild(s.root), "a", {}, ComponentA);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-26 19:51:44 -04:00
|
|
|
it("should support index routes with children", () => {
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, [
|
2016-05-23 19:14:23 -04:00
|
|
|
{
|
2016-05-26 19:51:44 -04:00
|
|
|
index: true, component: ComponentA, children: [
|
|
|
|
{ index: true, component: ComponentB, children: [
|
|
|
|
{path: 'c/:id', component: ComponentC}
|
2016-05-23 19:14:23 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2016-06-01 16:29:52 -04:00
|
|
|
], tree("c/10")).forEach(s => {
|
2016-05-23 19:14:23 -04:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("wildcards", () => {
|
2016-05-26 19:51:44 -04:00
|
|
|
it("should support simple wildcards", () => {
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, [
|
2016-05-26 19:51:44 -04:00
|
|
|
{path: '**', component: ComponentA}
|
2016-06-01 16:29:52 -04:00
|
|
|
], tree("a/b/c/d;a1=11")).forEach(s => {
|
2016-05-23 19:14:23 -04:00
|
|
|
checkActivatedRoute(s.firstChild(s.root), "a/b/c/d", {a1:'11'}, ComponentA);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("query parameters", () => {
|
2016-05-26 19:51:44 -04:00
|
|
|
it("should support query params", () => {
|
|
|
|
const config = [{path: 'a', component: ComponentA}];
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, config, tree("a?q=11")).forEach(s => {
|
|
|
|
expect(s.queryParams).toEqual({q: '11'});
|
2016-05-23 19:14:23 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("fragment", () => {
|
2016-05-26 19:51:44 -04:00
|
|
|
it("should support fragment", () => {
|
|
|
|
const config = [{path: 'a', component: ComponentA}];
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, config, tree("a#f1")).forEach(s => {
|
|
|
|
expect(s.fragment).toEqual("f1");
|
2016-05-23 19:14:23 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("error handling", () => {
|
2016-05-26 19:51:44 -04:00
|
|
|
it('should error when two routes with the same outlet name got matched', () => {
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, [
|
2016-05-26 19:51:44 -04:00
|
|
|
{ path: 'a', component: ComponentA },
|
|
|
|
{ path: 'b', component: ComponentB, outlet: 'aux' },
|
|
|
|
{ path: 'c', component: ComponentC, outlet: 'aux' }
|
2016-06-01 16:29:52 -04:00
|
|
|
], tree("a(aux:b//aux:c)")).subscribe(null, s => {
|
2016-05-26 19:51:44 -04:00
|
|
|
expect(s.toString()).toContain("Two segments cannot have the same outlet name: 'aux:b' and 'aux:c'.");
|
2016-05-23 19:14:23 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-26 19:51:44 -04:00
|
|
|
it("should error when no matching routes", () => {
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, [
|
2016-05-26 19:51:44 -04:00
|
|
|
{ path: 'a', component: ComponentA }
|
2016-06-01 16:29:52 -04:00
|
|
|
], tree("invalid")).subscribe(null, s => {
|
2016-05-23 19:14:23 -04:00
|
|
|
expect(s.toString()).toContain("Cannot match any routes");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-26 19:51:44 -04:00
|
|
|
it("should error when no matching routes (too short)", () => {
|
2016-06-01 16:29:52 -04:00
|
|
|
recognize(RootComponent, [
|
2016-05-26 19:51:44 -04:00
|
|
|
{ path: 'a/:id', component: ComponentA }
|
2016-06-01 16:29:52 -04:00
|
|
|
], tree("a")).subscribe(null, s => {
|
2016-05-23 19:14:23 -04:00
|
|
|
expect(s.toString()).toContain("Cannot match any routes");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-01 16:29:52 -04:00
|
|
|
function checkActivatedRoute(actual: ActivatedRouteCandidate | null, url: string, params: Params, cmp: Function, outlet: string = PRIMARY_OUTLET):void {
|
2016-05-23 19:14:23 -04:00
|
|
|
if (actual === null) {
|
|
|
|
expect(actual).toBeDefined();
|
|
|
|
} else {
|
2016-06-01 16:29:52 -04:00
|
|
|
expect(actual.urlSegments.map(s => s.path).join("/")).toEqual(url);
|
|
|
|
expect(actual.params).toEqual(params);
|
2016-05-23 19:14:23 -04:00
|
|
|
expect(actual.component).toBe(cmp);
|
|
|
|
expect(actual.outlet).toEqual(outlet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tree(url: string): UrlTree {
|
|
|
|
return new DefaultUrlSerializer().parse(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
class RootComponent {}
|
|
|
|
class ComponentA {}
|
|
|
|
class ComponentB {}
|
|
|
|
class ComponentC {}
|