import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { useForm } from "react-hook-form"; import { describe, expect, it } from "vitest"; import { Form } from "@/components/shadcn/form"; import { CustomInput } from "./custom-input"; interface TestFormValues { password: string; email: string; } const TestForm = ({ password = false, isRequired, }: { password?: boolean; isRequired?: boolean; }) => { const form = useForm({ defaultValues: { password: "", email: "" }, }); return (
); }; describe("CustomInput", () => { describe("when used as a password field", () => { it("should mask the value by default", () => { // Given render(); // Then expect(screen.getByLabelText(/^password/i)).toHaveAttribute( "type", "password", ); }); it("should reveal and re-mask the value with the visibility toggle", async () => { // Given const user = userEvent.setup(); render(); const input = screen.getByLabelText(/^password/i); await user.type(input, "hunter2"); // When the user shows the password await user.click(screen.getByRole("button", { name: "Show password" })); // Then the value becomes readable and the toggle flips expect(input).toHaveAttribute("type", "text"); expect(input).toHaveValue("hunter2"); // When the user hides it again await user.click(screen.getByRole("button", { name: "Hide password" })); // Then expect(input).toHaveAttribute("type", "password"); }); }); describe("when used as a regular field", () => { it("should keep the provided type and accept user input", async () => { // Given const user = userEvent.setup(); render(); const input = screen.getByLabelText(/email/i); // When await user.type(input, "dev@prowler.com"); // Then expect(input).toHaveAttribute("type", "text"); expect(input).toHaveValue("dev@prowler.com"); expect( screen.queryByRole("button", { name: /password/i }), ).not.toBeInTheDocument(); }); it("should hide the required indicator when isRequired is false", () => { // Given render(); // Then expect(screen.queryByText("*")).not.toBeInTheDocument(); expect(screen.getByLabelText(/email/i)).not.toBeRequired(); }); }); });