Skip to content

Commit

Permalink
Add last tests and format
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy Brauner committed Aug 30, 2022
1 parent fc94924 commit 0ffce85
Showing 1 changed file with 118 additions and 108 deletions.
226 changes: 118 additions & 108 deletions src/core/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import {
getSubRouterBase,
getSubRouterRoutes,
patchMissingRootRoute,
applyMiddlewaresToRoutes,
} from "./core";
import { preventSlashes } from "./helpers";
import { routeList } from "../_fixtures/routeList";
import { Routers } from "./Routers";
import { Children } from "react";

/**
* Public
Expand All @@ -26,63 +26,67 @@ import { Children } from "react";
*
*/

describe("createUrl", () => {
it("should create URL properly", () => {
const base = "/";
expect(createUrl("/", base, routeList)).toBe("/");
expect(createUrl("/foo", base, routeList)).toBe("/foo");
expect(createUrl({ name: "ZooPage" }, base, routeList)).toBe("/hello/foo/zoo");
describe("public", () => {
describe("createUrl", () => {
it("should create URL properly", () => {
const base = "/";
expect(createUrl("/", base, routeList)).toBe("/");
expect(createUrl("/foo", base, routeList)).toBe("/foo");
expect(createUrl({ name: "ZooPage" }, base, routeList)).toBe("/hello/foo/zoo");
});
});
});

describe("getSubRouterBase", () => {
it("should return subrouter base URL", () => {
expect(getSubRouterBase("/foo", "")).toBe("/foo");
expect(getSubRouterBase("/foo", "/")).toBe("/foo");
expect(getSubRouterBase("/foo", "/hello/")).toBe("/hello/foo");
expect(getSubRouterBase("/foo", "/hello")).toBe("/hello/foo");
expect(getSubRouterBase("/foo", "/custom/base/hello/")).toBe(
"/custom/base/hello/foo"
);
describe("getSubRouterBase", () => {
it("should return subrouter base URL", () => {
expect(getSubRouterBase("/foo", "")).toBe("/foo");
expect(getSubRouterBase("/foo", "/")).toBe("/foo");
expect(getSubRouterBase("/foo", "/hello/")).toBe("/hello/foo");
expect(getSubRouterBase("/foo", "/hello")).toBe("/hello/foo");
expect(getSubRouterBase("/foo", "/custom/base/hello/")).toBe(
"/custom/base/hello/foo"
);

Routers.langService = new LangService({ languages: [{ key: "en" }, { key: "fr" }] });
const langPathTest = { en: "/foo-en", fr: "/foo-fr" };
expect(getSubRouterBase(langPathTest, "/base/", true)).toBe("/base/:lang/foo-en");
expect(getSubRouterBase(langPathTest, "/base/", false)).toBe("/base/foo-en");
Routers.langService = undefined;
Routers.langService = new LangService({
languages: [{ key: "en" }, { key: "fr" }],
});
const langPathTest = { en: "/foo-en", fr: "/foo-fr" };
expect(getSubRouterBase(langPathTest, "/base/", true)).toBe("/base/:lang/foo-en");
expect(getSubRouterBase(langPathTest, "/base/", false)).toBe("/base/foo-en");
Routers.langService = undefined;
});
});
});

describe("getSubRouterRoutes", () => {
it("should return subrouter route list", () => {
getSubRouterRoutes("/", routeList.find((e) => e.name === "HomePage").children);
getSubRouterRoutes("/:testParam?", [
{
path: "/foo4",
props: { color: "red" },
name: "Foo4Page",
},
]);
describe("getSubRouterRoutes", () => {
it("should return subrouter route list", () => {
getSubRouterRoutes("/", routeList.find((e) => e.name === "HomePage").children);
getSubRouterRoutes("/:testParam?", [
{
path: "/foo4",
props: { color: "red" },
name: "Foo4Page",
},
]);
});
});
});

describe("getPathByRouteName", () => {
it("should return the right path with name", () => {
expect(getPathByRouteName(routeList, "HelloPage")).toEqual("/hello");
expect(getPathByRouteName(routeList, "EndPage")).toEqual("/end");
expect(getPathByRouteName(routeList, "FooPage")).toEqual("/foo");
expect(getPathByRouteName(routeList, "ZooPage")).toEqual("/zoo/:id?");
describe("getPathByRouteName", () => {
it("should return the right path with name", () => {
expect(getPathByRouteName(routeList, "HelloPage")).toEqual("/hello");
expect(getPathByRouteName(routeList, "EndPage")).toEqual("/end");
expect(getPathByRouteName(routeList, "FooPage")).toEqual("/foo");
expect(getPathByRouteName(routeList, "ZooPage")).toEqual("/zoo/:id?");
});
});
});

describe("getStaticPropsFromRoute", () => {
it("should return promise result of staticProps request", async () => {
const ssrStaticProps = await requestStaticPropsFromRoute({
url: "/hello",
base: "/",
routes: routeList,
describe("getStaticPropsFromRoute", () => {
it("should return promise result of staticProps request", async () => {
const ssrStaticProps = await requestStaticPropsFromRoute({
url: "/hello",
base: "/",
routes: routeList,
});
expect(ssrStaticProps).toEqual({ props: { fetchData: {} }, name: "HelloPage" });
});
expect(ssrStaticProps).toEqual({ props: { fetchData: {} }, name: "HelloPage" });
});
});

Expand Down Expand Up @@ -170,32 +174,37 @@ describe("matcher", () => {
*
*
*/
describe("routes", () => {
describe("patchMissingRootRoute", () => {
it("should patch missing route", () => {
const pathchedRoutes = patchMissingRootRoute(routeList[0].children);
const firstRouteAdded = pathchedRoutes[0];
expect(firstRouteAdded.path).toBe("/");
expect(firstRouteAdded.name).toContain("auto-generate-slash-route");
});

describe("patchMissingRootRoute", () => {
it("should patch missing route", () => {
const pathchedRoutes = patchMissingRootRoute(routeList[0].children);
const firstRouteAdded = pathchedRoutes[0];
expect(firstRouteAdded.path).toBe("/");
expect(firstRouteAdded.name).toContain("auto-generate-slash-route");
it("should not patch missing route if '/' route already exist", () => {
const pathchedRoutes = patchMissingRootRoute(routeList);
const firstRouteAdded = pathchedRoutes[0];
expect(firstRouteAdded.path).toBe("/");
expect(firstRouteAdded.name).toBe("HomePage");
});
});

it("should not patch missing route if '/' route exist", () => {
const pathchedRoutes = patchMissingRootRoute(routeList);
const firstRouteAdded = pathchedRoutes[0];
expect(firstRouteAdded.path).toBe("/");
expect(firstRouteAdded.name).toBe("HomePage");
});
});

describe("applyMiddlewaresToRoutes", () => {
it("should apply middleware to routes", () => {
// TODO
describe("applyMiddlewaresToRoutes", () => {
it("should apply middleware to routes", () => {
// TODO
const transformFn = (r) => r.forEach((e) => (e.path = `-${e.path}`));
const routes = [{ path: "/" }, { path: "/foo" }];
const afterMiddlewareRoutes = [{ path: "-/" }, { path: "-/foo" }];
const transformRoutes = applyMiddlewaresToRoutes(routes, [transformFn]);
expect(transformRoutes).toEqual(afterMiddlewareRoutes);
});
});
});

describe("formatRoutes", () => {
it("should format routes properly", () => {
// TODO
describe("formatRoutes", () => {
// No need test for this function who is calling already tested low level fn
// (patchMissingRootRoute etc...)
});
});

Expand All @@ -205,52 +214,53 @@ describe("formatRoutes", () => {
*
*
*/

describe("compileUrl", () => {
it("should build url", () => {
const parh = compileUrl("/foo/:id/bar", { id: "2" });
expect(parh).toBe("/foo/2/bar");
describe("URLs and paths", () => {
describe("compileUrl", () => {
it("should build url", () => {
const parh = compileUrl("/foo/:id/bar", { id: "2" });
expect(parh).toBe("/foo/2/bar");
});
});
});

describe("getFullPathByPath", () => {
it("should return the full path", () => {
expect(getFullPathByPath(routeList, "/foo", "FooPage")).toBe("/hello/foo");
expect(getFullPathByPath(routeList, "/yes", "YesPage")).toBe("/hello/foo/bla/yes");
expect(getFullPathByPath(routeList, "/", "FirstLevelRoute-2")).toBe(
"/hello/foo/bla/"
);
expect(getFullPathByPath(routeList, "/no", "NoPage")).toBe("/hello/foo/bla/no");
describe("getFullPathByPath", () => {
it("should return the full path", () => {
expect(getFullPathByPath(routeList, "/foo", "FooPage")).toBe("/hello/foo");
expect(getFullPathByPath(routeList, "/yes", "YesPage")).toBe("/hello/foo/bla/yes");
expect(getFullPathByPath(routeList, "/", "FirstLevelRoute-2")).toBe(
"/hello/foo/bla/"
);
expect(getFullPathByPath(routeList, "/no", "NoPage")).toBe("/hello/foo/bla/no");
});
});
});

describe("getUrlByRouteName", () => {
it("should return full URL with only page name and params", () => {
expect(getUrlByRouteName(routeList, { name: "HelloPage" })).toBe("/hello");
expect(getUrlByRouteName(routeList, { name: "FooPage" })).toBe("/hello/foo");
expect(getUrlByRouteName(routeList, { name: "BlaPage", params: { id: 2 } })).toBe(
"/hello/foo/bla"
);
expect(getUrlByRouteName(routeList, { name: "NoPage", params: { id: 4 } })).toBe(
"/hello/foo/bla/no"
);
describe("getUrlByRouteName", () => {
it("should return full URL with only page name and params", () => {
expect(getUrlByRouteName(routeList, { name: "HelloPage" })).toBe("/hello");
expect(getUrlByRouteName(routeList, { name: "FooPage" })).toBe("/hello/foo");
expect(getUrlByRouteName(routeList, { name: "BlaPage", params: { id: 2 } })).toBe(
"/hello/foo/bla"
);
expect(getUrlByRouteName(routeList, { name: "NoPage", params: { id: 4 } })).toBe(
"/hello/foo/bla/no"
);
});
});
});

describe("getLangPath", () => {
it("should format routes properly", () => {
const path = "/:lang/foo";
const pathObj = { fr: "/:lang/foo-fr", en: "/:lang/foo-en" };
expect(getLangPath(path, "fr")).toEqual("/foo");
expect(getLangPath(pathObj, "en")).toEqual("/foo-en");
expect(getLangPath(pathObj, "de")).toBeUndefined();
describe("getLangPath", () => {
it("should format routes properly", () => {
const path = "/:lang/foo";
const pathObj = { fr: "/:lang/foo-fr", en: "/:lang/foo-en" };
expect(getLangPath(path, "fr")).toEqual("/foo");
expect(getLangPath(pathObj, "en")).toEqual("/foo-en");
expect(getLangPath(pathObj, "de")).toBeUndefined();
});
});
});

describe("addLangToUrl", () => {
it("should add lang to Url", () => {
const url = "/foo/en/bar";
expect(addLangToUrl(url, "en", true)).toBe(`/en${url}`);
expect(addLangToUrl(url, "en", false)).toBe(`${url}`);
describe("addLangToUrl", () => {
it("should add lang to Url", () => {
const url = "/foo/en/bar";
expect(addLangToUrl(url, "en", true)).toBe(`/en${url}`);
expect(addLangToUrl(url, "en", false)).toBe(`${url}`);
});
});
});

0 comments on commit 0ffce85

Please sign in to comment.