2016-04-22 15:04:56 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
xdescribe,
|
|
|
|
describe,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
beforeEachProviders,
|
|
|
|
it,
|
|
|
|
xit
|
2016-05-02 13:36:58 -04:00
|
|
|
} from '@angular/core/testing/testing_internal';
|
2016-04-22 15:04:56 -04:00
|
|
|
|
2016-05-02 13:36:58 -04:00
|
|
|
import {recognize} from '../src/recognize';
|
|
|
|
import {Routes, Route} from '@angular/router';
|
|
|
|
import {provide, Component, ComponentResolver} from '@angular/core';
|
|
|
|
import {UrlSegment, RouteTree, UrlTree} from '../src/segments';
|
|
|
|
import {DefaultRouterUrlSerializer} from '../src/router_url_serializer';
|
|
|
|
import {DEFAULT_OUTLET_NAME} from '../src/constants';
|
2016-04-22 15:04:56 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('recognize', () => {
|
|
|
|
it('should handle position args',
|
|
|
|
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
2016-04-25 19:57:15 -04:00
|
|
|
recognize(resolver, ComponentA, tree("b/paramB/c/paramC/d"))
|
2016-04-22 15:04:56 -04:00
|
|
|
.then(r => {
|
2016-04-25 19:57:15 -04:00
|
|
|
let a = r.root;
|
|
|
|
expect(stringifyUrl(a.urlSegments)).toEqual([""]);
|
|
|
|
expect(a.type).toBe(ComponentA);
|
|
|
|
|
|
|
|
let b = r.firstChild(r.root);
|
2016-04-22 15:04:56 -04:00
|
|
|
expect(stringifyUrl(b.urlSegments)).toEqual(["b", "paramB"]);
|
|
|
|
expect(b.type).toBe(ComponentB);
|
|
|
|
|
2016-04-25 19:57:15 -04:00
|
|
|
let c = r.firstChild(r.firstChild(r.root));
|
2016-04-22 15:04:56 -04:00
|
|
|
expect(stringifyUrl(c.urlSegments)).toEqual(["c", "paramC"]);
|
|
|
|
expect(c.type).toBe(ComponentC);
|
|
|
|
|
2016-04-25 19:57:15 -04:00
|
|
|
let d = r.firstChild(r.firstChild(r.firstChild(r.root)));
|
|
|
|
expect(stringifyUrl(d.urlSegments)).toEqual(["d"]);
|
|
|
|
expect(d.type).toBe(ComponentD);
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2016-04-28 21:33:48 -04:00
|
|
|
it('should support empty routes',
|
|
|
|
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
|
|
|
recognize(resolver, ComponentA, tree("f"))
|
|
|
|
.then(r => {
|
|
|
|
let a = r.root;
|
|
|
|
expect(stringifyUrl(a.urlSegments)).toEqual([""]);
|
|
|
|
expect(a.type).toBe(ComponentA);
|
|
|
|
|
|
|
|
let f = r.firstChild(r.root);
|
|
|
|
expect(stringifyUrl(f.urlSegments)).toEqual(["f"]);
|
|
|
|
expect(f.type).toBe(ComponentF);
|
|
|
|
|
|
|
|
let d = r.firstChild(r.firstChild(r.root));
|
|
|
|
expect(stringifyUrl(d.urlSegments)).toEqual([]);
|
|
|
|
expect(d.type).toBe(ComponentD);
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2016-04-25 19:57:15 -04:00
|
|
|
it('should handle aux routes',
|
|
|
|
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
|
|
|
recognize(resolver, ComponentA, tree("b/paramB(/d//right:d)"))
|
|
|
|
.then(r => {
|
|
|
|
let c = r.children(r.root);
|
|
|
|
expect(stringifyUrl(c[0].urlSegments)).toEqual(["b", "paramB"]);
|
|
|
|
expect(c[0].outlet).toEqual(DEFAULT_OUTLET_NAME);
|
|
|
|
expect(c[0].type).toBe(ComponentB);
|
|
|
|
|
|
|
|
expect(stringifyUrl(c[1].urlSegments)).toEqual(["d"]);
|
|
|
|
expect(c[1].outlet).toEqual("aux");
|
|
|
|
expect(c[1].type).toBe(ComponentD);
|
|
|
|
|
|
|
|
expect(stringifyUrl(c[2].urlSegments)).toEqual(["d"]);
|
|
|
|
expect(c[2].outlet).toEqual("right");
|
|
|
|
expect(c[2].type).toBe(ComponentD);
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should error when two segments with the same outlet name",
|
|
|
|
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
|
|
|
recognize(resolver, ComponentA, tree("b/paramB(right:d//right:e)"))
|
|
|
|
.catch(e => {
|
|
|
|
expect(e.message).toEqual(
|
|
|
|
"Two segments cannot have the same outlet name: 'right:d' and 'right:e'.");
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle nested aux routes',
|
|
|
|
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
|
|
|
recognize(resolver, ComponentA, tree("b/paramB(/d(right:e))"))
|
|
|
|
.then(r => {
|
|
|
|
let c = r.children(r.root);
|
|
|
|
expect(stringifyUrl(c[0].urlSegments)).toEqual(["b", "paramB"]);
|
|
|
|
expect(c[0].outlet).toEqual(DEFAULT_OUTLET_NAME);
|
|
|
|
expect(c[0].type).toBe(ComponentB);
|
|
|
|
|
|
|
|
expect(stringifyUrl(c[1].urlSegments)).toEqual(["d"]);
|
|
|
|
expect(c[1].outlet).toEqual("aux");
|
|
|
|
expect(c[1].type).toBe(ComponentD);
|
|
|
|
|
|
|
|
expect(stringifyUrl(c[2].urlSegments)).toEqual(["e"]);
|
|
|
|
expect(c[2].outlet).toEqual("right");
|
|
|
|
expect(c[2].type).toBe(ComponentE);
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2016-04-27 18:36:11 -04:00
|
|
|
it('should handle non top-level aux routes',
|
|
|
|
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
|
|
|
recognize(resolver, ComponentA, tree('b/paramB/d(e)'))
|
|
|
|
.then(r => {
|
|
|
|
let c = r.children(r.firstChild(r.root));
|
|
|
|
expect(stringifyUrl(c[0].urlSegments)).toEqual(["d"]);
|
|
|
|
expect(c[0].outlet).toEqual(DEFAULT_OUTLET_NAME);
|
|
|
|
expect(c[0].type).toBe(ComponentD);
|
|
|
|
|
|
|
|
expect(stringifyUrl(c[1].urlSegments)).toEqual(["e"]);
|
|
|
|
expect(c[1].outlet).toEqual("aux");
|
|
|
|
expect(c[1].type).toBe(ComponentE);
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2016-04-25 19:57:15 -04:00
|
|
|
it('should handle matrix parameters',
|
|
|
|
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
|
|
|
recognize(resolver, ComponentA, tree("b/paramB;b1=1;b2=2(/d;d1=1;d2=2)"))
|
|
|
|
.then(r => {
|
|
|
|
let c = r.children(r.root);
|
|
|
|
expect(c[0].parameters).toEqual({'b': 'paramB', 'b1': '1', 'b2': '2'});
|
|
|
|
expect(c[1].parameters).toEqual({'d1': '1', 'd2': '2'});
|
|
|
|
|
2016-04-22 15:04:56 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2016-04-30 21:08:26 -04:00
|
|
|
it('should match a wildcard',
|
|
|
|
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
|
|
|
recognize(resolver, ComponentG, tree("a;aa=1/b;bb=2"))
|
|
|
|
.then(r => {
|
|
|
|
let c = r.children(r.root);
|
|
|
|
expect(c.length).toEqual(1);
|
|
|
|
expect(stringifyUrl(c[0].urlSegments)).toEqual([]);
|
|
|
|
expect(c[0].parameters).toEqual(null);
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2016-04-22 15:04:56 -04:00
|
|
|
it('should error when no matching routes',
|
|
|
|
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
2016-04-25 19:57:15 -04:00
|
|
|
recognize(resolver, ComponentA, tree("invalid"))
|
|
|
|
.catch(e => {
|
2016-04-28 21:33:48 -04:00
|
|
|
expect(e.message).toContain("Cannot match any routes");
|
2016-04-25 19:57:15 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should handle no matching routes (too short)',
|
|
|
|
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
|
|
|
recognize(resolver, ComponentA, tree("b"))
|
2016-04-22 15:04:56 -04:00
|
|
|
.catch(e => {
|
2016-04-28 21:33:48 -04:00
|
|
|
expect(e.message).toContain("Cannot match any routes");
|
2016-04-22 15:04:56 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it("should error when a component doesn't have @Routes",
|
|
|
|
inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => {
|
2016-04-25 19:57:15 -04:00
|
|
|
recognize(resolver, ComponentA, tree("d/invalid"))
|
2016-04-22 15:04:56 -04:00
|
|
|
.catch(e => {
|
|
|
|
expect(e.message)
|
|
|
|
.toEqual("Component 'ComponentD' does not have route configuration");
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-01 16:45:14 -04:00
|
|
|
function tree(url: string): UrlTree {
|
2016-04-27 18:36:11 -04:00
|
|
|
return new DefaultRouterUrlSerializer().parse(url);
|
2016-04-22 15:04:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function stringifyUrl(segments: UrlSegment[]): string[] {
|
|
|
|
return segments.map(s => s.segment);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'd', template: 't'})
|
|
|
|
class ComponentD {
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:57:15 -04:00
|
|
|
@Component({selector: 'e', template: 't'})
|
|
|
|
class ComponentE {
|
|
|
|
}
|
|
|
|
|
2016-04-28 21:33:48 -04:00
|
|
|
@Component({selector: 'f', template: 't'})
|
|
|
|
@Routes([new Route({path: "/", component: ComponentD})])
|
|
|
|
class ComponentF {
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:57:15 -04:00
|
|
|
@Component({selector: 'c', template: 't'})
|
|
|
|
@Routes([new Route({path: "d", component: ComponentD})])
|
|
|
|
class ComponentC {
|
|
|
|
}
|
|
|
|
|
2016-04-22 15:04:56 -04:00
|
|
|
@Component({selector: 'b', template: 't'})
|
2016-04-27 18:36:11 -04:00
|
|
|
@Routes([
|
|
|
|
new Route({path: "d", component: ComponentD}),
|
|
|
|
new Route({path: "e", component: ComponentE}),
|
|
|
|
new Route({path: "c/:c", component: ComponentC})
|
|
|
|
])
|
2016-04-22 15:04:56 -04:00
|
|
|
class ComponentB {
|
|
|
|
}
|
|
|
|
|
2016-04-30 21:08:26 -04:00
|
|
|
@Component({selector: 'g', template: 't'})
|
|
|
|
@Routes(
|
|
|
|
[new Route({path: "d", component: ComponentD}), new Route({path: "*", component: ComponentE})])
|
|
|
|
class ComponentG {
|
|
|
|
}
|
|
|
|
|
2016-04-22 15:04:56 -04:00
|
|
|
@Component({selector: 'a', template: 't'})
|
|
|
|
@Routes([
|
|
|
|
new Route({path: "b/:b", component: ComponentB}),
|
2016-04-25 19:57:15 -04:00
|
|
|
new Route({path: "d", component: ComponentD}),
|
2016-04-28 21:33:48 -04:00
|
|
|
new Route({path: "e", component: ComponentE}),
|
|
|
|
new Route({path: "f", component: ComponentF})
|
2016-04-22 15:04:56 -04:00
|
|
|
])
|
|
|
|
class ComponentA {
|
2016-05-02 13:36:58 -04:00
|
|
|
}
|