import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { ExpandableContent } from "./expandable-content"; describe("ExpandableContent", () => { let scrollHeightSpy: ReturnType | undefined; afterEach(() => { scrollHeightSpy?.mockRestore(); scrollHeightSpy = undefined; }); it("renders no toggle when content fits within the cap", () => { render(
short
, ); expect( screen.queryByRole("button", { name: /show more/i }), ).not.toBeInTheDocument(); }); describe("when content overflows", () => { beforeEach(() => { scrollHeightSpy = vi .spyOn(HTMLElement.prototype, "scrollHeight", "get") .mockReturnValue(999); }); it("clamps with a Show more toggle that expands and collapses", async () => { const user = userEvent.setup(); render(
tall content
, ); const showMore = screen.getByRole("button", { name: /show more/i }); await user.click(showMore); expect( screen.getByRole("button", { name: /show less/i }), ).toBeInTheDocument(); await user.click(screen.getByRole("button", { name: /show less/i })); expect( screen.getByRole("button", { name: /show more/i }), ).toBeInTheDocument(); }); }); });