From 944c297d19803af09756b23112c7501134aece87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Tue, 17 Sep 2024 20:51:21 +0100 Subject: [PATCH] test: Fix failing tests which by adding error boundaries --- .../PlanningConstraints/Public.test.tsx | 74 ++++++++++--------- .../PropertyInformation/Public.test.tsx | 12 ++- .../src/components/Error/GraphError.tsx | 4 +- 3 files changed, 50 insertions(+), 40 deletions(-) diff --git a/editor.planx.uk/src/@planx/components/PlanningConstraints/Public.test.tsx b/editor.planx.uk/src/@planx/components/PlanningConstraints/Public.test.tsx index 2eefae94fe..256dff7063 100644 --- a/editor.planx.uk/src/@planx/components/PlanningConstraints/Public.test.tsx +++ b/editor.planx.uk/src/@planx/components/PlanningConstraints/Public.test.tsx @@ -1,5 +1,6 @@ -import { screen } from "@testing-library/react"; +import ErrorFallback from "components/Error/ErrorFallback"; import React from "react"; +import { ErrorBoundary } from "react-error-boundary"; import { setup } from "testUtils"; import { vi } from "vitest"; import { axe } from "vitest-axe"; @@ -9,7 +10,7 @@ import digitalLandResponseMock from "./mocks/digitalLandResponseMock"; import PlanningConstraints from "./Public"; vi.mock("swr", () => ({ - default: vi.fn((url: any) => { + default: vi.fn((url: () => string) => { const isGISRequest = url()?.startsWith( `${import.meta.env.VITE_APP_API_URL}/gis/`, ); @@ -17,47 +18,54 @@ vi.mock("swr", () => ({ `${import.meta.env.VITE_APP_API_URL}/roads/`, ); - if (isGISRequest) return { data: digitalLandResponseMock } as any; - if (isRoadsRequest) return { data: classifiedRoadsResponseMock } as any; + if (isGISRequest) return { data: digitalLandResponseMock }; + if (isRoadsRequest) return { data: classifiedRoadsResponseMock }; return { data: null }; }), })); -it("renders correctly", async () => { - const handleSubmit = vi.fn(); +describe("error state", () => { + it("renders an error if no addres is present in the passport", async () => { + const handleSubmit = vi.fn(); - const { user } = setup( - , - ); - - expect(screen.getByText("Planning constraints")).toBeInTheDocument(); + const { getByRole, getByTestId } = setup( + + + , + , + ); - // TODO mock passport _address so that SWR request is actually triggered to return mock response - expect(screen.getByTestId("error-summary-invalid-graph")).toBeInTheDocument(); + expect(getByTestId("error-summary-invalid-graph")).toBeInTheDocument(); + expect(getByRole("heading", { name: "Invalid graph" })).toBeInTheDocument(); + }); - await user.click(screen.getByTestId("continue-button")); - expect(handleSubmit).toHaveBeenCalledTimes(1); + it("should not have any accessibility violations", async () => { + const { container } = setup( + + + , + , + ); + const results = await axe(container); + expect(results).toHaveNoViolations(); + }); }); -it("should not have any accessibility violations", async () => { - const { container } = setup( - , - ); - const results = await axe(container); - expect(results).toHaveNoViolations(); -}); +it.todo("renders correctly"); + +it.todo("should not have any accessibility violations"); it.todo("fetches classified roads only when we have a siteBoundary"); // using expect(spy).toHaveBeenCalled() ?? diff --git a/editor.planx.uk/src/@planx/components/PropertyInformation/Public.test.tsx b/editor.planx.uk/src/@planx/components/PropertyInformation/Public.test.tsx index 318aaf37f2..2e35f47189 100644 --- a/editor.planx.uk/src/@planx/components/PropertyInformation/Public.test.tsx +++ b/editor.planx.uk/src/@planx/components/PropertyInformation/Public.test.tsx @@ -1,6 +1,8 @@ import { MockedProvider } from "@apollo/client/testing"; import { screen } from "@testing-library/react"; +import ErrorFallback from "components/Error/ErrorFallback"; import React from "react"; +import { ErrorBoundary } from "react-error-boundary"; import { setup } from "testUtils"; import { vi } from "vitest"; @@ -18,10 +20,12 @@ const defaultPresentationalProps: PresentationalProps = { test("renders a warning for editors if address data is not in state", async () => { setup( - + + + , ); diff --git a/editor.planx.uk/src/components/Error/GraphError.tsx b/editor.planx.uk/src/components/Error/GraphError.tsx index 91afdb93c4..2b86e7aaef 100644 --- a/editor.planx.uk/src/components/Error/GraphError.tsx +++ b/editor.planx.uk/src/components/Error/GraphError.tsx @@ -1,6 +1,5 @@ import Typography from "@mui/material/Typography"; import Card from "@planx/components/shared/Preview/Card"; -import CardHeader from "@planx/components/shared/Preview/CardHeader"; import { ErrorSummaryContainer } from "@planx/components/shared/Preview/ErrorSummaryContainer"; import React from "react"; @@ -29,12 +28,11 @@ export const GraphErrorComponent: React.FC<{ error: GraphError }> = ({ error, }) => ( - - + Invalid graph