Skip to content

Commit

Permalink
test: Uniform routing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Oct 24, 2024
1 parent b6fa0c0 commit 2ad11e1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
78 changes: 75 additions & 3 deletions editor.planx.uk/src/@planx/components/Send/Public.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
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";
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)
*/
Expand All @@ -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", () => {
Expand Down Expand Up @@ -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<SendIntegration, unknown>)[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(
<SendComponent
title="Send"
destinations={["bops", "uniform"]}
/>,
);

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(
<SendComponent
title="Send"
destinations={["bops", "uniform"]}
/>,
);

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();

Expand Down
2 changes: 1 addition & 1 deletion editor.planx.uk/src/@planx/components/Send/Public.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const CreateSendEvents: React.FC<Props> = ({
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`,
Expand Down
27 changes: 27 additions & 0 deletions editor.planx.uk/src/@planx/components/Send/mocks/simpleFlow.ts
Original file line number Diff line number Diff line change
@@ -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,
},
};

0 comments on commit 2ad11e1

Please sign in to comment.