Skip to content

Commit

Permalink
test: Add basic test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Feb 12, 2024
1 parent 0486c1f commit f25e37d
Showing 1 changed file with 113 additions and 4 deletions.
117 changes: 113 additions & 4 deletions editor.planx.uk/src/@planx/components/Review/Public/Public.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { act, screen } from "@testing-library/react";
import { act, screen, waitFor, within } from "@testing-library/react";
import { FullStore, vanillaStore } from "pages/FlowEditor/lib/store";
import React from "react";
import { axe, setup } from "testUtils";
Expand Down Expand Up @@ -34,6 +34,7 @@ beforeAll(() => (initialState = getState()));
describe("Simple flow", () => {
it("renders correctly", async () => {
const handleSubmit = jest.fn();
const changeAnswer = jest.fn();

const { user } = setup(
<Review
Expand All @@ -42,7 +43,7 @@ describe("Simple flow", () => {
flow={{}}
breadcrumbs={{}}
passport={{}}
changeAnswer={() => {}}
changeAnswer={changeAnswer}
handleSubmit={handleSubmit}
showChangeButton={true}
/>,
Expand All @@ -57,6 +58,7 @@ describe("Simple flow", () => {

it("doesn't return undefined when multiple nodes are filled", async () => {
const handleSubmit = jest.fn();
const changeAnswer = jest.fn();

setup(
<Review
Expand All @@ -65,7 +67,7 @@ describe("Simple flow", () => {
flow={mockedFlow}
breadcrumbs={mockedBreadcrumbs}
passport={mockedPassport}
changeAnswer={() => {}}
changeAnswer={changeAnswer}
handleSubmit={handleSubmit}
showChangeButton={true}
/>,
Expand All @@ -78,20 +80,127 @@ describe("Simple flow", () => {
});

it("should not have any accessibility violations", async () => {
const changeAnswer = jest.fn();

const { container } = setup(
<Review
title="Review"
description="Check your answers before submitting"
flow={mockedFlow}
breadcrumbs={mockedBreadcrumbs}
passport={mockedPassport}
changeAnswer={() => {}}
changeAnswer={changeAnswer}
showChangeButton={true}
/>,
);
const results = await axe(container);
expect(results).toHaveNoViolations();
});

it("opens a 'confirm' dialog on change", async () => {
const handleSubmit = jest.fn();
const changeAnswer = jest.fn();

setup(
<Review
title="Review"
description="Check your answers before submitting"
flow={mockedFlow}
breadcrumbs={mockedBreadcrumbs}
passport={mockedPassport}
changeAnswer={changeAnswer}
handleSubmit={handleSubmit}
showChangeButton={true}
/>,
);

// Dialog not shown
expect(
within(document.body).queryByTestId("confirmation-dialog"),
).not.toBeInTheDocument();

// Click "change"
act(() =>
screen
.getAllByRole("button", { name: "Change Input a number" })[0]
.click(),
);

// Dialog shown to user
expect(
within(document.body).queryByTestId("confirmation-dialog"),
).toBeVisible();
});

it("selecting 'no' closes the dialog and does not make a change", async () => {
const handleSubmit = jest.fn();
const changeAnswer = jest.fn();

setup(
<Review
title="Review"
description="Check your answers before submitting"
flow={mockedFlow}
breadcrumbs={mockedBreadcrumbs}
passport={mockedPassport}
changeAnswer={changeAnswer}
handleSubmit={handleSubmit}
showChangeButton={true}
/>,
);

act(() =>
screen
.getAllByRole("button", { name: "Change Input a number" })[0]
.click(),
);
act(() => screen.getAllByRole("button", { name: "No" })[0].click());

// Modal closed
await waitFor(() =>
expect(
within(document.body).queryByTestId("confirmation-dialog"),
).not.toBeInTheDocument(),
);

// Change not made
expect(changeAnswer).not.toHaveBeenCalled();
});

it("selecting 'yes' closes the dialog and does make change", async () => {
const handleSubmit = jest.fn();
const changeAnswer = jest.fn();

setup(
<Review
title="Review"
description="Check your answers before submitting"
flow={mockedFlow}
breadcrumbs={mockedBreadcrumbs}
passport={mockedPassport}
changeAnswer={changeAnswer}
handleSubmit={handleSubmit}
showChangeButton={true}
/>,
);

act(() =>
screen
.getAllByRole("button", { name: "Change Input a number" })[0]
.click(),
);
act(() => screen.getAllByRole("button", { name: "Yes" })[0].click());

// Modal closed
await waitFor(() =>
expect(
within(document.body).queryByTestId("confirmation-dialog"),
).not.toBeInTheDocument(),
);

// Change made
expect(changeAnswer).toHaveBeenCalled();
});
});

describe("File uploads", () => {
Expand Down

0 comments on commit f25e37d

Please sign in to comment.