From cdcaf3e919b9f4b50561cf5a261de688e6ffa4a7 Mon Sep 17 00:00:00 2001 From: Willy Brauner Date: Tue, 30 Aug 2022 12:23:46 +0200 Subject: [PATCH] Add last tests and format --- src/core/core.test.ts | 237 ++++++++++++++++++++++-------------------- 1 file changed, 123 insertions(+), 114 deletions(-) diff --git a/src/core/core.test.ts b/src/core/core.test.ts index fe518fc3..c4c315c4 100644 --- a/src/core/core.test.ts +++ b/src/core/core.test.ts @@ -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 @@ -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" }); }); }); @@ -97,12 +101,11 @@ describe("matcher", () => { describe("getRouteFromUrl", () => { it("should get right route from URL", () => { - const getRoute = getRouteFromUrl({ + let getRoute = getRouteFromUrl({ pUrl: preventSlashes(`${base}/bar/my-id`), pRoutes: routeList, pBase: base, }); - expect(getRoute._fullUrl).toBe(`${base}/bar/my-id`); expect(getRoute._fullPath).toBe(`${base}/bar/:id`); expect(getRoute.path).toBe("/bar/:id"); @@ -110,19 +113,19 @@ describe("matcher", () => { expect(getRoute.name).toBe(`BarPage`); expect(getRoute.props).toEqual({ params: { id: "my-id" }, color: "blue" }); - const getRoute3 = getRouteFromUrl({ + getRoute = getRouteFromUrl({ pUrl: "/hello-2", pRoutes: routeList, pBase: "/", }); - expect(getRoute3._fullPath).toBe(`/hello-2`); + expect(getRoute._fullPath).toBe(`/hello-2`); - const getRoute2 = getRouteFromUrl({ + getRoute = getRouteFromUrl({ pUrl: "/end", pRoutes: routeList, pBase: "/", }); - expect(getRoute2.name).toBe(`EndPage`); + expect(getRoute.name).toBe(`EndPage`); }); it("should get right route from URL with subRoute", () => { @@ -170,32 +173,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 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 already 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...) }); }); @@ -205,52 +213,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}`); + }); }); });