-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(page): Handle submit and "back" functionality #3678
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
"packageManager": "[email protected]", | ||
"dependencies": { | ||
"@cucumber/cucumber": "^9.3.0", | ||
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#bea2192", | ||
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#d47553e", | ||
"axios": "^1.7.4", | ||
"dotenv": "^16.3.1", | ||
"dotenv-expand": "^10.0.0", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,191 @@ | ||
test.todo("coming soon!"); | ||
import React from "react"; | ||
import { setup } from "testUtils"; | ||
import { vi } from "vitest"; | ||
import { axe } from "vitest-axe"; | ||
|
||
import PageComponent from "./Public"; | ||
import { ProposedAdvertisements } from "./schema/AdvertConsent"; | ||
|
||
it("renders correctly", async () => { | ||
const { getByRole } = setup( | ||
<PageComponent | ||
handleSubmit={vi.fn()} | ||
schema={ProposedAdvertisements} | ||
fn="testFn" | ||
schemaName="Proposed advertisements" | ||
title="Tell us about your proposed advertisements" | ||
/>, | ||
); | ||
|
||
const title = getByRole("heading", { | ||
level: 1, | ||
name: "Tell us about your proposed advertisements", | ||
}); | ||
expect(title).toBeVisible(); | ||
}); | ||
|
||
it("has no accessibility violations", async () => { | ||
const { container } = setup( | ||
<PageComponent | ||
handleSubmit={vi.fn()} | ||
schema={ProposedAdvertisements} | ||
fn="testFn" | ||
schemaName="Proposed advertisements" | ||
title="Tell us about your proposed advertisements" | ||
/>, | ||
); | ||
|
||
const results = await axe(container); | ||
expect(results).toHaveNoViolations(); | ||
}); | ||
|
||
it("displays the supplied Page schema", () => { | ||
const { getByLabelText } = setup( | ||
<PageComponent | ||
handleSubmit={vi.fn()} | ||
schema={ProposedAdvertisements} | ||
fn="testFn" | ||
schemaName="Proposed advertisements" | ||
title="Tell us about your proposed advertisements" | ||
/>, | ||
); | ||
|
||
// Each PageSchema field is displayed | ||
ProposedAdvertisements.fields.forEach(({ data }) => { | ||
const input = getByLabelText(data.title); | ||
expect(input).toBeVisible(); | ||
}); | ||
}); | ||
|
||
it("handles PageSchema errors", async () => { | ||
const handleSubmit = vi.fn(); | ||
const { | ||
user, | ||
queryAllByTestId, | ||
getAllByTestId, | ||
getByTestId, | ||
queryByText, | ||
getAllByText, | ||
} = setup( | ||
<PageComponent | ||
handleSubmit={handleSubmit} | ||
schema={ProposedAdvertisements} | ||
fn="testFn" | ||
schemaName="Proposed advertisements" | ||
title="Tell us about your proposed advertisements" | ||
/>, | ||
); | ||
|
||
// No error messages present initially | ||
let errorMessages = queryAllByTestId(/error-message-input-number/); | ||
expect(queryByText("Enter your answer before continuing")).toBeNull(); | ||
errorMessages.forEach((message) => expect(message).toBeEmptyDOMElement()); | ||
|
||
// User hits "Continue" without populating fields | ||
const continueButton = getByTestId("continue-button"); | ||
await user.click(continueButton); | ||
|
||
// Error messages present, per field | ||
errorMessages = getAllByTestId(/error-message-input-number/); | ||
errorMessages.forEach((message) => expect(message).not.toBeEmptyDOMElement()); | ||
expect(getAllByText("Enter your answer before continuing")).toHaveLength( | ||
ProposedAdvertisements.fields.length, | ||
); | ||
|
||
// Due to errors, component has not submitted data | ||
expect(handleSubmit).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("submits a valid payload", async () => { | ||
const handleSubmit = vi.fn(); | ||
const { user, getAllByLabelText, getByTestId } = setup( | ||
<PageComponent | ||
handleSubmit={handleSubmit} | ||
schema={ProposedAdvertisements} | ||
fn="testFn" | ||
schemaName="Proposed advertisements" | ||
title="Tell us about your proposed advertisements" | ||
/>, | ||
); | ||
|
||
const inputs = getAllByLabelText(/How many/); | ||
expect(inputs).toHaveLength(ProposedAdvertisements.fields.length); | ||
|
||
// Populate each field | ||
for (const input of inputs) { | ||
await user.type(input, "1"); | ||
} | ||
|
||
const continueButton = getByTestId("continue-button"); | ||
await user.click(continueButton); | ||
|
||
expect(handleSubmit).toHaveBeenCalled(); | ||
|
||
const expectedFlattenedData = expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
"testFn.fascia": 1, | ||
"testFn.hoarding": 1, | ||
"testFn.other": 1, | ||
"testFn.projecting": 1, | ||
}), | ||
}); | ||
expect(handleSubmit).toHaveBeenCalledWith(expectedFlattenedData); | ||
|
||
const expectedDefaultData = expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
testFn: [ | ||
{ | ||
fascia: 1, | ||
hoarding: 1, | ||
other: 1, | ||
projecting: 1, | ||
}, | ||
], | ||
}), | ||
}); | ||
expect(handleSubmit).toHaveBeenCalledWith(expectedDefaultData); | ||
}); | ||
|
||
it("handles back navigation", () => { | ||
const previousData = { | ||
data: { | ||
testFn: [ | ||
{ | ||
fascia: 1, | ||
hoarding: 2, | ||
other: 3, | ||
projecting: 4, | ||
}, | ||
], | ||
"testFn.fascia": 1, | ||
"testFn.hoarding": 2, | ||
"testFn.other": 3, | ||
"testFn.projecting": 4, | ||
}, | ||
}; | ||
|
||
const handleSubmit = vi.fn(); | ||
const { getByLabelText } = setup( | ||
<PageComponent | ||
handleSubmit={handleSubmit} | ||
schema={ProposedAdvertisements} | ||
fn="testFn" | ||
schemaName="Proposed advertisements" | ||
title="Tell us about your proposed advertisements" | ||
previouslySubmittedData={previousData} | ||
/>, | ||
); | ||
|
||
// Each input is correctly pre-populated | ||
const fasciaInput = getByLabelText(/fascia/); | ||
expect(fasciaInput).toHaveValue(1); | ||
|
||
const hoardingInput = getByLabelText(/hoarding/); | ||
expect(hoardingInput).toHaveValue(2); | ||
|
||
const otherInput = getByLabelText(/other/); | ||
expect(otherInput).toHaveValue(3); | ||
|
||
const projectingInput = getByLabelText(/projecting/); | ||
expect(projectingInput).toHaveValue(4); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As with the
List
component, we're preversing both forms of data here as opposed to writing to and parsing from a single format.This seems like an ok approach, especially as it's following an established pattern?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By "preversing both forms of data" you're referring to
defaultPassportData
andflattenedData
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's right. We could parse/map/serialise both into a single structure and then unpack that on "back" navigation, but I'm proposing we allow both just now 👍