import { act, fireEvent, render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { afterEach, describe, expect, it, vi } from "vitest"; import { Disclosure, DisclosureContent } from "@/components/ui/disclosure"; import { ExpandableText } from "@/components/ui/expandable-text"; afterEach(() => { vi.restoreAllMocks(); vi.unstubAllGlobals(); }); describe("shared disclosures", () => { it("releases disposable content only after all closing transitions finish", async () => { const closed = vi.fn(); const { container, rerender } = render(Diff); let finishHeight!: () => void; let finishOpacity!: () => void; const height = new Promise((resolve) => { finishHeight = resolve; }); const opacity = new Promise((resolve) => { finishOpacity = resolve; }); Object.defineProperty(container.firstElementChild, "getAnimations", { value: () => [{ finished: height }, { finished: opacity }], }); rerender(Diff); expect(closed).not.toHaveBeenCalled(); expect(container.firstElementChild).toHaveAttribute("inert"); await act(async () => { finishHeight(); }); expect(closed).not.toHaveBeenCalled(); await act(async () => { finishOpacity(); }); expect(closed).toHaveBeenCalledOnce(); }); it.each(["reopen", "unmount"])("does not release content after %s interrupts a close", async (action) => { const closed = vi.fn(); let finish!: () => void; const finished = new Promise((resolve) => { finish = resolve; }); const { container, rerender, unmount } = render(Diff); Object.defineProperty(container.firstElementChild, "getAnimations", { value: () => [{ finished }] }); rerender(Diff); if (action === "reopen") rerender(Diff); else unmount(); await act(async () => { finish(); }); expect(closed).not.toHaveBeenCalled(); }); it("finishes immediately without motion and also handles a cancelled transition", async () => { const closed = vi.fn(); const { container, rerender } = render(Diff); const animations = vi.fn((): Array<{ finished: Promise }> => []); Object.defineProperty(container.firstElementChild, "getAnimations", { value: animations }); rerender(Diff); expect(closed).toHaveBeenCalledOnce(); rerender(Diff); let cancel!: () => void; const finished = new Promise((_resolve, reject) => { cancel = () => reject(new Error("motion disabled")); }); animations.mockReturnValue([{ finished }]); rerender(Diff); await act(async () => { cancel(); }); expect(closed).toHaveBeenCalledTimes(2); }); it("animates both ways without discarding form drafts, and hides collapsed controls", async () => { const user = userEvent.setup(); render(); const toggle = screen.getByRole("button", { name: "More details" }); const content = document.getElementById(toggle.getAttribute("aria-controls")!)!; expect(content).toHaveClass("inline-disclosure"); expect(content).toHaveAttribute("data-state", "closed"); expect(content).toHaveAttribute("inert"); expect(screen.queryByRole("textbox")).not.toBeInTheDocument(); toggle.focus(); await user.keyboard("{Enter}"); expect(toggle).toHaveAttribute("aria-expanded", "true"); expect(content).not.toHaveAttribute("inert"); const input = screen.getByRole("textbox", { name: "Draft" }); await user.clear(input); await user.type(input, "unsaved draft"); await user.click(toggle); expect(toggle).toHaveFocus(); expect(content).toHaveAttribute("data-state", "closed"); expect(content).toHaveAttribute("aria-hidden", "true"); expect(content).toHaveAttribute("inert"); expect(input).toBeInTheDocument(); await user.keyboard(" "); expect(screen.getByRole("textbox", { name: "Draft" })).toBe(input); expect(input).toHaveValue("unsaved draft"); }); it("keeps independent IDs and handles rapid reversals without timers or stale closing state", () => { render(<>OneTwo); const first = screen.getByRole("button", { name: "First" }); const second = screen.getByRole("button", { name: "Second" }); expect(first.getAttribute("aria-controls")).not.toBe(second.getAttribute("aria-controls")); const content = document.getElementById(first.getAttribute("aria-controls")!)!; for (let index = 0; index < 7; index++) fireEvent.click(first); expect(content).toHaveAttribute("data-state", "open"); expect(second).toHaveAttribute("aria-expanded", "false"); fireEvent.click(first); expect(content).toHaveAttribute("data-state", "closed"); }); }); describe("expandable text", () => { it("measures natural text height, handles resize and content changes, and preserves one text node", () => { let fullHeight = 280; let resize: ResizeObserverCallback | undefined; const disconnect = vi.fn(); vi.spyOn(HTMLElement.prototype, "scrollHeight", "get").mockImplementation(() => fullHeight); const getStyle = window.getComputedStyle.bind(window); vi.spyOn(window, "getComputedStyle").mockImplementation((node) => node.tagName === "P" ? { lineHeight: "20px" } as CSSStyleDeclaration : getStyle(node)); vi.stubGlobal("ResizeObserver", class { constructor(callback: ResizeObserverCallback) { resize = callback; } observe() {} disconnect = disconnect; }); const { container, rerender, unmount } = render( Long instructions, ); const viewport = container.firstElementChild!; const text = screen.getByText("Long instructions"); expect(viewport).toHaveStyle({ height: "120px" }); rerender(Long instructions); expect(viewport).toHaveStyle({ height: "280px" }); expect(screen.getByText("Long instructions")).toBe(text); fullHeight = 380; act(() => resize?.([], {} as ResizeObserver)); expect(viewport).toHaveStyle({ height: "380px" }); rerender(Long instructions); expect(viewport).toHaveStyle({ height: "120px" }); fullHeight = 40; rerender(Short replacement); expect(viewport).toHaveStyle({ height: "40px" }); expect(container.querySelectorAll("p")).toHaveLength(1); unmount(); expect(disconnect).toHaveBeenCalled(); }); });