import React, { useState } from "react"; import { render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { expect, test, describe, it, vi } from "vitest"; import Select from "../../app/components/Select"; describe("Select Component Integration", () => { const TestForm = ({ initialValue = "" }) => { const [value, setValue] = useState(initialValue); const [errors, setErrors] = useState({}); const handleChange = (newValue) => { setValue(newValue); if (errors.select) { setErrors({ ...errors, select: null }); } }; const handleSubmit = (e) => { e.preventDefault(); if (!value) { setErrors({ select: "Please select an option" }); } }; return (
); }; it("handles dynamic disabled state changes", async () => { const { rerender } = render(); const selectButton = screen.getByRole("button", { name: /Dynamic Select/, }); expect(selectButton).not.toBeDisabled(); rerender(); expect(selectButton).toBeDisabled(); rerender(); expect(selectButton).not.toBeDisabled(); }); it("handles dynamic error state changes", async () => { const { rerender } = render(); const selectButton = screen.getByRole("button", { name: /Dynamic Select/, }); expect(selectButton).not.toHaveClass( "border-[var(--color-border-default-utility-negative)]" ); rerender(); expect(selectButton).toHaveClass( "border-[var(--color-border-default-utility-negative)]" ); rerender(); expect(selectButton).not.toHaveClass( "border-[var(--color-border-default-utility-negative)]" ); }); it("handles dynamic size changes", async () => { const { rerender } = render(); const selectButton = screen.getByRole("button", { name: /Dynamic Select/, }); expect(selectButton).toHaveClass("h-[32px]"); rerender(); expect(selectButton).toHaveClass("h-[36px]"); rerender(); expect(selectButton).toHaveClass("h-[40px]"); }); }); describe("Focus State Behavior", () => { it("enters focus state when tabbed to (not active state)", async () => { const user = userEvent.setup(); render(); const selectButton = screen.getByRole("button", { name: /Test Select/ }); await user.tab(); expect(selectButton).toHaveFocus(); // Should have focus state styling, not active state expect(selectButton).toHaveClass( "focus-visible:border-[var(--color-border-default-utility-info)]" ); }); it("does not enter focus state when clicked", async () => { const user = userEvent.setup(); render(); const selectButton = screen.getByRole("button", { name: /Test Select/ }); await user.click(selectButton); expect(selectButton).toHaveFocus(); // Click should not trigger focus-visible styles (class is always present but only active on keyboard focus) // The focus-visible class is always in the component but only applies on keyboard focus expect(selectButton).toHaveClass( "focus-visible:border-[var(--color-border-default-utility-info)]" ); }); }); describe("Performance", () => { it("handles rapid state changes without issues", async () => { const user = userEvent.setup(); const { rerender } = render(); const selectButton = screen.getByRole("button", { name: /Test Select/ }); // Rapidly change props for (let i = 0; i < 10; i++) { rerender(); await user.click(selectButton); await user.keyboard("{Escape}"); } // Should still be functional await user.click(selectButton); await waitFor(() => { expect(screen.getByText("Option 1")).toBeInTheDocument(); }); }); it("handles large option lists efficiently", async () => { const user = userEvent.setup(); const largeOptions = Array.from({ length: 100 }, (_, i) => ({ value: `option${i}`, label: `Option ${i}`, })); render(