From 2ad11e1d38cba1f0dc1993ad385e44a0d341bc9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Thu, 24 Oct 2024 09:13:03 +0100 Subject: [PATCH] test: Uniform routing coverage --- .../@planx/components/Send/Public.test.tsx | 78 ++++++++++++++++++- .../src/@planx/components/Send/Public.tsx | 2 +- .../components/Send/mocks/simpleFlow.ts | 27 +++++++ 3 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 editor.planx.uk/src/@planx/components/Send/mocks/simpleFlow.ts diff --git a/editor.planx.uk/src/@planx/components/Send/Public.test.tsx b/editor.planx.uk/src/@planx/components/Send/Public.test.tsx index 89caf632be..3bbf372dea 100644 --- a/editor.planx.uk/src/@planx/components/Send/Public.test.tsx +++ b/editor.planx.uk/src/@planx/components/Send/Public.test.tsx @@ -1,6 +1,7 @@ import { SendIntegration } from "@opensystemslab/planx-core/types"; import { waitFor } from "@testing-library/react"; import axios from "axios"; +import { FullStore, useStore } from "pages/FlowEditor/lib/store"; import React from "react"; import { act } from "react-dom/test-utils"; import { setup } from "testUtils"; @@ -8,8 +9,13 @@ import { vi } from "vitest"; import { axe } from "vitest-axe"; import hasuraEventsResponseMock from "./mocks/hasuraEventsResponseMock"; +import { flow } from "./mocks/simpleFlow"; import SendComponent from "./Public"; +const { getState, setState } = useStore; + +let initialState: FullStore; + /** * Adds a small tick to allow MUI to render (e.g. card transitions) */ @@ -28,9 +34,14 @@ mockAxios.post.mockResolvedValue({ const originalLocation = window.location.pathname; +beforeAll(() => (initialState = getState())); + +beforeEach(() => (act(() => setState({ teamSlug: "testTeam" })))); + afterEach(() => { vi.clearAllMocks(); window.history.pushState({}, "", originalLocation); + act(() => setState(initialState)) }); it("displays a warning at /draft URLs", () => { @@ -119,11 +130,72 @@ it("generates a valid payload for the API", async () => { const apiPayload = mockAxios.post.mock.calls[0][1]; - destinations.forEach(destination => - expect(apiPayload).toHaveProperty(destination) - ); + destinations.forEach(destination => { + expect(apiPayload).toHaveProperty(destination); + expect((apiPayload as Record)[destination]).toHaveProperty("localAuthority", "testTeam"); + }); }); +describe("Uniform overrides for Buckinghamshire", () => { + it("converts property.localAuthorityDistrict to the correct format", async () => { + act(() => setState({ + teamSlug: "buckinghamshire", + flow, + breadcrumbs: { + findProperty: { + data: { + "property.localAuthorityDistrict": ["Some local authority district"] + } + } + } + })); + + setup( + , + ); + + await waitFor(() => expect(mockAxios.post).toHaveBeenCalledTimes(1)); + + const apiPayload = mockAxios.post.mock.calls[0][1] as any; + + // BOPS event not modified + expect(apiPayload?.bops?.localAuthority).toEqual("buckinghamshire"); + + // Uniform event has read property.localAuthorityDistrict from the passport + expect(apiPayload?.uniform?.localAuthority).toEqual("some-local-authority-district"); + }); + + it("maps requests for South Bucks to Chiltern", async () => { + act(() => setState({ + teamSlug: "buckinghamshire", + flow, + breadcrumbs: { + findProperty: { + data: { + "property.localAuthorityDistrict": ["South Bucks"] + } + } + } + })); + + setup( + , + ); + + await waitFor(() => expect(mockAxios.post).toHaveBeenCalledTimes(1)); + + const apiPayload = mockAxios.post.mock.calls[0][1] as any; + + expect(apiPayload?.uniform?.localAuthority).toEqual("chiltern"); + }); +}) + it("generates a valid breadcrumb", async () => { const handleSubmit = vi.fn(); diff --git a/editor.planx.uk/src/@planx/components/Send/Public.tsx b/editor.planx.uk/src/@planx/components/Send/Public.tsx index 75c39ddf27..3e1ceedc93 100644 --- a/editor.planx.uk/src/@planx/components/Send/Public.tsx +++ b/editor.planx.uk/src/@planx/components/Send/Public.tsx @@ -84,7 +84,7 @@ const CreateSendEvents: React.FC = ({ const isReady = !loading && !error && value; if (!isReady) return; - // Construct breadcrumb containing event IDs of each send event generated + // Construct breadcrumb containing IDs of each send event generated const data = Object.fromEntries( destinations.map(destination => [ `${destination}SendEventId`, diff --git a/editor.planx.uk/src/@planx/components/Send/mocks/simpleFlow.ts b/editor.planx.uk/src/@planx/components/Send/mocks/simpleFlow.ts new file mode 100644 index 0000000000..2199b4b9db --- /dev/null +++ b/editor.planx.uk/src/@planx/components/Send/mocks/simpleFlow.ts @@ -0,0 +1,27 @@ +import { Graph } from "@planx/graph"; + +export const flow: Graph = { + _root: { + edges: ["findProperty", "send"], + }, + send: { + data: { + tags: [], + title: "Send", + destinations: ["bops", "uniform"], + }, + type: 650, + }, + findProperty: { + data: { + title: "Find the property", + newAddressTitle: + "Click or tap at where the property is on the map and name it below", + allowNewAddresses: false, + newAddressDescription: + "You will need to select a location and provide a name to continue", + newAddressDescriptionLabel: "Name the site", + }, + type: 9, + }, +}; \ No newline at end of file