Skip to content

Commit

Permalink
test: Coverage for Send component
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Oct 24, 2024
1 parent d2bf078 commit b6fa0c0
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 13 deletions.
143 changes: 132 additions & 11 deletions editor.planx.uk/src/@planx/components/Send/Public.test.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,151 @@
import { SendIntegration } from "@opensystemslab/planx-core/types";
import { waitFor } from "@testing-library/react";
import axios from "axios";
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 SendComponent from "./Public";

/**
* Adds a small tick to allow MUI to render (e.g. card transitions)
*/
const tick = () => act(async () => {
await new Promise(resolve => setTimeout(resolve, 0));
});

vi.mock("axios");
const mockAxios = vi.mocked(axios, true);

mockAxios.post.mockResolvedValue(async (url: string) => {
return {
value: url.startsWith(
`${import.meta.env.VITE_APP_API_URL}/create-send-events/`,
)
? hasuraEventsResponseMock
: null,
};
mockAxios.post.mockResolvedValue({
data: hasuraEventsResponseMock,
status: 200,
statusText: "OK",
});

const originalLocation = window.location.pathname;

afterEach(() => {
vi.clearAllMocks();
window.history.pushState({}, "", originalLocation);
});

it.todo("renders correctly");
it("displays a warning at /draft URLs", () => {
window.history.pushState({}, "", "/draft");
const { getByText } = setup(
<SendComponent
title="Send"
destinations={["bops", "uniform"]}
/>,
);

expect(getByText(/You can only test submissions on/)).toBeVisible();
});

it("displays a warning at /preview URLs", () => {
window.history.pushState({}, "", "/preview");
const { getByText } = setup(
<SendComponent
title="Send"
destinations={["bops", "uniform"]}
/>,
);

expect(getByText(/You can only test submissions on/)).toBeVisible();
});

it("displays loading messages to the user", async () => {
let resolvePromise: (value: any) => void;
const promise = new Promise((resolve) => {
resolvePromise = resolve;
});

mockAxios.post.mockImplementationOnce(() => promise);

const { getByText } = setup(
<SendComponent
title="Send"
destinations={["bops", "uniform"]}
/>,
);

await tick();

// Initial loading state
expect(getByText(/Submitting your application.../)).toBeVisible();

// Trigger mock API response
resolvePromise!({
data: hasuraEventsResponseMock,
status: 200,
statusText: "OK",
});

expect(mockAxios.post).toHaveBeenCalledTimes(1);

await tick();

it.todo("sets :localAuthority API param correctly based on team or passport");
// Final submission state
expect(getByText(/Finalising your submission.../)).toBeVisible();
});

it("calls the /create-send-event endpoint", async () => {
setup(
<SendComponent
title="Send"
destinations={["bops", "uniform"]}
/>,
);

await waitFor(() => expect(mockAxios.post).toHaveBeenCalledTimes(1));

expect(mockAxios.post).toHaveBeenCalledTimes(1);
});

it("generates a valid payload for the API", async () => {
const destinations: SendIntegration[] = ["bops", "uniform"];

setup(
<SendComponent
title="Send"
destinations={destinations}
/>,
);

await waitFor(() => expect(mockAxios.post).toHaveBeenCalledTimes(1));

const apiPayload = mockAxios.post.mock.calls[0][1];

destinations.forEach(destination =>
expect(apiPayload).toHaveProperty(destination)
);
});

it("generates a valid breadcrumb", async () => {
const handleSubmit = vi.fn();

setup(
<SendComponent
title="Send"
destinations={["bops", "uniform"]}
handleSubmit={handleSubmit}
/>,
);

await waitFor(() => expect(mockAxios.post).toHaveBeenCalledTimes(1));
expect(handleSubmit).toHaveBeenCalledTimes(1);

const breadcrumb = handleSubmit.mock.calls[0][0];

expect(breadcrumb.data).toEqual(expect.objectContaining({
bopsSendEventId: hasuraEventsResponseMock.bops.event_id,
uniformSendEventId: hasuraEventsResponseMock.uniform.event_id,
}));
});

// TODO: Turn this test back on when Uniform payload generation is moved to API
it("should not have any accessibility violations", async () => {
const { container } = setup(
<SendComponent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default {
bops: {
event_id: 1,
event_id: "1",
},
uniform: {
event_id: 2,
event_id: "2",
},
};

0 comments on commit b6fa0c0

Please sign in to comment.