import { render, screen } from "@testing-library/react"; // eslint-disable-next-line import/no-named-as-default import userEvent from "@testing-library/user-event"; import { InputSearchInput } from "./InputSearchInput"; import { InputSearchRoot } from "./InputSearchRoot"; import { InputSearchPopover } from "./InputSearchPopover"; import { InputSearchList } from "./InputSearchList"; import { InputSearchListItem } from "./InputSearchListItem"; import { theme } from "../theme"; describe("InputSearchInput", () => { const customRender = (ui, contextProps) => { return render( {ui} ); }; test("renders visibly into the document", () => { customRender(, {}); expect(screen.getByRole("combobox")).toBeInTheDocument(); }); test("renders with custom CSS class", () => { customRender( , {} ); expect(screen.getByRole("combobox")).toHaveClass(/wpds-.*-css/); }); test("uses the error prop", () => { customRender(, {}); expect(screen.getByTestId("input-text-container")).toHaveClass( /wpds-.*-isInvalid-true/ ); }); test("uses the success prop", () => { customRender(, {}); expect(screen.getByTestId("input-text-container")).toHaveClass( /wpds-.*-isSuccessful-true/ ); }); test("uses the disabled prop", () => { customRender(, {}); expect(screen.getByRole("combobox")).toBeDisabled(); }); test("uses the label", () => { const labelText = "Test"; customRender( , {} ); expect(screen.getByLabelText(labelText)).toBeInTheDocument(); }); /** must be managed by react-stately / react-aria */ test.skip("uses id", () => { customRender(, {}); expect(screen.getByRole("combobox")).toHaveAttribute("id", "test"); }); test("uses name", () => { customRender(, {}); expect(screen.getByRole("combobox")).toHaveAttribute("name", "test"); }); test("uses name", () => { customRender(, {}); expect(screen.getByRole("combobox")).toHaveAttribute("name", "test"); }); test("uses placeholder", () => { customRender( , {} ); expect(screen.getByRole("combobox")).toHaveAttribute("placeholder", "Test"); }); test("renders with custom CSS class", () => { customRender( , {} ); expect(screen.getByRole("combobox")).toHaveClass(/wpds-.*-css/); }); test("shows as required", () => { customRender(, {}); expect(screen.getByText("*")).toBeInTheDocument(); }); test("uses the value prop", () => { customRender(, {}); expect(screen.getByRole("combobox")).toHaveValue("Test"); }); test("uses the buttonIconText prop", () => { const buttonIconText = "Click to search"; customRender( , {} ); expect(screen.getByText(buttonIconText)).toBeInTheDocument(); }); test("uses the buttonIconType prop", () => { customRender( , {} ); expect(screen.getByRole("button")).toHaveAttribute("type", "submit"); }); test("uses the errorMessage prop", () => { customRender( , {} ); expect(screen.getByText("Error")).toBeInTheDocument(); }); test("uses the helperText prop", () => { customRender( , {} ); expect(screen.getByText("Help")).toBeInTheDocument(); }); test("uses the onFocus event handler", () => { const onFocus = jest.fn(); customRender( , {} ); screen.getByRole("combobox").focus(); expect(onFocus).toHaveBeenCalled(); }); test("uses the onBlur event handler", () => { const onBlur = jest.fn(); customRender( , {} ); const input = screen.getByRole("combobox"); input.focus(); input.blur(); expect(onBlur).toHaveBeenCalled(); }); test("uses the onChange event handler", async () => { const user = userEvent.setup(); const onChange = jest.fn(); customRender( , {} ); await user.keyboard("Test"); expect(onChange).toHaveBeenCalled; }); test("uses the onButtonIconClick event handler", async () => { const user = userEvent.setup(); const onButtonIconClick = jest.fn(); customRender( , {} ); await user.click(screen.getByRole("button")); expect(onButtonIconClick).toHaveBeenCalled(); }); test("updates the value when an item is highlighted", async () => { const user = userEvent.setup(); customRender(, {}); const inputElement = screen.getByRole("combobox"); await user.click(inputElement); await user.keyboard("T"); await user.keyboard("[ArrowDown]"); expect(inputElement).toHaveValue("Test item"); }); }); describe("InputSearchInput - Controlled", () => { test("updates the value when an item is highlighted", async () => { const user = userEvent.setup(); const changeSpy = jest.fn(); render( { changeSpy(event.target.value); }} /> ); const inputElement = screen.getByRole("combobox"); await user.click(inputElement); await user.keyboard("T"); await user.keyboard("[ArrowDown]"); expect(inputElement).toHaveValue("Apple"); }); });