feat(router): update url parser to handle aux routes

This commit is contained in:
vsavkin
2016-04-25 16:54:51 -07:00
committed by Victor Savkin
parent 073ec0a7eb
commit fad3b6434c
4 changed files with 303 additions and 47 deletions
@@ -17,6 +17,7 @@ import {
import {DefaultRouterUrlParser} from 'angular2/src/alt_router/router_url_parser';
import {UrlSegment} from 'angular2/src/alt_router/segments';
import {DEFAULT_OUTLET_NAME} from 'angular2/src/alt_router/constants';
export function main() {
describe('url parsing', () => {
@@ -26,20 +27,90 @@ export function main() {
it('should parse the root url', () => {
let tree = parser.parse("/");
expect(tree.root).toEqual(new UrlSegment("/", {}, ""));
expectSegment(tree.root, "");
});
it('should parse non-empty urls', () => {
let tree = parser.parse("one/two/three");
expect(tree.root).toEqual(new UrlSegment("one", {}, ""));
expect(tree.firstChild(tree.root)).toEqual(new UrlSegment("two", {}, ""));
expect(tree.firstChild(tree.firstChild(tree.root))).toEqual(new UrlSegment("three", {}, ""));
let tree = parser.parse("one/two");
expectSegment(tree.firstChild(tree.root), "one");
expectSegment(tree.firstChild(tree.firstChild(tree.root)), "two");
});
it('should parse non-empty absolute urls', () => {
let tree = parser.parse("/one/two");
expect(tree.root).toEqual(new UrlSegment("/one", {}, ""));
expect(tree.firstChild(tree.root)).toEqual(new UrlSegment("two", {}, ""));
it("should parse multiple aux routes", () => {
let tree = parser.parse("/one/two(/three//right:four)/five");
let c = tree.children(tree.firstChild(tree.root));
expectSegment(c[0], "two");
expectSegment(c[1], "aux:three");
expectSegment(c[2], "right:four");
expectSegment(tree.firstChild(c[0]), "five");
});
it("should parse aux routes that have aux routes", () => {
let tree = parser.parse("/one(/two(/three))");
let c = tree.children(tree.root);
expectSegment(c[0], "one");
expectSegment(c[1], "aux:two");
expectSegment(c[2], "aux:three");
});
it("should parse aux routes that have children", () => {
let tree = parser.parse("/one(/two/three)");
let c = tree.children(tree.root);
expectSegment(c[0], "one");
expectSegment(c[1], "aux:two");
expectSegment(tree.firstChild(c[1]), "three");
});
it("should parse an empty aux route definition", () => {
let tree = parser.parse("/one()");
let c = tree.children(tree.root);
expectSegment(c[0], "one");
expect(tree.children(c[0]).length).toEqual(0);
});
it("should parse key-value matrix params", () => {
let tree = parser.parse("/one;a=11a;b=11b(/two;c=22//right:three;d=33)");
let c = tree.children(tree.root);
expectSegment(c[0], "one;a=11a;b=11b");
expectSegment(c[1], "aux:two;c=22");
expectSegment(c[2], "right:three;d=33");
});
it("should parse key only matrix params", () => {
let tree = parser.parse("/one;a");
let c = tree.children(tree.root);
expectSegment(c[0], "one;a=true");
});
it("should parse key-value query params", () => {
let tree = parser.parse("/one?a=1&b=2");
expect(tree.root).toEqual(new UrlSegment("", {'a': '1', 'b': '2'}, DEFAULT_OUTLET_NAME));
});
it("should parse key only query params", () => {
let tree = parser.parse("/one?a");
expect(tree.root).toEqual(new UrlSegment("", {'a': "true"}, DEFAULT_OUTLET_NAME));
});
it("should parse a url with only query params", () => {
let tree = parser.parse("?a");
expect(tree.root).toEqual(new UrlSegment("", {'a': "true"}, DEFAULT_OUTLET_NAME));
});
it("should allow slashes within query params", () => {
let tree = parser.parse("?a=http://boo");
expect(tree.root).toEqual(new UrlSegment("", {'a': "http://boo"}, DEFAULT_OUTLET_NAME));
});
});
}
function expectSegment(segment: UrlSegment, expected: string): void {
expect(segment.toString()).toEqual(expected);
}
+13 -6
View File
@@ -15,29 +15,36 @@ import {
xit
} from 'angular2/testing_internal';
import {Tree} from 'angular2/src/alt_router/segments';
import {Tree, TreeNode} from 'angular2/src/alt_router/segments';
export function main() {
describe('tree', () => {
it("should return the root of the tree", () => {
let t = new Tree<any>([1, 2, 3]);
let t = new Tree<any>(new TreeNode<number>(1, []));
expect(t.root).toEqual(1);
});
it("should return the parent of a node", () => {
let t = new Tree<any>([1, 2, 3]);
let t = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(2, [])]));
expect(t.parent(1)).toEqual(null);
expect(t.parent(2)).toEqual(1);
});
it("should return the children of a node", () => {
let t = new Tree<any>([1, 2, 3]);
let t = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(2, [])]));
expect(t.children(1)).toEqual([2]);
expect(t.children(2)).toEqual([]);
});
it("should return the first child of a node", () => {
let t = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(2, [])]));
expect(t.firstChild(1)).toEqual(2);
expect(t.firstChild(2)).toEqual(null);
});
it("should return the path to the root", () => {
let t = new Tree<any>([1, 2, 3]);
expect(t.pathToRoot(2)).toEqual([1, 2]);
let t = new Tree<any>(new TreeNode<number>(1, [new TreeNode<number>(2, [])]));
expect(t.pathFromRoot(2)).toEqual([1, 2]);
});
});
}