Tests for maybeReplaceDesignerModules

pull/695/head
Rick Carlino 2018-03-04 16:56:10 -06:00
parent f6a8fffb82
commit 81636758c7
2 changed files with 43 additions and 11 deletions

View File

@ -1,14 +1,45 @@
jest.mock("fastclick", () => ({ attach: jest.fn() }));
import { topLevelRoutes } from "../route_config";
import {
topLevelRoutes,
designerRoutes,
maybeReplaceDesignerModules
} from "../route_config";
import { RouterState, RedirectFunction } from "react-router";
describe("route configs", () => {
it("generates all of them", async () => {
const cb = jest.fn();
const routes = topLevelRoutes.childRoutes;
const results = await Promise.all(routes.map(route => route.getComponent(undefined, cb)));
expect(cb).toHaveBeenCalled();
expect(cb).toHaveBeenCalledTimes(routes.length);
cb.mock.calls.map(x => expect(!!x[1]).toBeTruthy());
async function makeSureTheyAreRoutes(input: typeof topLevelRoutes.childRoutes) {
const cb = jest.fn();
const results = await Promise
.all(input.map(route => route.getComponent(undefined, cb)));
expect(cb).toHaveBeenCalled();
expect(cb).toHaveBeenCalledTimes(input.length);
cb.mock.calls.map(x => expect(!!x[1]).toBeTruthy());
}
describe("top level routes", () => {
it("generates all of them",
() => makeSureTheyAreRoutes(topLevelRoutes.childRoutes));
});
describe("designer routes", () => {
it("generates all of them",
() => makeSureTheyAreRoutes(designerRoutes.childRoutes));
});
describe("maybeReplaceDesignerModules", () => {
it("does replace the route", () => {
const pathname = "/app/designer";
const next = { location: { pathname } } as RouterState;
const replace = jest.fn() as RedirectFunction;
maybeReplaceDesignerModules(next, replace);
expect(replace).toHaveBeenCalledWith(`${pathname}/plants`);
});
it("does not replace the route", () => {
const pathname = "/app/nope";
const next = { location: { pathname } } as RouterState;
const replace = jest.fn() as RedirectFunction;
maybeReplaceDesignerModules(next, replace);
expect(replace).not.toHaveBeenCalled();
});
});

View File

@ -5,7 +5,8 @@ import { RouterState, RedirectFunction } from "react-router";
/** These methods are a way to determine how to load certain modules
* based on the device (mobile or desktop) for optimization/css purposes.
*/
function maybeReplaceDesignerModules(next: RouterState, replace: RedirectFunction) {
export function maybeReplaceDesignerModules(next: RouterState,
replace: RedirectFunction) {
if (next.location.pathname === "/app/designer") {
replace(`${next.location.pathname}/plants`);
}
@ -24,7 +25,7 @@ function page(path: string, getter: () => Promise<React.ReactType>) {
const controlsRoute =
page("app/controls", async () => (await import("./controls/controls")).Controls);
const designerRoutes = {
export const designerRoutes = {
path: "app/designer",
onEnter: maybeReplaceDesignerModules,
getComponent(_discard: void, cb: Function) {