2016-06-14 17:55:59 -04:00
|
|
|
import {DefaultUrlSerializer, serializePath} from '../src/url_serializer';
|
2016-05-21 20:35:55 -04:00
|
|
|
import {UrlSegment} from '../src/url_tree';
|
2016-06-14 17:55:59 -04:00
|
|
|
import {PRIMARY_OUTLET} from '../src/shared';
|
2016-05-21 20:35:55 -04:00
|
|
|
|
|
|
|
describe('url serializer', () => {
|
2016-05-26 19:50:59 -04:00
|
|
|
const url = new DefaultUrlSerializer();
|
2016-05-21 20:35:55 -04:00
|
|
|
|
|
|
|
it('should parse the root url', () => {
|
2016-05-26 19:50:59 -04:00
|
|
|
const tree = url.parse("/");
|
2016-06-14 17:55:59 -04:00
|
|
|
|
2016-05-21 20:35:55 -04:00
|
|
|
expectSegment(tree.root, "");
|
2016-06-14 17:55:59 -04:00
|
|
|
expect(url.serialize(tree)).toEqual("/");
|
2016-05-21 20:35:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse non-empty urls', () => {
|
2016-05-26 19:50:59 -04:00
|
|
|
const tree = url.parse("one/two");
|
2016-06-14 17:55:59 -04:00
|
|
|
expectSegment(tree.root.children[PRIMARY_OUTLET], "one/two");
|
2016-05-21 20:35:55 -04:00
|
|
|
expect(url.serialize(tree)).toEqual("/one/two");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse multiple secondary segments", () => {
|
2016-06-14 17:55:59 -04:00
|
|
|
const tree = url.parse("/one/two(left:three//right:four)");
|
|
|
|
|
|
|
|
expectSegment(tree.root.children[PRIMARY_OUTLET], "one/two");
|
|
|
|
expectSegment(tree.root.children['left'], "three");
|
|
|
|
expectSegment(tree.root.children['right'], "four");
|
|
|
|
|
|
|
|
expect(url.serialize(tree)).toEqual("/one/two(left:three//right:four)");
|
|
|
|
});
|
|
|
|
|
2016-06-19 16:45:40 -04:00
|
|
|
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)");
|
|
|
|
});
|
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
it("should parse scoped secondary segments", () => {
|
|
|
|
const tree = url.parse("/one/(two//left:three)");
|
2016-05-21 20:35:55 -04:00
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
const primary = tree.root.children[PRIMARY_OUTLET];
|
|
|
|
expectSegment(primary, "one", true);
|
2016-05-21 20:35:55 -04:00
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
expectSegment(primary.children[PRIMARY_OUTLET], "two");
|
|
|
|
expectSegment(primary.children["left"], "three");
|
2016-05-21 20:35:55 -04:00
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
expect(url.serialize(tree)).toEqual("/one/(two//left:three)");
|
2016-05-21 20:35:55 -04:00
|
|
|
});
|
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
it("should parse scoped secondary segments with unscoped ones", () => {
|
|
|
|
const tree = url.parse("/one/(two//left:three)(right:four)");
|
2016-05-21 20:35:55 -04:00
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
const primary = tree.root.children[PRIMARY_OUTLET];
|
|
|
|
expectSegment(primary, "one", true);
|
|
|
|
expectSegment(primary.children[PRIMARY_OUTLET], "two");
|
|
|
|
expectSegment(primary.children["left"], "three");
|
|
|
|
expectSegment(tree.root.children["right"], "four");
|
2016-05-21 20:35:55 -04:00
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
expect(url.serialize(tree)).toEqual("/one/(two//left:three)(right:four)");
|
2016-05-21 20:35:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse secondary segments that have children", () => {
|
2016-05-26 19:50:59 -04:00
|
|
|
const tree = url.parse("/one(left:two/three)");
|
2016-05-21 20:35:55 -04:00
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
expectSegment(tree.root.children[PRIMARY_OUTLET], "one");
|
|
|
|
expectSegment(tree.root.children['left'], "two/three");
|
2016-05-21 20:35:55 -04:00
|
|
|
|
2016-05-26 19:50:59 -04:00
|
|
|
expect(url.serialize(tree)).toEqual("/one(left:two/three)");
|
2016-05-21 20:35:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse an empty secondary segment group", () => {
|
2016-05-26 19:50:59 -04:00
|
|
|
const tree = url.parse("/one()");
|
2016-05-21 20:35:55 -04:00
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
expectSegment(tree.root.children[PRIMARY_OUTLET], "one");
|
2016-05-21 20:35:55 -04:00
|
|
|
|
|
|
|
expect(url.serialize(tree)).toEqual("/one");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse key-value matrix params", () => {
|
2016-05-26 19:50:59 -04:00
|
|
|
const tree = url.parse("/one;a=11a;b=11b(left:two;c=22//right:three;d=33)");
|
2016-05-21 20:35:55 -04:00
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
expectSegment(tree.root.children[PRIMARY_OUTLET], "one;a=11a;b=11b");
|
|
|
|
expectSegment(tree.root.children["left"], "two;c=22");
|
|
|
|
expectSegment(tree.root.children["right"], "three;d=33");
|
2016-05-21 20:35:55 -04:00
|
|
|
|
2016-05-26 19:50:59 -04:00
|
|
|
expect(url.serialize(tree)).toEqual("/one;a=11a;b=11b(left:two;c=22//right:three;d=33)");
|
2016-05-21 20:35:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse key only matrix params", () => {
|
2016-05-26 19:50:59 -04:00
|
|
|
const tree = url.parse("/one;a");
|
2016-05-21 20:35:55 -04:00
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
expectSegment(tree.root.children[PRIMARY_OUTLET], "one;a=true");
|
2016-05-21 20:35:55 -04:00
|
|
|
|
|
|
|
expect(url.serialize(tree)).toEqual("/one;a=true");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse query params", () => {
|
2016-05-26 19:50:59 -04:00
|
|
|
const tree = url.parse("/one?a=1&b=2");
|
2016-06-06 19:34:48 -04:00
|
|
|
expect(tree.queryParams).toEqual({a: '1', b: '2'});
|
2016-05-21 20:35:55 -04:00
|
|
|
});
|
|
|
|
|
2016-06-16 19:28:35 -04:00
|
|
|
it("should parse query params when with parenthesis", () => {
|
|
|
|
const tree = url.parse("/one?a=(11)&b=(22)");
|
|
|
|
expect(tree.queryParams).toEqual({a: '(11)', b: '(22)'});
|
|
|
|
});
|
|
|
|
|
2016-05-21 20:35:55 -04:00
|
|
|
it("should parse key only query params", () => {
|
2016-05-26 19:50:59 -04:00
|
|
|
const tree = url.parse("/one?a");
|
2016-06-06 19:34:48 -04:00
|
|
|
expect(tree.queryParams).toEqual({a: 'true'});
|
2016-05-21 20:35:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should serializer query params", () => {
|
2016-05-26 19:50:59 -04:00
|
|
|
const tree = url.parse("/one?a");
|
2016-05-21 20:35:55 -04:00
|
|
|
expect(url.serialize(tree)).toEqual("/one?a=true");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse fragment", () => {
|
2016-05-26 19:50:59 -04:00
|
|
|
const tree = url.parse("/one#two");
|
2016-05-21 20:35:55 -04:00
|
|
|
expect(tree.fragment).toEqual("two");
|
|
|
|
expect(url.serialize(tree)).toEqual("/one#two");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse empty fragment", () => {
|
2016-05-26 19:50:59 -04:00
|
|
|
const tree = url.parse("/one#");
|
2016-05-21 20:35:55 -04:00
|
|
|
expect(tree.fragment).toEqual("");
|
|
|
|
expect(url.serialize(tree)).toEqual("/one#");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-14 17:55:59 -04:00
|
|
|
function expectSegment(segment:UrlSegment, expected:string, hasChildren: boolean = false):void {
|
|
|
|
const p = segment.pathsWithParams.map(p => serializePath(p)).join("/");
|
|
|
|
expect(p).toEqual(expected);
|
|
|
|
expect(Object.keys(segment.children).length > 0).toEqual(hasChildren);
|
2016-05-21 20:35:55 -04:00
|
|
|
}
|