import React from "react"; import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { vi } from "vitest"; import { IconButton } from "./icon-button"; const TestIcon = () => ; describe("IconButton", () => { it("renders a button element with aria-label", () => { render(} />); const button = screen.getByRole("button", { name: "Close" }); expect(button).toBeInTheDocument(); expect(button).toHaveAttribute("aria-label", "Close"); }); it("renders a button element with aria-labelledby", () => { render( <> Delete item } /> ); const button = screen.getByRole("button", { name: "Delete item" }); expect(button).toBeInTheDocument(); expect(button).toHaveAttribute("aria-labelledby", "lbl"); }); it("renders the icon as a child of the button", () => { render(} />); expect(screen.getByTestId("test-icon")).toBeInTheDocument(); }); it("renders label text when label prop is provided", () => { render( } label="Settings" /> ); expect(screen.getByText("Settings")).toBeInTheDocument(); }); it("applies data-icon-label attribute to the label span", () => { render( } label="Settings" /> ); const labelSpan = screen.getByText("Settings"); expect(labelSpan).toHaveAttribute("data-icon-label"); }); it("applies data-icon-btn='has-label' to the button when label is provided", () => { render( } label="Settings" /> ); const button = screen.getByRole("button", { name: "Settings" }); expect(button).toHaveAttribute("data-icon-btn", "has-label"); }); it("does not render a label span when label prop is omitted", () => { render(} />); expect(document.querySelector("[data-icon-label]")).toBeNull(); }); it("sets data-icon-btn to 'icon' when label is omitted", () => { render(} />); const button = screen.getByRole("button", { name: "Close" }); expect(button).toHaveAttribute("data-icon-btn", "icon"); }); it("fires the click handler when clicked", async () => { const handleClick = vi.fn(); render( } onClick={handleClick} /> ); await userEvent.click(screen.getByRole("button", { name: "Close" })); expect(handleClick).toHaveBeenCalledTimes(1); }); it("does not fire click handler when disabled", async () => { const handleClick = vi.fn(); render( } disabled onClick={handleClick} /> ); const button = screen.getByRole("button", { name: "Close" }); expect(button).toHaveAttribute("aria-disabled", "true"); await userEvent.click(button); expect(handleClick).toHaveBeenCalledTimes(0); }); it("defaults variant to 'icon'", () => { render(} />); const button = screen.getByRole("button", { name: "Close" }); expect(button).toHaveAttribute("data-style", "icon"); }); it("accepts a variant override", () => { render( } variant="outline" /> ); const button = screen.getByRole("button", { name: "Settings" }); expect(button).toHaveAttribute("data-style", "outline"); }); });