/* eslint-disable playwright/missing-playwright-await */ import { TooltipProvider } from "@radix-ui/react-tooltip"; import { render, fireEvent } from "@testing-library/react"; import { vi } from "vitest"; import type { UnstyledSelect } from "../../../form/Select"; import { EmailField, TextAreaField, PasswordField, NumberInput, FilterSearchField } from "./Input"; import { InputFieldWithSelect } from "./InputFieldWithSelect"; import { InputField } from "./TextField"; const onChangeMock = vi.fn(); describe("Tests for InputField Component", () => { test("Should render correctly with label and placeholder", () => { const { getByLabelText, getByPlaceholderText } = render( ); expect(getByLabelText("Test Label")).toBeInTheDocument(); expect(getByPlaceholderText("Test Placeholder")).toBeInTheDocument(); }); test("Should handle input correctly", () => { const { getByRole } = render(); const inputElement = getByRole("textbox") as HTMLInputElement; fireEvent.change(inputElement, { target: { value: "Hello" } }); expect(onChangeMock).toHaveBeenCalledTimes(1); expect(inputElement.value).toBe("Hello"); }); it("should render with addOnLeading prop", () => { const { getByText } = render(Leading} />); const addOnLeadingElement = getByText("Leading"); expect(addOnLeadingElement).toBeInTheDocument(); }); it("should render with addOnSuffix prop", () => { const { getByText } = render(Suffix} />); const addOnSuffixElement = getByText("Suffix"); expect(addOnSuffixElement).toBeInTheDocument(); }); it("should display both addOnLeading and addOnSuffix", () => { const { getByText } = render( Leading} addOnSuffix={Suffix} /> ); const addOnLeadingElement = getByText("Leading"); const addOnSuffixElement = getByText("Suffix"); expect(addOnLeadingElement).toBeInTheDocument(); expect(addOnSuffixElement).toBeInTheDocument(); }); it("Should display error message when error prop is provided", () => { const errorMessage = "This field is required"; const { getByRole } = render(); const errorElement = getByRole("textbox"); expect(errorElement).toHaveAttribute("error", errorMessage); }); }); describe("Tests for PasswordField Component", () => { test("Should toggle password visibility correctly", () => { const { getByLabelText, getByText } = render( ); const passwordInput = getByLabelText("password") as HTMLInputElement; const toggleButton = getByText("show_password"); expect(passwordInput.type).toBe("password"); fireEvent.click(toggleButton); expect(passwordInput.type).toBe("text"); fireEvent.click(toggleButton); expect(passwordInput.type).toBe("password"); }); }); describe("Tests for EmailField Component", () => { test("Should render correctly with email-related attributes", () => { const { getByRole } = render(); const emailInput = getByRole("textbox"); expect(emailInput).toHaveAttribute("type", "email"); expect(emailInput).toHaveAttribute("autoCapitalize", "none"); expect(emailInput).toHaveAttribute("autoComplete", "email"); expect(emailInput).toHaveAttribute("autoCorrect", "off"); expect(emailInput).toHaveAttribute("inputMode", "email"); }); }); describe("Tests for TextAreaField Component", () => { test("Should render correctly with label and placeholder", () => { const { getByText, getByPlaceholderText, getByRole } = render( ); expect(getByText("Test Label")).toBeInTheDocument(); expect(getByPlaceholderText("Test Placeholder")).toBeInTheDocument(); expect(getByRole("textbox")).toBeInTheDocument(); }); test("Should handle input correctly", () => { const { getByRole } = render(); const textareaElement = getByRole("textbox") as HTMLInputElement; fireEvent.change(textareaElement, { target: { value: "Hello" } }); expect(onChangeMock).toHaveBeenCalled(); expect(textareaElement.value).toBe("Hello"); }); }); describe("Tests for InputFieldWithSelect Component", () => { test("Should render correctly with InputField and UnstyledSelect", () => { const onChangeMock = vi.fn(); const selectProps = { value: null, onChange: onChangeMock, name: "testSelect", options: [ { value: "Option 1", label: "Option 1" }, { value: "Option 2", label: "Option 2" }, { value: "Option 3", label: "Option 3" }, ], } as unknown as typeof UnstyledSelect; const { getByText } = render(); const inputElement = getByText("Select..."); fireEvent.mouseDown(inputElement); const optionElement = getByText("Option 1"); expect(optionElement).toBeInTheDocument(); }); }); describe("Tests for NumberInput Component", () => { test("Should render correctly with input type number", () => { const { getByRole } = render(); const numberInput = getByRole("spinbutton"); expect(numberInput).toBeInTheDocument(); expect(numberInput).toHaveAttribute("type", "number"); }); test("Should handle input correctly", () => { const { getByRole } = render(); const numberInput = getByRole("spinbutton") as HTMLInputElement; fireEvent.change(numberInput, { target: { value: "42" } }); expect(onChangeMock).toHaveBeenCalled(); expect(numberInput.value).toBe("42"); }); }); describe("Tests for FilterSearchField Component", () => { test("Should render correctly with Search icon and input", () => { const { getByRole, getByTestId } = render(); const searchInput = getByRole("textbox"); const searchIcon = getByTestId("search-icon"); expect(searchInput).toBeInTheDocument(); expect(searchIcon).toBeInTheDocument(); }); test("Should handle input correctly", () => { const { getByRole } = render(); const searchInput = getByRole("textbox") as HTMLInputElement; fireEvent.change(searchInput, { target: { value: "Test search" } }); expect(onChangeMock).toHaveBeenCalled(); expect(searchInput.value).toBe("Test search"); }); });