Skip to content
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

fix: Fail 'select's against disabled options. #967

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/inputs/SelectField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,16 @@ describe("SelectFieldTest", () => {
/>,
);
// When opening the menu
select(r.age, "Two");
click(r.age);
const optionTwo = r.getByRole("option", { name: "Two" });
// Then the disabled option to have the correct aria attributes
expect(optionTwo).toHaveAttribute("aria-disabled", "true");
// And when clicking on that option
click(optionTwo);
// Then the `onSelect` callback is not called
expect(onSelect).not.toHaveBeenCalled();
// And using select would have failed
expect(() => select(r.age, "Two")).toThrow("Cannot select disabled option Two");
});

it("can disable options with tooltips", async () => {
Expand Down
40 changes: 35 additions & 5 deletions src/utils/rtl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe("rtl", () => {
select(r.number, "2");
// Then the onSelect handler is called with the correct value
expect(onSelect).toHaveBeenCalledWith("2", { id: "2", name: "Two" });
expect(r.number).toHaveValue("Two");

// And the getSelected helper returns the correct value
expect(getSelected(r.number)).toBe("Two");
Expand All @@ -46,22 +47,51 @@ describe("rtl", () => {
it("can select options via label on SelectField", async () => {
const onSelect = jest.fn();
// Given the SelectField
function Test() {
const [value, setValue] = useState<string | undefined>();
return (
<SelectField
label="Number"
value={value}
onSelect={(value, opt) => {
setValue(value);
onSelect(value, opt);
}}
options={[
{ id: "1", name: "One" },
{ id: "2", name: "Two" },
{ id: "3", name: "Three" },
]}
/>
);
}
const r = await render(<Test />);
// When selecting an option
select(r.number, "Two");
// Then the onSelect handler is called with the correct value
expect(onSelect).toHaveBeenCalledWith("2", { id: "2", name: "Two" });
expect(r.number).toHaveValue("Two");
});

it("fails when selecting disabled options", async () => {
const onSelect = jest.fn();
// Given a SelectField
const r = await render(
<SelectField
label="Number"
value={undefined}
value={undefined as any}
onSelect={onSelect}
options={[
{ id: "1", name: "One" },
{ id: "2", name: "Two" },
{ id: "3", name: "Three" },
]}
// And the 1st option option is disabled
disabledOptions={["1"]}
/>,
);
// When selecting an option
select(r.number, "Two");
// Then the onSelect handler is called with the correct value
expect(onSelect).toHaveBeenCalledWith("2", { id: "2", name: "Two" });
// When selecting it, it fails
expect(() => select(r.number, "One")).toThrow("Cannot select disabled option One");
});

it("can selectAndWait on SelectField", async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/rtl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ function selectOption(select: HTMLElement, optionValue: string) {
if (!optionToSelect) {
throw new Error(`Could not find option with value or text content of ${optionValue}`);
}
if (optionToSelect.getAttribute("aria-disabled")) {
throw new Error(`Cannot select disabled option ${optionValue}`);
}
_click(optionToSelect);
}

Expand Down
Loading