import "@testing-library/jest-dom"; import { fireEvent, render, screen } from "@testing-library/react"; import { MemoryRouter } from "react-router"; import { describe, expect, it, vi } from "vitest"; import InputCombobox from "./InputCombobox"; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string) => { const translations: Record = { "shared.clear": "Clear", "shared.new": "New", "shared.noRecords": "No records", "shared.searchDot": "Search...", "shared.select": "Select", "shared.selectedCount": "selected", }; return translations[key] ?? key; }, }), })); const manyOptions = Array.from({ length: 12 }, (_, index) => ({ value: `option-${index}`, name: `Option ${index}`, })); function renderCombobox(ui: React.ReactElement) { return render({ui}); } describe("InputCombobox", () => { it("keeps the clear control reachable with a visible keyboard focus style", () => { const onClear = vi.fn(); renderCombobox(); const clearButton = screen.getByRole("button", { name: "Clear" }); expect(clearButton).toHaveClass("min-h-8", "shrink-0", "focus-visible:ring-2"); fireEvent.click(clearButton); expect(onClear).toHaveBeenCalledTimes(1); }); it("gives the icon-only new button an accessible name and stable hit area", () => { const onNew = vi.fn(); renderCombobox(); const newButton = screen.getByRole("button", { name: "New" }); expect(newButton).toHaveClass("h-9", "w-10", "shrink-0", "focus-visible:ring-2"); fireEvent.click(newButton); expect(onNew).toHaveBeenCalledTimes(1); }); it("keeps route-based icon-only creation and the popover constrained on mobile", () => { renderCombobox(); const newLink = screen.getByRole("link", { name: "New" }); expect(newLink).toHaveAttribute("href", "/admin/workspaces/new"); expect(newLink).toHaveClass("h-9", "w-10", "shrink-0", "focus-visible:ring-2"); fireEvent.click(screen.getByText("Select...").closest("button") as HTMLButtonElement); expect(screen.getByRole("listbox").parentElement).toHaveClass("max-w-[calc(100vw-2rem)]", "overflow-hidden"); expect(screen.getByRole("listbox")).toHaveClass("max-h-72", "overflow-auto"); }); it("localizes collapsed multi-select counts instead of appending hard-coded English", () => { renderCombobox(); expect(screen.getByRole("button", { name: /workspace selected/i })).toBeInTheDocument(); }); });