143 lines
4.5 KiB
TypeScript
143 lines
4.5 KiB
TypeScript
import "@testing-library/jest-dom/vitest";
|
|
import React from "react";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import userEvent from "@testing-library/user-event";
|
|
import SelectInput from "../../app/components/controls/SelectInput";
|
|
import { componentTestSuite } from "../utils/componentTestSuite";
|
|
import { renderWithProviders, screen } from "../utils/test-utils";
|
|
|
|
type SelectInputProps = React.ComponentProps<typeof SelectInput>;
|
|
|
|
componentTestSuite<SelectInputProps>({
|
|
component: SelectInput,
|
|
name: "SelectInput",
|
|
props: {
|
|
labelText: "Test Select Input",
|
|
showLabel: true,
|
|
placeholder: "Select an option",
|
|
options: [
|
|
{ value: "option1", label: "Option 1" },
|
|
{ value: "option2", label: "Option 2" },
|
|
],
|
|
} as SelectInputProps,
|
|
requiredProps: ["options"],
|
|
optionalProps: {},
|
|
primaryRole: "button",
|
|
testCases: {
|
|
renders: true,
|
|
accessibility: true,
|
|
keyboardNavigation: true,
|
|
disabledState: true,
|
|
errorState: true,
|
|
},
|
|
states: {
|
|
disabledProps: { disabled: true },
|
|
errorProps: { error: true },
|
|
},
|
|
});
|
|
|
|
describe("SelectInput listbox keyboard", () => {
|
|
it("moves between options with arrows and picks with Enter", async () => {
|
|
const onChange = vi.fn();
|
|
const user = userEvent.setup();
|
|
renderWithProviders(
|
|
<SelectInput
|
|
labelText="Who can read"
|
|
showLabel
|
|
value="option1"
|
|
onChange={onChange}
|
|
options={[
|
|
{ value: "option1", label: "Option 1" },
|
|
{ value: "option2", label: "Option 2" },
|
|
]}
|
|
/>,
|
|
);
|
|
|
|
const trigger = screen.getByRole("button", { name: /Who can read/i });
|
|
trigger.focus();
|
|
await user.keyboard("{ArrowDown}");
|
|
expect(await screen.findByRole("option", { name: "Option 1" })).toBeVisible();
|
|
expect(screen.getByRole("option", { name: "Option 2" })).toBeVisible();
|
|
await user.keyboard("{ArrowDown}{Enter}");
|
|
expect(onChange).toHaveBeenCalledWith({
|
|
target: { value: "option2", text: "Option 2" },
|
|
});
|
|
expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("closes the listbox when focus leaves the trigger", async () => {
|
|
const user = userEvent.setup();
|
|
renderWithProviders(
|
|
<>
|
|
<SelectInput
|
|
labelText="Who can read"
|
|
showLabel
|
|
value="option1"
|
|
options={[
|
|
{ value: "option1", label: "Option 1" },
|
|
{ value: "option2", label: "Option 2" },
|
|
]}
|
|
/>
|
|
<button type="button">Next</button>
|
|
</>,
|
|
);
|
|
|
|
const trigger = screen.getByRole("button", { name: /Who can read/i });
|
|
await user.click(trigger);
|
|
expect(await screen.findByRole("listbox")).toBeVisible();
|
|
await user.click(screen.getByRole("button", { name: "Next" }));
|
|
expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("Escape closes the listbox and does not reach document listeners", async () => {
|
|
const user = userEvent.setup();
|
|
const onDocumentKeyDown = vi.fn();
|
|
document.addEventListener("keydown", onDocumentKeyDown);
|
|
renderWithProviders(
|
|
<SelectInput
|
|
labelText="Who can read"
|
|
showLabel
|
|
value="option1"
|
|
options={[
|
|
{ value: "option1", label: "Option 1" },
|
|
{ value: "option2", label: "Option 2" },
|
|
]}
|
|
/>,
|
|
);
|
|
|
|
const trigger = screen.getByRole("button", { name: /Who can read/i });
|
|
trigger.focus();
|
|
await user.keyboard("{ArrowDown}");
|
|
expect(await screen.findByRole("listbox")).toBeVisible();
|
|
onDocumentKeyDown.mockClear();
|
|
await user.keyboard("{Escape}");
|
|
expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
|
|
expect(onDocumentKeyDown).not.toHaveBeenCalled();
|
|
document.removeEventListener("keydown", onDocumentKeyDown);
|
|
});
|
|
|
|
it("selects an option that is clicked", async () => {
|
|
const onChange = vi.fn();
|
|
const user = userEvent.setup();
|
|
renderWithProviders(
|
|
<SelectInput
|
|
labelText="Who can read"
|
|
showLabel
|
|
value="option1"
|
|
onChange={onChange}
|
|
options={[
|
|
{ value: "option1", label: "Option 1" },
|
|
{ value: "option2", label: "Option 2" },
|
|
]}
|
|
/>,
|
|
);
|
|
|
|
await user.click(screen.getByRole("button", { name: /Who can read/i }));
|
|
await user.click(await screen.findByRole("option", { name: "Option 2" }));
|
|
expect(onChange).toHaveBeenCalledWith({
|
|
target: { value: "option2", text: "Option 2" },
|
|
});
|
|
expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
|
|
});
|
|
});
|