import React from "react"; import { render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { renderWithSetup } from "test/test-utils"; import InputFieldWithIcon from "./InputFieldWithIcon"; describe("InputFieldWithIcon Component", () => { const mockOnChange = jest.fn(); beforeEach(() => { jest.clearAllMocks(); }); test("renders with label and placeholder", () => { render( ); expect(screen.getByText(/test input/i)).toBeInTheDocument(); expect(screen.getByPlaceholderText(/enter text/i)).toBeInTheDocument(); }); test("calls onChange when input value changes", async () => { render( ); // Change the input value await userEvent.type( screen.getByPlaceholderText(/enter text/i), "New Value" ); expect(mockOnChange).toHaveBeenCalledTimes(9); // 'New Value' has 9 characters }); test("renders help text when provided", () => { render( ); expect(screen.getByText(/this is a help text/i)).toBeInTheDocument(); }); test("renders error message when provided", () => { render( ); expect(screen.getByText(/this is an error message/i)).toBeInTheDocument(); }); test("renders clear button when clearButton is true and input has value", () => { render( ); expect(screen.getByRole("button")).toBeInTheDocument(); }); test("clears input value when clear button is clicked", async () => { render( ); // Click the clear button await userEvent.click(screen.getByRole("button")); expect(mockOnChange).toHaveBeenCalledTimes(1); expect(mockOnChange).toHaveBeenCalledWith(""); }); test("renders tooltip when provided", async () => { const { user } = renderWithSetup( ); await user.hover(screen.getByText(/test input/i)); await waitFor(() => { const tooltip = screen.getByText("This is a tooltip."); expect(tooltip).toBeInTheDocument(); }); }); test("renders icon when iconSvg is provided", () => { render( ); expect(screen.getByTestId("search-icon")).toBeInTheDocument(); }); test("does not render icon when iconSvg is not provided", () => { render( ); expect(screen.queryByTestId("search-icon")).not.toBeInTheDocument(); }); test("renders as disabled when disabled prop is true", () => { render( ); expect(screen.getByPlaceholderText(/enter text/i)).toBeDisabled(); }); test("does not allow typing when disabled", async () => { render( ); await userEvent.type( screen.getByPlaceholderText(/enter text/i), "some text" ); expect(mockOnChange).not.toHaveBeenCalled(); }); test("autofocuses the input when autofocus is true", () => { render( ); expect(screen.getByPlaceholderText(/enter text/i)).toHaveFocus(); }); test("does not autofocus the input when autofocus is false", () => { render( ); expect(screen.getByPlaceholderText(/enter text/i)).not.toHaveFocus(); }); test("calls onClick when the input is clicked", async () => { const mockOnClick = jest.fn(); render( ); await userEvent.click(screen.getByPlaceholderText(/enter text/i)); expect(mockOnClick).toHaveBeenCalledTimes(1); }); test("sets data-1p-ignore attribute when ignore1Password is true", () => { render( ); expect(screen.getByPlaceholderText(/enter text/i)).toHaveAttribute( "data-1p-ignore", "true" ); }); test("does not render clear button when value is empty", () => { render( ); expect(screen.queryByRole("button")).not.toBeInTheDocument(); }); test("applies password type styling when type is password and value is present", () => { render( ); const input = screen.getByPlaceholderText(/enter password/i); expect(input).toHaveAttribute("type", "password"); expect(input).toHaveClass("input-icon-field__input--password"); }); });