-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: can toggle feedback required and show validation errors (#3939)
- Loading branch information
Showing
7 changed files
with
235 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 0 additions & 45 deletions
45
editor.planx.uk/src/@planx/components/Feedback/Public/Public.test.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
editor.planx.uk/src/@planx/components/Feedback/Public/tests/Public.accessibility.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { screen } from "@testing-library/react"; | ||
import { | ||
getInternalFeedbackMetadata, | ||
insertFeedbackMutation, | ||
} from "lib/feedback"; | ||
import React from "react"; | ||
import { setup } from "testUtils"; | ||
import { vi } from "vitest"; | ||
import { axe } from "vitest-axe"; | ||
|
||
import FeedbackComponent from "../Public"; | ||
|
||
vi.mock("lib/feedback", () => ({ | ||
getInternalFeedbackMetadata: vi.fn(), | ||
insertFeedbackMutation: vi.fn(), | ||
})); | ||
|
||
describe("when the Feedback component is rendered", async () => { | ||
it("should not have any accessibility violations", async () => { | ||
const { container } = setup(<FeedbackComponent feedbackRequired={false} />); | ||
const results = await axe(container); | ||
expect(results).toHaveNoViolations(); | ||
}); | ||
it("should be navigable by keyboard", async () => { | ||
const { user } = setup(<FeedbackComponent feedbackRequired={false} />); | ||
|
||
const ratingButtons = screen.getAllByRole("button"); | ||
|
||
// user tabs through all rating buttons and selects the last one | ||
await user.tab(); | ||
expect(ratingButtons[0]).toHaveFocus(); | ||
await user.tab(); | ||
expect(ratingButtons[1]).toHaveFocus(); | ||
await user.tab(); | ||
expect(ratingButtons[2]).toHaveFocus(); | ||
await user.tab(); | ||
expect(ratingButtons[3]).toHaveFocus(); | ||
await user.tab(); | ||
expect(ratingButtons[4]).toHaveFocus(); | ||
await user.keyboard("[Space]"); // select 'Excellent' button | ||
|
||
await user.tab(); | ||
expect(screen.getByRole("textbox")).toHaveFocus(); | ||
|
||
await user.tab(); | ||
expect(screen.getByTestId("continue-button")).toHaveFocus(); | ||
await user.keyboard("[Space]"); // submits | ||
|
||
expect(getInternalFeedbackMetadata).toBeCalled(); | ||
expect(insertFeedbackMutation).toBeCalledWith({ | ||
feedbackScore: 5, | ||
feedbackType: "component", | ||
userComment: "", | ||
}); | ||
}); | ||
}); |
97 changes: 97 additions & 0 deletions
97
editor.planx.uk/src/@planx/components/Feedback/Public/tests/Public.submit.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { screen } from "@testing-library/react"; | ||
import { | ||
getInternalFeedbackMetadata, | ||
insertFeedbackMutation, | ||
} from "lib/feedback"; | ||
import React from "react"; | ||
import { setup } from "testUtils"; | ||
import { vi } from "vitest"; | ||
|
||
import FeedbackComponent from "../Public"; | ||
|
||
const handleSubmit = vi.fn(); | ||
vi.mock("lib/feedback", () => ({ | ||
getInternalFeedbackMetadata: vi.fn(), | ||
insertFeedbackMutation: vi.fn(), | ||
})); | ||
|
||
describe.each([ | ||
{ | ||
dataType: "a rating", | ||
expectedData: { | ||
feedbackScore: 1, | ||
feedbackType: "component", | ||
userComment: "", | ||
}, | ||
}, | ||
{ | ||
dataType: "a comment", | ||
expectedData: { | ||
feedbackScore: "", | ||
feedbackType: "component", | ||
userComment: "I had a great time", | ||
}, | ||
}, | ||
{ | ||
dataType: "nothing", | ||
expectedData: { | ||
feedbackScore: "", | ||
feedbackType: "component", | ||
userComment: "", | ||
}, | ||
}, | ||
])( | ||
"when a user submits $dataType on a feedback component where feedback is not required", | ||
({ dataType, expectedData }) => { | ||
beforeEach(async () => { | ||
const { user } = setup( | ||
<FeedbackComponent | ||
handleSubmit={handleSubmit} | ||
feedbackRequired={false} | ||
/>, | ||
); | ||
|
||
switch (dataType) { | ||
case "a rating": | ||
await user.click(screen.getByTestId("feedback-button-terrible")); | ||
|
||
break; | ||
case "a comment": | ||
await user.type( | ||
screen.getByTestId("user-comment"), | ||
"I had a great time", | ||
); | ||
break; | ||
case "nothing": | ||
default: | ||
break; | ||
} | ||
|
||
await user.click(screen.getByTestId("continue-button")); | ||
}); | ||
|
||
it("should call the handleSubmit function with the correct data", async () => { | ||
expect(getInternalFeedbackMetadata).toBeCalled(); | ||
expect(insertFeedbackMutation).toBeCalledWith(expectedData); | ||
}); | ||
}, | ||
); | ||
|
||
describe("when feedback is required but the user does not submit any data", async () => { | ||
beforeEach(async () => { | ||
const { user } = setup( | ||
<FeedbackComponent handleSubmit={handleSubmit} feedbackRequired={true} />, | ||
); | ||
await user.click(screen.getByTestId("continue-button")); | ||
}); | ||
|
||
it("displays an appropriate error message for each missing field", async () => { | ||
const errorMessages = [ | ||
"Please rate your experience", | ||
"Enter your feedback", | ||
]; | ||
errorMessages.map((error) => { | ||
expect(screen.getByText(error)).toBeVisible(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters