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 96a6c8f898..8646fbd0de 100644 --- a/editor.planx.uk/src/@planx/components/PlanningConstraints/Public.test.tsx +++ b/editor.planx.uk/src/@planx/components/PlanningConstraints/Public.test.tsx @@ -4,6 +4,7 @@ import React from "react"; import { act } from "react-dom/test-utils"; import { ErrorBoundary } from "react-error-boundary"; import swr from "swr"; +import useSWR from "swr"; import { setup } from "testUtils"; import { vi } from "vitest"; import { axe } from "vitest-axe"; @@ -21,15 +22,13 @@ const { setState } = useStore; beforeEach(() => vi.clearAllMocks()); -const swrMock = (swr as jest.Mock).mock; - vi.mock("swr", () => ({ default: vi.fn((url: () => string) => { const isGISRequest = url()?.startsWith( - `${import.meta.env.VITE_APP_API_URL}/gis` + `${import.meta.env.VITE_APP_API_URL}/gis`, ); const isRoadsRequest = url()?.startsWith( - `${import.meta.env.VITE_APP_API_URL}/roads` + `${import.meta.env.VITE_APP_API_URL}/roads`, ); if (isGISRequest) return { data: digitalLandResponseMock }; @@ -50,7 +49,7 @@ describe("error state", () => { disclaimer="This page does not include information about historic planning conditions that may apply to this property." handleSubmit={vi.fn()} /> - + , ); expect(getByTestId("error-summary-invalid-graph")).toBeInTheDocument(); @@ -66,7 +65,7 @@ describe("error state", () => { fn="property.constraints.planning" disclaimer="This page does not include information about historic planning conditions that may apply to this property." /> - + , ); const results = await axe(container); expect(results).toHaveNoViolations(); @@ -82,7 +81,7 @@ describe("following a FindProperty component", () => { teamIntegrations: { hasPlanningData: true, }, - }) + }), ); }); @@ -96,11 +95,11 @@ describe("following a FindProperty component", () => { fn="property.constraints.planning" disclaimer="This page does not include information about historic planning conditions that may apply to this property." handleSubmit={handleSubmit} - /> + />, ); expect( - getByRole("heading", { name: "Planning constraints" }) + getByRole("heading", { name: "Planning constraints" }), ).toBeInTheDocument(); await user.click(getByTestId("continue-button")); @@ -115,7 +114,7 @@ describe("following a FindProperty component", () => { description="Things that might affect your project" fn="property.constraints.planning" disclaimer="This page does not include information about historic planning conditions that may apply to this property." - /> + />, ); const results = await axe(container); expect(results).toHaveNoViolations(); @@ -129,14 +128,14 @@ describe("following a FindProperty component", () => { fn="property.constraints.planning" disclaimer="This page does not include information about historic planning conditions that may apply to this property." handleSubmit={vi.fn()} - /> + />, ); expect(swr).toHaveBeenCalled(); // Planning data is called first - const swrURL = swrMock.calls[0][0](); - const swrResponse = swrMock.results[0].value; + const swrURL = (vi.mocked(useSWR).mock.calls[0][0] as () => {})(); + const swrResponse = vi.mocked(useSWR).mock.results[0].value; expect(swrURL).toContain("/gis"); expect(swrResponse).toEqual({ data: digitalLandResponseMock }); @@ -150,14 +149,14 @@ describe("following a FindProperty component", () => { fn="property.constraints.planning" disclaimer="This page does not include information about historic planning conditions that may apply to this property." handleSubmit={vi.fn()} - /> + />, ); expect(swr).toHaveBeenCalled(); // Classified roads are called second - const swrURL = swrMock.calls[1][0](); - const swrResponse = swrMock.results[1].value; + const swrURL = (vi.mocked(useSWR).mock.calls[1][0] as () => {})(); + const swrResponse = vi.mocked(useSWR).mock.results[1].value; expect(swrURL).toContain("/roads"); expect(swrResponse).toEqual({ data: classifiedRoadsResponseMock }); @@ -171,7 +170,7 @@ describe("following a FindProperty component", () => { teamIntegrations: { hasPlanningData: true, }, - }) + }), ); setup( @@ -181,14 +180,16 @@ describe("following a FindProperty component", () => { fn="property.constraints.planning" disclaimer="This page does not include information about historic planning conditions that may apply to this property." handleSubmit={vi.fn()} - /> + />, ); expect(swr).toHaveBeenCalled(); // Planning constraints API still called - const planingConstraintsURL = swrMock.calls[0][0](); - const planingConstraintsResponse = swrMock.results[0].value; + const planingConstraintsURL = ( + vi.mocked(useSWR).mock.calls[0][0] as () => {} + )(); + const planingConstraintsResponse = vi.mocked(useSWR).mock.results[0].value; expect(planingConstraintsURL).toContain("/gis"); expect(planingConstraintsResponse).toEqual({ @@ -196,8 +197,8 @@ describe("following a FindProperty component", () => { }); // Classified roads API not called due to missing USRN - const swrURL = swrMock.calls[1][0](); - const swrResponse = swrMock.results[1].value; + const swrURL = (vi.mocked(useSWR).mock.calls[1][0] as () => {})(); + const swrResponse = vi.mocked(useSWR).mock.results[1].value; expect(swrURL).toBeNull(); expect(swrResponse).toEqual({ data: null }); @@ -211,12 +212,12 @@ describe("following a FindProperty component", () => { fn="property.constraints.planning" disclaimer="This page does not include information about historic planning conditions that may apply to this property." handleSubmit={vi.fn()} - /> + />, ); // Positive constraints visible by default expect( - getByRole("heading", { name: /These are the planning constraints/ }) + getByRole("heading", { name: /These are the planning constraints/ }), ).toBeVisible(); expect(getByRole("button", { name: /Parks and gardens/ })).toBeVisible(); @@ -227,7 +228,7 @@ describe("following a FindProperty component", () => { expect(showNegativeConstraintsButton).toBeVisible(); const negativeConstraintsContainer = getByTestId( - "negative-constraints-list" + "negative-constraints-list", ); expect(negativeConstraintsContainer).not.toBeVisible(); @@ -248,12 +249,12 @@ describe("following a FindProperty component", () => { description="Things that might affect your project" fn="property.constraints.planning" handleSubmit={vi.fn()} - /> + />, ); expect( queryByText( - "This page does not include information about historic planning conditions that may apply to this property." - ) + "This page does not include information about historic planning conditions that may apply to this property.", + ), ).toBeVisible(); }); }); @@ -268,7 +269,7 @@ describe("demo state", () => { hasPlanningData: false, }, teamSlug: "demo", - }) + }), ); }); it("should render an error when teamSlug is demo", async () => { @@ -282,21 +283,21 @@ describe("demo state", () => { disclaimer="This page does not include information about historic planning conditions that may apply to this property." handleSubmit={handleSubmit} /> - + , ); const errorMessage = queryByText( - "Planning Constraints are not enabled for demo users" + "Planning Constraints are not enabled for demo users", ); expect(errorMessage).toBeVisible(); // Check planning constraints has not rendered // reused positive constraints from basic layout test expect( - queryByRole("heading", { name: /These are the planning constraints/ }) + queryByRole("heading", { name: /These are the planning constraints/ }), ).not.toBeInTheDocument(); expect( - queryByRole("button", { name: /Parks and gardens/ }) + queryByRole("button", { name: /Parks and gardens/ }), ).not.toBeInTheDocument(); // Ensure a demo user can continue on in the application