[REF] rework router into a class

This commit is contained in:
Géry Debongnie
2019-07-25 13:24:57 +02:00
parent ca920a0091
commit cd0762b436
9 changed files with 207 additions and 210 deletions
+16 -15
View File
@@ -1,38 +1,39 @@
import { matchRoute, routeToURL } from "../../src/router/plugin";
import { Router } from "../../src/router/Router";
describe("routeToURL", () => {
test("simple non parameterized path", () => {
expect(routeToURL("/abc", {})).toBe("/abc");
expect(routeToURL("/abc/def", {})).toBe("/abc/def");
expect(routeToURL("/abc", {val: 12})).toBe("/abc");
});
test("simple parameterized path", () => {
expect(routeToURL("/abc/{{def}}", {def: 34})).toBe("/abc/34");
});
const routeToURL = Router.prototype.routeToURL;
test("simple non parameterized path", () => {
expect(routeToURL("/abc", {})).toBe("/abc");
expect(routeToURL("/abc/def", {})).toBe("/abc/def");
expect(routeToURL("/abc", { val: 12 })).toBe("/abc");
});
test("simple parameterized path", () => {
expect(routeToURL("/abc/{{def}}", { def: 34 })).toBe("/abc/34");
});
});
describe("match routes", () => {
const matchRoute = Router.prototype.matchRoute;
test("properly match simple routes", () => {
// simple route
expect(matchRoute({ path: "/home", name: "someroute" }, "/home")).toEqual({});
expect(matchRoute("/home", "/home")).toEqual({});
// no match
expect(matchRoute({ path: "/home", name: "someroute" }, "/otherpath")).toEqual(false);
expect(matchRoute("/home", "/otherpath")).toEqual(false);
// fallback route
expect(matchRoute({ path: "*", name: "someroute" }, "somepath")).toEqual({});
expect(matchRoute("*", "somepath")).toEqual({});
});
test("match some parameterized routes", () => {
expect(matchRoute({ path: "/invoices/{{id}}", name: "someroute" }, "/invoices/3")).toEqual({
expect(matchRoute("/invoices/{{id}}", "/invoices/3")).toEqual({
id: "3"
});
});
test("can convert to number if needed", () => {
expect(matchRoute({ path: "/invoices/{{id.number}}", name: "someroute" }, "/invoices/3")).toEqual({
expect(matchRoute("/invoices/{{id.number}}", "/invoices/3")).toEqual({
id: 3
});
});