From 7e31990201c471577884ce2eeb0c7dabae94b913 Mon Sep 17 00:00:00 2001 From: Bart Date: Mon, 22 Mar 2021 21:58:23 +0100 Subject: [PATCH] Fix unit tests --- apps/blueprints/specs/index.spec.tsx | 4 +++- apps/blueprints/src/hooks/fbe.hook.ts | 8 ++++++-- apps/blueprints/src/hooks/query.hook.ts | 2 +- apps/blueprints/src/pages/index.tsx | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/apps/blueprints/specs/index.spec.tsx b/apps/blueprints/specs/index.spec.tsx index 533d232..1f3d4a7 100644 --- a/apps/blueprints/specs/index.spec.tsx +++ b/apps/blueprints/specs/index.spec.tsx @@ -1,9 +1,11 @@ import React from "react"; -import { render } from "@testing-library/react"; +import { act, render } from "@testing-library/react"; import Index from "../src/pages/index"; import * as nextRouter from "next/router"; const useRouter = jest.spyOn(nextRouter, "useRouter"); +(global as any).fetch = jest.fn(() => Promise.resolve()); +console.error = jest.fn(); describe("Index", () => { it("should render successfully", () => { diff --git a/apps/blueprints/src/hooks/fbe.hook.ts b/apps/blueprints/src/hooks/fbe.hook.ts index a3c3ef1..6cd249a 100644 --- a/apps/blueprints/src/hooks/fbe.hook.ts +++ b/apps/blueprints/src/hooks/fbe.hook.ts @@ -60,8 +60,12 @@ export const useFbeData = (): FactorioData | null => { useEffect(() => { (async () => { - const result = await fetch("/api/fbe-proxy/data.json").then((res) => res.json()); - setData(result); + const result = await fetch("/api/fbe-proxy/data.json") + .then((res) => res.json()) + .catch(() => { + console.error("Failed to fetch data.json"); + }); + if (result) setData(result); })(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); diff --git a/apps/blueprints/src/hooks/query.hook.ts b/apps/blueprints/src/hooks/query.hook.ts index acc3f41..ce9e69d 100644 --- a/apps/blueprints/src/hooks/query.hook.ts +++ b/apps/blueprints/src/hooks/query.hook.ts @@ -6,7 +6,7 @@ export const useRouterQueryToHref = () => { const router = useRouter(); return useCallback( (override: Record, overrideAll = false) => { - const query = overrideAll ? override : { ...router.query, ...override }; + const query = overrideAll ? override : { ...router?.query, ...override }; return stringifyQuery(query); }, [router] diff --git a/apps/blueprints/src/pages/index.tsx b/apps/blueprints/src/pages/index.tsx index 7e2e92c..7937ef7 100644 --- a/apps/blueprints/src/pages/index.tsx +++ b/apps/blueprints/src/pages/index.tsx @@ -83,7 +83,7 @@ export const Index: NextPage = ({ useEffect(() => { setSearchQuery((router.query.q as string) || ""); - }, [router.query.q]); + }, [router?.query.q]); if (!data) return null;