2016-04-27 18:37:11 -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-27 18:37:11 -04:00
|
|
|
|
2016-05-02 13:36:58 -04:00
|
|
|
import {RouteSegment, UrlSegment, UrlTree, TreeNode, RouteTree} from '../src/segments';
|
|
|
|
import {link} from '../src/link';
|
|
|
|
import {DefaultRouterUrlSerializer} from '../src/router_url_serializer';
|
2016-04-27 18:37:11 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('link', () => {
|
|
|
|
let parser = new DefaultRouterUrlSerializer();
|
|
|
|
|
|
|
|
it("should return the original tree when given an empty array", () => {
|
|
|
|
let p = parser.parse("/");
|
2016-04-29 21:04:55 -04:00
|
|
|
let tree = s(p.root);
|
|
|
|
let t = link(tree.root, tree, p, []);
|
2016-04-27 18:37:11 -04:00
|
|
|
expect(t).toBe(p);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should support going to root", () => {
|
|
|
|
let p = parser.parse("/");
|
2016-04-29 21:04:55 -04:00
|
|
|
let tree = s(p.root);
|
|
|
|
let t = link(tree.root, tree, p, ["/"]);
|
2016-04-27 18:37:11 -04:00
|
|
|
expect(parser.serialize(t)).toEqual("");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should support positional params", () => {
|
2016-04-29 21:04:55 -04:00
|
|
|
let p = parser.parse("/a/b");
|
|
|
|
let tree = s(p.firstChild(p.root));
|
|
|
|
let t = link(tree.root, tree, p, ["/one", 11, "two", 22]);
|
2016-04-27 18:37:11 -04:00
|
|
|
expect(parser.serialize(t)).toEqual("/one/11/two/22");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should preserve route siblings when changing the main route", () => {
|
|
|
|
let p = parser.parse("/a/11/b(c)");
|
2016-04-29 21:04:55 -04:00
|
|
|
let tree = s(p.root);
|
|
|
|
let t = link(tree.root, tree, p, ["/a", 11, 'd']);
|
2016-04-27 18:37:11 -04:00
|
|
|
expect(parser.serialize(t)).toEqual("/a/11/d(aux:c)");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should preserve route siblings when changing a aux route", () => {
|
|
|
|
let p = parser.parse("/a/11/b(c)");
|
2016-04-29 21:04:55 -04:00
|
|
|
let tree = s(p.root);
|
|
|
|
let t = link(tree.root, tree, p, ["/a", 11, 'aux:d']);
|
2016-04-27 18:37:11 -04:00
|
|
|
expect(parser.serialize(t)).toEqual("/a/11/b(aux:d)");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update parameters', () => {
|
|
|
|
let p = parser.parse("/a;aa=11");
|
2016-04-29 21:04:55 -04:00
|
|
|
let tree = s(p.root);
|
|
|
|
let t = link(tree.root, tree, p, ["/a", {aa: 22, bb: 33}]);
|
2016-04-27 18:37:11 -04:00
|
|
|
expect(parser.serialize(t)).toEqual("/a;aa=22;bb=33");
|
|
|
|
});
|
2016-04-29 21:04:55 -04:00
|
|
|
|
|
|
|
it("should update relative subtree (when starts with ./)", () => {
|
|
|
|
let p = parser.parse("/a(ap)/c(cp)");
|
|
|
|
let c = p.firstChild(p.root);
|
|
|
|
let tree = s(c);
|
|
|
|
let t = link(tree.root, tree, p, ["./c2"]);
|
|
|
|
expect(parser.serialize(t)).toEqual("/a(aux:ap)/c2(aux:cp)");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update relative subtree (when does not start with ./)", () => {
|
|
|
|
let p = parser.parse("/a(ap)/c(cp)");
|
|
|
|
let c = p.firstChild(p.root);
|
|
|
|
let tree = s(c);
|
|
|
|
let t = link(tree.root, tree, p, ["c2"]);
|
|
|
|
expect(parser.serialize(t)).toEqual("/a(aux:ap)/c2(aux:cp)");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update relative subtree when the provided segment doesn't have url segments", () => {
|
|
|
|
let p = parser.parse("/a(ap)/c(cp)");
|
|
|
|
let c = p.firstChild(p.root);
|
|
|
|
|
|
|
|
let child = new RouteSegment([], null, null, null, null);
|
|
|
|
let root = new TreeNode<RouteSegment>(new RouteSegment([c], {}, null, null, null),
|
|
|
|
[new TreeNode<RouteSegment>(child, [])]);
|
2016-05-01 16:45:14 -04:00
|
|
|
let tree = new RouteTree(root);
|
2016-04-29 21:04:55 -04:00
|
|
|
|
|
|
|
let t = link(child, tree, p, ["./c2"]);
|
|
|
|
expect(parser.serialize(t)).toEqual("/a(aux:ap)/c2(aux:cp)");
|
|
|
|
});
|
2016-04-27 18:37:11 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-01 16:45:14 -04:00
|
|
|
function s(u: UrlSegment): RouteTree {
|
2016-04-29 21:04:55 -04:00
|
|
|
let root = new TreeNode<RouteSegment>(new RouteSegment([u], {}, null, null, null), []);
|
2016-05-01 16:45:14 -04:00
|
|
|
return new RouteTree(root);
|
2016-05-02 13:36:58 -04:00
|
|
|
}
|