From c93091f804fdd5cc4fbd277ec1e31c6a3c726521 Mon Sep 17 00:00:00 2001 From: rare-magma Date: Sat, 23 Dec 2023 14:39:05 +0100 Subject: [PATCH] test: update tests after refactor Signed-off-by: rare-magma --- .vscode/settings.json | 6 +- src/App.test.tsx | 4 +- src/components/ErrorModal/ErrorModal.test.tsx | 61 +++++---------- .../__snapshots__/ErrorModal.test.tsx.snap | 32 +------- .../LandingPage/LandingPage.test.tsx | 36 +++------ .../__snapshots__/LandingPage.test.tsx.snap | 20 +---- src/components/NavBar/NavBar.test.tsx | 49 ++---------- .../NavBar/__snapshots__/NavBar.test.tsx.snap | 13 +--- .../Notification/Notification.test.tsx | 15 ++-- .../__snapshots__/Notification.test.tsx.snap | 1 - src/context/GeneralContext.test.tsx | 32 ++++++++ src/hooks/useDB.ts | 5 -- src/setupTests.ts | 78 ++++++++++++++++++- src/utils.test.ts | 16 ++++ 14 files changed, 173 insertions(+), 195 deletions(-) create mode 100644 src/context/GeneralContext.test.tsx diff --git a/.vscode/settings.json b/.vscode/settings.json index 4a8edef..1720134 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,6 +6,6 @@ "typescriptreact" ], "editor.codeActionsOnSave": { - "source.fixAll.eslint": true, - }, -} \ No newline at end of file + "source.fixAll.eslint": "explicit" + } +} diff --git a/src/App.test.tsx b/src/App.test.tsx index d1baba9..e997c31 100644 --- a/src/App.test.tsx +++ b/src/App.test.tsx @@ -16,7 +16,9 @@ describe("App", () => { budgetContextSpy.mockReturnValue(testEmptyBudgetContext); render(comp); expect(screen.getAllByText("guitos")[0]).toBeInTheDocument(); - expect(screen.getByRole("status")).toBeInTheDocument(); + expect( + screen.getByRole("link", { name: /open guitos changelog/i }), + ).toBeInTheDocument(); expect(budgetsDB.config("name")).toBe("guitos"); expect(budgetsDB.config("storeName")).toBe("budgets"); expect(optionsDB.config("name")).toBe("guitos"); diff --git a/src/components/ErrorModal/ErrorModal.test.tsx b/src/components/ErrorModal/ErrorModal.test.tsx index 395d357..6779d92 100644 --- a/src/components/ErrorModal/ErrorModal.test.tsx +++ b/src/components/ErrorModal/ErrorModal.test.tsx @@ -1,50 +1,26 @@ import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; -import { vi } from "vitest"; +import { + generalContextSpy, + testCsvErrorGeneralContext, + testErrorGeneralContext, + testJsonErrorGeneralContext, +} from "../../setupTests"; import { ErrorModal } from "./ErrorModal"; describe("ErrorModal", () => { - const onError = vi.fn(); - const onShow = vi.fn(); - - const comp = ( - - ); + const comp = ; beforeEach(() => { + generalContextSpy.mockReturnValue(testJsonErrorGeneralContext); render(comp); }); it("matches snapshot", () => { expect(comp).toMatchSnapshot(); }); + it("renders initial state", () => { - expect(screen.getByText("Thrown error")).toBeInTheDocument(); expect( screen.getAllByText("Errors found while importing:")[0], ).toBeInTheDocument(); @@ -56,37 +32,36 @@ describe("ErrorModal", () => { }); it("closes error when clicking the button", async () => { + generalContextSpy.mockClear(); + generalContextSpy.mockReturnValue(testErrorGeneralContext); + render(comp); expect(screen.getByTestId("error-modal")).toBeInTheDocument(); await userEvent.click(screen.getByTestId("error-modal-dismiss")); - - expect(onShow).toHaveBeenCalledWith(false); }); it("closes json error when clicking the button", async () => { expect(screen.getByTestId("json-error-close")).toBeInTheDocument(); await userEvent.click(screen.getByTestId("json-error-close")); - - expect(onShow).toHaveBeenCalledWith(false); }); it("closes json error modal when clicking the button", async () => { expect(screen.getByTestId("json-error-modal")).toBeInTheDocument(); await userEvent.click(screen.getByTestId("json-error-modal")); - - expect(onShow).toHaveBeenCalledWith(false); }); it("closes csv error when clicking the button", async () => { + generalContextSpy.mockReturnValue(testCsvErrorGeneralContext); + render(comp); + expect(screen.getByTestId("csv-error-close")).toBeInTheDocument(); await userEvent.click(screen.getByTestId("csv-error-close")); - - expect(onShow).toHaveBeenCalledWith(false); }); it("closes csv error modal when clicking the button", async () => { + generalContextSpy.mockReturnValue(testCsvErrorGeneralContext); + render(comp); + expect(screen.getByTestId("csv-error-modal")).toBeInTheDocument(); await userEvent.click(screen.getByTestId("csv-error-modal")); - - expect(onShow).toHaveBeenCalledWith(false); }); }); diff --git a/src/components/ErrorModal/__snapshots__/ErrorModal.test.tsx.snap b/src/components/ErrorModal/__snapshots__/ErrorModal.test.tsx.snap index 669a33d..b5f568c 100644 --- a/src/components/ErrorModal/__snapshots__/ErrorModal.test.tsx.snap +++ b/src/components/ErrorModal/__snapshots__/ErrorModal.test.tsx.snap @@ -1,33 +1,3 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`ErrorModal > matches snapshot 1`] = ` - -`; +exports[`ErrorModal > matches snapshot 1`] = ``; diff --git a/src/components/LandingPage/LandingPage.test.tsx b/src/components/LandingPage/LandingPage.test.tsx index 5005327..cee6d69 100644 --- a/src/components/LandingPage/LandingPage.test.tsx +++ b/src/components/LandingPage/LandingPage.test.tsx @@ -1,25 +1,16 @@ import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; -import { vi } from "vitest"; import { budgetContextSpy, + generalContextSpy, testBudget, testEmptyBudgetContext, + testGeneralContext, } from "../../setupTests"; import { LandingPage } from "./LandingPage"; describe("LandingPage", () => { - const inputRefMock: { current: HTMLInputElement | null } = { current: null }; - const onNew = vi.fn(); - const onImport = vi.fn(); - const comp = ( - - ); + const comp = ; beforeEach(() => { budgetContextSpy.mockReturnValue(testEmptyBudgetContext); @@ -38,18 +29,16 @@ describe("LandingPage", () => { ).toBeInTheDocument(); }); - it("triggers onNew", async () => { + it("triggers new budget", async () => { const newButton = screen.getAllByRole("button", { name: "new budget" })[0]; await userEvent.click(newButton); - expect(onNew).toHaveBeenCalledTimes(1); }); - it("triggers onImport", async () => { + it("triggers upload", async () => { await userEvent.upload( screen.getByTestId("import-form-control-landing-page"), - testBudget as unknown as File, + new File([testBudget as unknown as Blob], "test"), ); - expect(onImport).toHaveBeenCalledTimes(1); }); it("opens instructions in new tab", async () => { @@ -64,14 +53,11 @@ describe("LandingPage", () => { }); it("renders loading spinner", () => { - render( - , - ); + generalContextSpy.mockReturnValue({ + ...testGeneralContext, + loadingFromDB: true, + }); + render(); expect(screen.getByRole("status")).toBeInTheDocument(); }); }); diff --git a/src/components/LandingPage/__snapshots__/LandingPage.test.tsx.snap b/src/components/LandingPage/__snapshots__/LandingPage.test.tsx.snap index dd6ea8d..799568a 100644 --- a/src/components/LandingPage/__snapshots__/LandingPage.test.tsx.snap +++ b/src/components/LandingPage/__snapshots__/LandingPage.test.tsx.snap @@ -1,21 +1,3 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`LandingPage > matches snapshot 1`] = ` -