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 b129676048..86504baec4 100644 --- a/editor.planx.uk/src/@planx/components/PlanningConstraints/Public.test.tsx +++ b/editor.planx.uk/src/@planx/components/PlanningConstraints/Public.test.tsx @@ -10,9 +10,13 @@ import { axe } from "vitest-axe"; import classifiedRoadsResponseMock from "./mocks/classifiedRoadsResponseMock"; import digitalLandResponseMock from "./mocks/digitalLandResponseMock"; -import { simpleBreadcrumbs, simpleFlow } from "./mocks/simpleFlow"; +import { breadcrumbsWithoutUSRN, simpleBreadcrumbs, simpleFlow } from "./mocks/simpleFlow"; import PlanningConstraints from "./Public"; +const { setState } = useStore; + +beforeEach(() => vi.clearAllMocks()); + const swrMock = (swr as jest.Mock).mock; vi.mock("swr", () => ({ @@ -66,9 +70,9 @@ describe("error state", () => { }); describe("following a FindProperty component", () => { - beforeAll(() => { + beforeEach(() => { act(() => - useStore.setState({ + setState({ breadcrumbs: simpleBreadcrumbs, flow: simpleFlow, teamIntegrations: { @@ -134,7 +138,7 @@ describe("following a FindProperty component", () => { expect(swrResponse).toEqual({ data: digitalLandResponseMock }); }); - it("fetches classified roads only when we have a siteBoundary", () => { + it("fetches classified roads when a USRN is provided", () => { setup( { expect(swrResponse).toEqual({ data: classifiedRoadsResponseMock }); }); + it("does not fetch classified roads when a USRN is not provided", async () => { + act(() => + setState({ + breadcrumbs: breadcrumbsWithoutUSRN, + flow: simpleFlow, + teamIntegrations: { + hasPlanningData: true, + }, + }) + ); + + setup( + , + ); + + expect(swr).toHaveBeenCalled(); + + // Planning constraints API still called + const planingConstraintsURL = swrMock.calls[0][0](); + const planingConstraintsResponse = swrMock.results[0].value; + + expect(planingConstraintsURL).toContain("/gis"); + expect(planingConstraintsResponse).toEqual({ data: digitalLandResponseMock }); + + // Classified roads API not called due to missing USRN + const swrURL = swrMock.calls[1][0](); + const swrResponse = swrMock.results[1].value; + + expect(swrURL).toBeNull(); + expect(swrResponse).toEqual({ data: null }); + }); + test("basic layout and interactions", async () => { const { user, getByRole, queryByRole, getByTestId } = setup( { expect(negativeConstraintsContainer).toBeVisible(); expect(getByRole("heading", { name: /Ecology/ })).toBeVisible(); }); -}); +}); \ No newline at end of file diff --git a/editor.planx.uk/src/@planx/components/PlanningConstraints/mocks/simpleFlow.ts b/editor.planx.uk/src/@planx/components/PlanningConstraints/mocks/simpleFlow.ts index ed015395e8..ae7a984bea 100644 --- a/editor.planx.uk/src/@planx/components/PlanningConstraints/mocks/simpleFlow.ts +++ b/editor.planx.uk/src/@planx/components/PlanningConstraints/mocks/simpleFlow.ts @@ -1,3 +1,4 @@ +import { cloneDeep, merge } from "lodash"; import { Store } from "pages/FlowEditor/lib/store"; export const simpleFlow: Store.Flow = { @@ -93,3 +94,5 @@ export const simpleBreadcrumbs: Store.Breadcrumbs = { }, }, }; + +export const breadcrumbsWithoutUSRN = merge(cloneDeep(simpleBreadcrumbs), { findProperty: { data: { _address: { usrn: null }}}}); \ No newline at end of file