import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { StatsDisclosureCard } from "./StatsDisclosureCard";
afterEach(cleanup);
describe("StatsDisclosureCard", () => {
it("uses a keyboard-focusable disclosure button with a valid detail relationship", () => {
const onToggle = vi.fn();
const { rerender } = render(
icon}
expanded={false}
onToggle={onToggle}
>
Manage swatches
,
);
const button = screen.getByRole("button", { name: /total swatches/i });
const panelId = button.getAttribute("aria-controls");
const panel = panelId ? document.getElementById(panelId) : null;
expect(button).toBeInstanceOf(HTMLButtonElement);
expect(button.getAttribute("type")).toBe("button");
expect(button.getAttribute("aria-expanded")).toBe("false");
expect(panel).not.toBeNull();
expect(panel?.hidden).toBe(true);
button.focus();
expect(document.activeElement).toBe(button);
fireEvent.click(button);
expect(onToggle).toHaveBeenCalledOnce();
rerender(
icon}
expanded
onToggle={onToggle}
>
Manage swatches
,
);
expect(button.getAttribute("aria-expanded")).toBe("true");
expect(panel?.hidden).toBe(false);
expect(screen.getByRole("link", { name: "Manage swatches" })).toBeTruthy();
expect(button.querySelector("a")).toBeNull();
});
});