mirror of
https://github.com/nextui-org/nextui.git
synced 2025-12-08 19:26:11 +00:00
* fix(autocomplete): autocomplete focus behaviour * feat(autocomplete): add test case for catching blur cases * refactor(autocomplete): use isOpen instead * feat(autocomplete): add "should focus when clicking autocomplete" test case * feat(autocomplete): add should set the input after selection * fix(autocomplete): remove shouldUseVirtualFocus * fix(autocomplete): uncomment blur logic * refactor(autocomplete): remove state as it is in getPopoverProps * refactor(autocomplete): remove unnecessary blur * refactor(select): remove unncessary props * fix(popover): use domRef instead * fix(popover): revise isNonModal and isDismissable * fix(popover): use dialogRef back * fix(popover): rollback * fix(autocomplete): onFocus logic * feat(popover): set disableFocusManagement to overlay * feat(modal): set disableFocusManagement to overlay * fix(autocomplete): set disableFocusManagement for autocomplete * feat(popover): include disableFocusManagement prop * refactor(autocomplete): revise type in selectorButton * fix(autocomplete): revise focus logic * feat(autocomplete): add internal focus state and add shouldCloseOnInteractOutside * feat(autocomplete): handle selectedItem change * feat(autocomplete): add clear button test * feat(changeset): add changeset * refactor(components): use the original order * refactor(autocomplete): add more comments * fix(autocomplete): revise focus behaviours * refactor(autocomplete): rename to listbox * chore(popover): remove disableFocusManagement from popover * chore(autocomplete): remove disableFocusManagement from autocomplete * chore(changeset): add issue number * fix(popover): don't set default value to transformOrigin * fix(autocomplete): revise shouldCloseOnInteractOutside logic * feat(autocomplete): should close listbox by clicking another autocomplete * fix(popover): add disableFocusManagement to overlay * refactor(autocomplete): revise comments and refactor shouldCloseOnInteractOutside * feat(changeset): add issue number * fix(autocomplete): merge with selectorButtonProps.onClick * refactor(autocomplete): remove extra line * refactor(autocomplete): revise comment * feat(select): add shouldCloseOnInteractOutside * feat(dropdown): add shouldCloseOnInteractOutside * feat(date-picker): add shouldCloseOnInteractOutside * feat(changeset): add dropdown and date-picker * fix(popover): revise shouldCloseOnInteractOutside * feat(date-picker): integrate with ariaShouldCloseOnInteractOutside * feat(select): integrate with ariaShouldCloseOnInteractOutside * feat(dropdown): integrate with ariaShouldCloseOnInteractOutside * feat(popover): integrate with ariaShouldCloseOnInteractOutside * feat(aria-utils): ariaShouldCloseOnInteractOutside * chore(deps): update pnpm-lock.yaml * feat(autocomplete): integrate with ariaShouldCloseOnInteractOutside * feat(aria-utils): handle setShouldFocus logic * feat(changeset): add @nextui-org/aria-utils * chore(autocomplete): put the test into correct group * feat(select): should close listbox by clicking another select * feat(dropdown): should close listbox by clicking another dropdown * feat(popover): should close listbox by clicking another popover * feat(date-picker): should close listbox by clicking another datepicker * chore(changeset): add issue numbers and revise changeset message * refactor(autocomplete): change to useRef instead * refactor(autocomplete): change to useRef instead * refactor(aria-utils): revise comments and format code * chore(changeset): add issue number * chore: take popoverProps.shouldCloseOnInteractOutside first * refactor(autocomplete): remove unnecessary logic * refactor(autocomplete): focus management logic
665 lines
20 KiB
TypeScript
665 lines
20 KiB
TypeScript
import * as React from "react";
|
|
import {within, render, renderHook, act} from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import {useForm} from "react-hook-form";
|
|
|
|
import {Autocomplete, AutocompleteItem, AutocompleteProps, AutocompleteSection} from "../src";
|
|
import {Modal, ModalContent, ModalBody, ModalHeader, ModalFooter} from "../../modal/src";
|
|
|
|
type Item = {
|
|
label: string;
|
|
value: string;
|
|
};
|
|
|
|
const itemsData: Item[] = [
|
|
{label: "Cat", value: "cat"},
|
|
{label: "Dog", value: "dog"},
|
|
{label: "Elephant", value: "elephant"},
|
|
{label: "Lion", value: "lion"},
|
|
{label: "Tiger", value: "tiger"},
|
|
{label: "Giraffe", value: "giraffe"},
|
|
{label: "Dolphin", value: "dolphin"},
|
|
{label: "Penguin", value: "penguin"},
|
|
{label: "Zebra", value: "zebra"},
|
|
{label: "Shark", value: "shark"},
|
|
{label: "Whale", value: "whale"},
|
|
{label: "Otter", value: "otter"},
|
|
{label: "Crocodile", value: "crocodile"},
|
|
];
|
|
|
|
const itemsSectionData = [
|
|
{
|
|
key: "mammals",
|
|
title: "Mammals",
|
|
children: [
|
|
{key: "lion", label: "Lion", value: "lion"},
|
|
{key: "tiger", label: "Tiger", value: "tiger"},
|
|
{key: "elephant", label: "Elephant", value: "elephant"},
|
|
],
|
|
},
|
|
{
|
|
key: "birds",
|
|
title: "Birds",
|
|
children: [
|
|
{key: "penguin", label: "Penguin", value: "penguin"},
|
|
{key: "ostrich", label: "Ostrich", value: "ostrich"},
|
|
{key: "peacock", label: "Peacock", value: "peacock"},
|
|
],
|
|
},
|
|
];
|
|
|
|
const AutocompleteExample = (props: Partial<AutocompleteProps> = {}) => (
|
|
<Autocomplete label="Favorite Animal" {...props}>
|
|
<AutocompleteItem key="penguin" value="penguin">
|
|
Penguin
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="zebra" value="zebra">
|
|
Zebra
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="shark" value="shark">
|
|
Shark
|
|
</AutocompleteItem>
|
|
</Autocomplete>
|
|
);
|
|
|
|
describe("Autocomplete", () => {
|
|
it("should render correctly", () => {
|
|
const wrapper = render(<AutocompleteExample />);
|
|
|
|
expect(() => wrapper.unmount()).not.toThrow();
|
|
});
|
|
|
|
it("ref should be forwarded", () => {
|
|
const ref = React.createRef<HTMLInputElement>();
|
|
|
|
render(
|
|
<Autocomplete ref={ref} aria-label="Favorite Animal" label="Favorite Animal">
|
|
<AutocompleteItem key="penguin" value="penguin">
|
|
Penguin
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="zebra" value="zebra">
|
|
Zebra
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="shark" value="shark">
|
|
Shark
|
|
</AutocompleteItem>
|
|
</Autocomplete>,
|
|
);
|
|
|
|
expect(ref.current).not.toBeNull();
|
|
});
|
|
|
|
it("should render correctly (dynamic)", () => {
|
|
const wrapper = render(
|
|
<Autocomplete aria-label="Favorite Animal" items={itemsData} label="Favorite Animal">
|
|
{(item) => <AutocompleteItem key={item.value}>{item.label}</AutocompleteItem>}
|
|
</Autocomplete>,
|
|
);
|
|
|
|
expect(() => wrapper.unmount()).not.toThrow();
|
|
});
|
|
|
|
it("should render correctly with section (static)", () => {
|
|
const wrapper = render(
|
|
<Autocomplete aria-label="Favorite Animal" label="Favorite Animal">
|
|
<AutocompleteSection title="Birds">
|
|
<AutocompleteItem key="penguin" value="penguin">
|
|
Penguin
|
|
</AutocompleteItem>
|
|
</AutocompleteSection>
|
|
<AutocompleteSection title="Mammals">
|
|
<AutocompleteItem key="zebra" value="zebra">
|
|
Zebra
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="shark" value="shark">
|
|
Shark
|
|
</AutocompleteItem>
|
|
</AutocompleteSection>
|
|
</Autocomplete>,
|
|
);
|
|
|
|
expect(() => wrapper.unmount()).not.toThrow();
|
|
});
|
|
|
|
it("should render correctly with section (dynamic)", () => {
|
|
const wrapper = render(
|
|
<Autocomplete aria-label="Favorite Animal" items={itemsSectionData} label="Favorite Animal">
|
|
{(section) => (
|
|
<AutocompleteSection<Item>
|
|
aria-label={section.title}
|
|
items={section.children}
|
|
title={section.title}
|
|
>
|
|
{/* @ts-ignore TODO: fix section children types*/}
|
|
{(item: Item) => <AutocompleteItem key={item.value}>{item.label}</AutocompleteItem>}
|
|
</AutocompleteSection>
|
|
)}
|
|
</Autocomplete>,
|
|
);
|
|
|
|
expect(() => wrapper.unmount()).not.toThrow();
|
|
});
|
|
|
|
it("should focus when clicking autocomplete", async () => {
|
|
const wrapper = render(
|
|
<Autocomplete aria-label="Favorite Animal" data-testid="autocomplete" label="Favorite Animal">
|
|
<AutocompleteItem key="penguin" value="penguin">
|
|
Penguin
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="zebra" value="zebra">
|
|
Zebra
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="shark" value="shark">
|
|
Shark
|
|
</AutocompleteItem>
|
|
</Autocomplete>,
|
|
);
|
|
|
|
const autocomplete = wrapper.getByTestId("autocomplete");
|
|
|
|
// open the select listbox
|
|
await act(async () => {
|
|
await userEvent.click(autocomplete);
|
|
});
|
|
|
|
// assert that the autocomplete listbox is open
|
|
expect(autocomplete).toHaveAttribute("aria-expanded", "true");
|
|
|
|
// assert that input is focused
|
|
expect(autocomplete).toHaveFocus();
|
|
});
|
|
|
|
it("should clear value after clicking clear button", async () => {
|
|
const wrapper = render(
|
|
<Autocomplete aria-label="Favorite Animal" data-testid="autocomplete" label="Favorite Animal">
|
|
<AutocompleteItem key="penguin" value="penguin">
|
|
Penguin
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="zebra" value="zebra">
|
|
Zebra
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="shark" value="shark">
|
|
Shark
|
|
</AutocompleteItem>
|
|
</Autocomplete>,
|
|
);
|
|
|
|
const autocomplete = wrapper.getByTestId("autocomplete");
|
|
|
|
// open the select listbox
|
|
await act(async () => {
|
|
await userEvent.click(autocomplete);
|
|
});
|
|
|
|
// assert that the autocomplete listbox is open
|
|
expect(autocomplete).toHaveAttribute("aria-expanded", "true");
|
|
|
|
let options = wrapper.getAllByRole("option");
|
|
|
|
// select the target item
|
|
await act(async () => {
|
|
await userEvent.click(options[0]);
|
|
});
|
|
|
|
const {container} = wrapper;
|
|
|
|
const clearButton = container.querySelector(
|
|
"[data-slot='inner-wrapper'] button:nth-of-type(1)",
|
|
)!;
|
|
|
|
expect(clearButton).not.toBeNull();
|
|
|
|
// select the target item
|
|
await act(async () => {
|
|
await userEvent.click(clearButton);
|
|
});
|
|
|
|
// assert that the input has empty value
|
|
expect(autocomplete).toHaveValue("");
|
|
|
|
// assert that input is focused
|
|
expect(autocomplete).toHaveFocus();
|
|
});
|
|
|
|
it("should open and close listbox by clicking selector button", async () => {
|
|
const wrapper = render(
|
|
<Autocomplete aria-label="Favorite Animal" data-testid="autocomplete" label="Favorite Animal">
|
|
<AutocompleteItem key="penguin" value="penguin">
|
|
Penguin
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="zebra" value="zebra">
|
|
Zebra
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="shark" value="shark">
|
|
Shark
|
|
</AutocompleteItem>
|
|
</Autocomplete>,
|
|
);
|
|
|
|
const {container} = wrapper;
|
|
|
|
const selectorButton = container.querySelector(
|
|
"[data-slot='inner-wrapper'] button:nth-of-type(2)",
|
|
)!;
|
|
|
|
expect(selectorButton).not.toBeNull();
|
|
|
|
const autocomplete = wrapper.getByTestId("autocomplete");
|
|
|
|
// open the select listbox by clicking selector button
|
|
await act(async () => {
|
|
await userEvent.click(selectorButton);
|
|
});
|
|
|
|
// assert that the autocomplete listbox is open
|
|
expect(autocomplete).toHaveAttribute("aria-expanded", "true");
|
|
|
|
// assert that input is focused
|
|
expect(autocomplete).toHaveFocus();
|
|
|
|
// close the select listbox by clicking selector button again
|
|
await act(async () => {
|
|
await userEvent.click(selectorButton);
|
|
});
|
|
|
|
// assert that the autocomplete listbox is closed
|
|
expect(autocomplete).toHaveAttribute("aria-expanded", "false");
|
|
|
|
// assert that input is still focused
|
|
expect(autocomplete).toHaveFocus();
|
|
});
|
|
|
|
it("should close listbox when clicking outside autocomplete", async () => {
|
|
const wrapper = render(
|
|
<Autocomplete
|
|
aria-label="Favorite Animal"
|
|
data-testid="close-when-clicking-outside-test"
|
|
label="Favorite Animal"
|
|
>
|
|
<AutocompleteItem key="penguin" value="penguin">
|
|
Penguin
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="zebra" value="zebra">
|
|
Zebra
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="shark" value="shark">
|
|
Shark
|
|
</AutocompleteItem>
|
|
</Autocomplete>,
|
|
);
|
|
|
|
const autocomplete = wrapper.getByTestId("close-when-clicking-outside-test");
|
|
|
|
// open the select listbox
|
|
await act(async () => {
|
|
await userEvent.click(autocomplete);
|
|
});
|
|
|
|
// assert that the autocomplete listbox is open
|
|
expect(autocomplete).toHaveAttribute("aria-expanded", "true");
|
|
|
|
// click outside the autocomplete component
|
|
await act(async () => {
|
|
await userEvent.click(document.body);
|
|
});
|
|
|
|
// assert that the autocomplete is closed
|
|
expect(autocomplete).toHaveAttribute("aria-expanded", "false");
|
|
|
|
// assert that input is not focused
|
|
expect(autocomplete).not.toHaveFocus();
|
|
});
|
|
|
|
it("should close listbox when clicking outside autocomplete with modal open", async () => {
|
|
const wrapper = render(
|
|
<Modal isOpen>
|
|
<ModalContent>
|
|
<ModalHeader>Modal header</ModalHeader>
|
|
<ModalBody>
|
|
<Autocomplete
|
|
aria-label="Favorite Animal"
|
|
data-testid="close-when-clicking-outside-test"
|
|
label="Favorite Animal"
|
|
>
|
|
<AutocompleteItem key="penguin" value="penguin">
|
|
Penguin
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="zebra" value="zebra">
|
|
Zebra
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="shark" value="shark">
|
|
Shark
|
|
</AutocompleteItem>
|
|
</Autocomplete>
|
|
</ModalBody>
|
|
<ModalFooter>Modal footer</ModalFooter>
|
|
</ModalContent>
|
|
</Modal>,
|
|
);
|
|
|
|
const autocomplete = wrapper.getByTestId("close-when-clicking-outside-test");
|
|
|
|
// open the autocomplete listbox
|
|
await act(async () => {
|
|
await userEvent.click(autocomplete);
|
|
});
|
|
|
|
// assert that the autocomplete listbox is open
|
|
expect(autocomplete).toHaveAttribute("aria-expanded", "true");
|
|
|
|
// click outside the autocomplete component
|
|
await act(async () => {
|
|
await userEvent.click(document.body);
|
|
});
|
|
|
|
// assert that the autocomplete listbox is closed
|
|
expect(autocomplete).toHaveAttribute("aria-expanded", "false");
|
|
|
|
// assert that input is not focused
|
|
expect(autocomplete).not.toHaveFocus();
|
|
});
|
|
|
|
it("should set the input after selection", async () => {
|
|
const wrapper = render(
|
|
<Autocomplete aria-label="Favorite Animal" data-testid="autocomplete" label="Favorite Animal">
|
|
<AutocompleteItem key="penguin" value="penguin">
|
|
Penguin
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="zebra" value="zebra">
|
|
Zebra
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="shark" value="shark">
|
|
Shark
|
|
</AutocompleteItem>
|
|
</Autocomplete>,
|
|
);
|
|
|
|
const autocomplete = wrapper.getByTestId("autocomplete");
|
|
|
|
// open the listbox
|
|
await act(async () => {
|
|
await userEvent.click(autocomplete);
|
|
});
|
|
|
|
// assert that the autocomplete listbox is open
|
|
expect(autocomplete).toHaveAttribute("aria-expanded", "true");
|
|
|
|
// assert that input is focused
|
|
expect(autocomplete).toHaveFocus();
|
|
|
|
let options = wrapper.getAllByRole("option");
|
|
|
|
expect(options.length).toBe(3);
|
|
|
|
// select the target item
|
|
await act(async () => {
|
|
await userEvent.click(options[0]);
|
|
});
|
|
|
|
// assert that the input has target selection
|
|
expect(autocomplete).toHaveValue("Penguin");
|
|
});
|
|
|
|
it("should close listbox by clicking another autocomplete", async () => {
|
|
const wrapper = render(
|
|
<>
|
|
<Autocomplete
|
|
aria-label="Favorite Animal"
|
|
data-testid="autocomplete"
|
|
label="Favorite Animal"
|
|
>
|
|
<AutocompleteItem key="penguin" value="penguin">
|
|
Penguin
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="zebra" value="zebra">
|
|
Zebra
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="shark" value="shark">
|
|
Shark
|
|
</AutocompleteItem>
|
|
</Autocomplete>
|
|
<Autocomplete
|
|
aria-label="Favorite Animal"
|
|
data-testid="autocomplete2"
|
|
label="Favorite Animal"
|
|
>
|
|
<AutocompleteItem key="penguin" value="penguin">
|
|
Penguin
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="zebra" value="zebra">
|
|
Zebra
|
|
</AutocompleteItem>
|
|
<AutocompleteItem key="shark" value="shark">
|
|
Shark
|
|
</AutocompleteItem>
|
|
</Autocomplete>
|
|
</>,
|
|
);
|
|
|
|
const {container} = wrapper;
|
|
|
|
const autocomplete = wrapper.getByTestId("autocomplete");
|
|
|
|
const autocomplete2 = wrapper.getByTestId("autocomplete2");
|
|
|
|
const innerWrappers = container.querySelectorAll("[data-slot='inner-wrapper']");
|
|
|
|
const selectorButton = innerWrappers[0].querySelector("button:nth-of-type(2)")!;
|
|
|
|
const selectorButton2 = innerWrappers[1].querySelector("button:nth-of-type(2)")!;
|
|
|
|
expect(selectorButton).not.toBeNull();
|
|
|
|
expect(selectorButton2).not.toBeNull();
|
|
|
|
// open the select listbox by clicking selector button in the first autocomplete
|
|
await act(async () => {
|
|
await userEvent.click(selectorButton);
|
|
});
|
|
|
|
// assert that the first autocomplete listbox is open
|
|
expect(autocomplete).toHaveAttribute("aria-expanded", "true");
|
|
|
|
// assert that input is focused
|
|
expect(autocomplete).toHaveFocus();
|
|
|
|
// close the select listbox by clicking the second autocomplete
|
|
await act(async () => {
|
|
await userEvent.click(selectorButton2);
|
|
});
|
|
|
|
// assert that the first autocomplete listbox is closed
|
|
expect(autocomplete).toHaveAttribute("aria-expanded", "false");
|
|
|
|
// assert that the second autocomplete listbox is open
|
|
expect(autocomplete2).toHaveAttribute("aria-expanded", "true");
|
|
|
|
// assert that the first autocomplete is not focused
|
|
expect(autocomplete).not.toHaveFocus();
|
|
|
|
// assert that the second autocomplete is focused
|
|
expect(autocomplete2).toHaveFocus();
|
|
});
|
|
|
|
describe("validation", () => {
|
|
let user;
|
|
|
|
beforeAll(() => {
|
|
user = userEvent.setup();
|
|
});
|
|
|
|
describe("validationBehavior=native", () => {
|
|
it("supports isRequired", async () => {
|
|
const {getByTestId, getByRole, findByRole} = render(
|
|
<form data-testid="form">
|
|
<AutocompleteExample isRequired validationBehavior="native" />
|
|
</form>,
|
|
);
|
|
|
|
const input = getByRole("combobox") as HTMLInputElement;
|
|
|
|
expect(input).toHaveAttribute("required");
|
|
expect(input).not.toHaveAttribute("aria-required");
|
|
expect(input).not.toHaveAttribute("aria-describedby");
|
|
expect(input.validity.valid).toBe(false);
|
|
|
|
act(() => {
|
|
(getByTestId("form") as HTMLFormElement).checkValidity();
|
|
});
|
|
|
|
expect(input).toHaveAttribute("aria-describedby");
|
|
expect(document.getElementById(input.getAttribute("aria-describedby")!)).toHaveTextContent(
|
|
"Constraints not satisfied",
|
|
);
|
|
|
|
await user.click(input);
|
|
await user.keyboard("pe");
|
|
|
|
const listbox = await findByRole("listbox");
|
|
const items = within(listbox).getAllByRole("option");
|
|
|
|
await user.click(items[0]);
|
|
expect(input).toHaveAttribute("aria-describedby");
|
|
});
|
|
});
|
|
|
|
describe("validationBehavior=aria", () => {
|
|
it("supports validate function", async () => {
|
|
let {getByRole, findByRole} = render(
|
|
<form data-testid="form">
|
|
<AutocompleteExample
|
|
defaultInputValue="Penguin"
|
|
validate={(v) => (v === "Penguin" ? "Invalid value" : null)}
|
|
validationBehavior="aria"
|
|
/>
|
|
</form>,
|
|
);
|
|
|
|
const input = getByRole("combobox") as HTMLInputElement;
|
|
|
|
expect(input).toHaveAttribute("aria-describedby");
|
|
expect(input).toHaveAttribute("aria-invalid", "true");
|
|
expect(document.getElementById(input.getAttribute("aria-describedby")!)).toHaveTextContent(
|
|
"Invalid value",
|
|
);
|
|
expect(input.validity.valid).toBe(true);
|
|
|
|
await user.tab();
|
|
await user.click();
|
|
// open the select dropdown
|
|
await user.keyboard("{ArrowDown}");
|
|
|
|
const listbox = await findByRole("listbox");
|
|
const item = within(listbox).getByRole("option", {name: "Zebra"});
|
|
|
|
await user.click(item);
|
|
|
|
expect(input).not.toHaveAttribute("aria-describedby");
|
|
expect(input).not.toHaveAttribute("aria-invalid");
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("Autocomplete with React Hook Form", () => {
|
|
let autocomplete1: HTMLInputElement;
|
|
let autocomplete2: HTMLInputElement;
|
|
let autocomplete3: HTMLInputElement;
|
|
let submitButton: HTMLButtonElement;
|
|
let wrapper: any;
|
|
let onSubmit: () => void;
|
|
|
|
beforeEach(() => {
|
|
const {result} = renderHook(() =>
|
|
useForm({
|
|
defaultValues: {
|
|
withDefaultValue: "cat",
|
|
withoutDefaultValue: "",
|
|
requiredField: "",
|
|
},
|
|
}),
|
|
);
|
|
|
|
const {
|
|
handleSubmit,
|
|
register,
|
|
formState: {errors},
|
|
} = result.current;
|
|
|
|
onSubmit = jest.fn();
|
|
|
|
wrapper = render(
|
|
<form className="flex w-full max-w-xs flex-col gap-2" onSubmit={handleSubmit(onSubmit)}>
|
|
<Autocomplete
|
|
data-testid="autocomplete-1"
|
|
{...register("withDefaultValue")}
|
|
aria-label="Favorite Animal"
|
|
items={itemsData}
|
|
label="Favorite Animal"
|
|
>
|
|
{(item) => <AutocompleteItem key={item.value}>{item.label}</AutocompleteItem>}
|
|
</Autocomplete>
|
|
<Autocomplete
|
|
data-testid="autocomplete-2"
|
|
{...register("withoutDefaultValue")}
|
|
aria-label="Favorite Animal"
|
|
items={itemsData}
|
|
label="Favorite Animal"
|
|
>
|
|
{(item) => <AutocompleteItem key={item.value}>{item.label}</AutocompleteItem>}
|
|
</Autocomplete>
|
|
<Autocomplete
|
|
data-testid="autocomplete-3"
|
|
{...register("requiredField", {required: true})}
|
|
aria-label="Favorite Animal"
|
|
items={itemsData}
|
|
label="Favorite Animal"
|
|
>
|
|
{(item) => <AutocompleteItem key={item.value}>{item.label}</AutocompleteItem>}
|
|
</Autocomplete>
|
|
{errors.requiredField && <span className="text-danger">This field is required</span>}
|
|
<button data-testid="submit-button" type="submit">
|
|
Submit
|
|
</button>
|
|
</form>,
|
|
);
|
|
|
|
autocomplete1 = wrapper.getByTestId("autocomplete-1");
|
|
autocomplete2 = wrapper.getByTestId("autocomplete-2");
|
|
autocomplete3 = wrapper.getByTestId("autocomplete-3");
|
|
submitButton = wrapper.getByTestId("submit-button");
|
|
});
|
|
|
|
it("should work with defaultValues", () => {
|
|
expect(autocomplete1).toHaveValue("Cat");
|
|
expect(autocomplete2).toHaveValue("");
|
|
expect(autocomplete3).toHaveValue("");
|
|
});
|
|
|
|
it("should not submit form when required field is empty", async () => {
|
|
const user = userEvent.setup();
|
|
|
|
await user.click(submitButton);
|
|
|
|
expect(onSubmit).toHaveBeenCalledTimes(0);
|
|
});
|
|
|
|
it("should submit form when required field is not empty", async () => {
|
|
const user = userEvent.setup();
|
|
|
|
await user.click(autocomplete3);
|
|
|
|
expect(autocomplete3).toHaveAttribute("aria-expanded", "true");
|
|
|
|
let listboxItems = wrapper.getAllByRole("option");
|
|
|
|
await user.click(listboxItems[1]);
|
|
|
|
expect(autocomplete3).toHaveValue("Dog");
|
|
|
|
await user.click(submitButton);
|
|
|
|
expect(onSubmit).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|