Farmbot-Web-App/frontend/__tests__/route_config_test.tsx

55 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-09-21 08:09:30 -06:00
import { UNBOUND_ROUTES, UnboundRouteConfig } from "../route_config";
import { RouteEnterEvent } from "takeme";
interface ConnectedComponent {
2019-04-09 23:17:03 -06:00
displayName: string;
2019-03-11 20:34:49 -06:00
WrappedComponent: React.ComponentType;
2019-04-09 23:17:03 -06:00
name?: string;
2018-09-21 08:09:30 -06:00
}
type Info = UnboundRouteConfig<{}, {}>;
2019-03-11 20:34:49 -06:00
const fakeCallback = (
component: ConnectedComponent,
child: ConnectedComponent | undefined,
2020-02-28 09:35:32 -07:00
info: Info,
2019-03-11 20:34:49 -06:00
) => {
if (info.$ == "*") {
expect(component.name).toEqual("FourOhFour");
} else {
2019-04-09 23:17:03 -06:00
expect(component.displayName).toContain("Connect");
expect(component.displayName).toContain(info.key);
2019-03-11 20:34:49 -06:00
expect(component.WrappedComponent.name).toContain(info.key);
if (child && info.children) {
2019-04-09 23:17:03 -06:00
expect(child.displayName).toContain("Connect");
expect(child.displayName).toContain(info.childKey);
2019-03-11 20:34:49 -06:00
expect(child.WrappedComponent.name).toContain(info.childKey);
}
}
};
2018-09-21 08:18:29 -06:00
2019-03-11 20:34:49 -06:00
const fakeRouteEnterEvent: RouteEnterEvent = {
params: { splat: "????" },
oldPath: "??",
newPath: "??"
};
2018-09-21 08:09:30 -06:00
describe("UNBOUND_ROUTES", () => {
2019-03-11 20:34:49 -06:00
it("generates correct routes", () => {
UNBOUND_ROUTES
.map(r => r(fakeCallback))
.map(r => r.enter && r.enter(fakeRouteEnterEvent));
});
it("generates crash route", async () => {
const fakeError = new Error("fake callback error");
const cb = jest.fn()
.mockImplementationOnce(() => { throw fakeError; })
.mockImplementationOnce(x => { expect(x.name).toEqual("Apology"); });
const r = UNBOUND_ROUTES[0](cb);
console.error = jest.fn();
r.enter && await r.enter(fakeRouteEnterEvent);
expect(console.error).toHaveBeenCalledWith(fakeError);
});
2018-09-21 08:09:30 -06:00
});