// @vitest-environment jsdom import { fireEvent, render, screen, within } from "@testing-library/react"; import { describe, expect, it, vi } from "vitest"; import { Pagination } from "../../src/presentation/design-system/index.ts"; import { CollectionPage } from "../../src/presentation/templates/index.ts"; function pagination(page: number, pageCount: number) { return ( {}} page={page} pageCount={pageCount} pageLabel={(candidate) => `Page ${candidate}`} previousLabel="Previous" /> ); } function pageLinks(): string[] { return screen .getAllByRole("link") .map((link) => link.textContent ?? ""); } describe("Pagination windowing", () => { it("links the first, last and neighbouring pages only", () => { render(pagination(50, 5000)); expect(pageLinks()).toEqual([ "Page 1", "Page 49", "Page 50", "Page 51", "Page 5000", ]); expect(screen.getByRole("link", { name: "Page 50" })).toHaveAttribute( "aria-current", "page", ); }); it("links a single skipped page instead of eliding it", () => { render(pagination(3, 5000)); expect(pageLinks()).toEqual([ "Page 1", "Page 2", "Page 3", "Page 4", "Page 5000", ]); }); it("keeps every page of a short range", () => { render(pagination(1, 3)); expect(pageLinks()).toEqual(["Page 1", "Page 2", "Page 3"]); }); }); describe("Pagination landmarks", () => { it("is a navigation landmark on its own", () => { render(pagination(1, 2)); expect(screen.getByRole("navigation", { name: "Pages" })).toBeVisible(); }); it("does not nest a navigation landmark inside the collection page slot", () => { render( Results , ); const slot = screen.getByRole("navigation", { name: "페이지 탐색" }); expect(within(slot).queryAllByRole("navigation")).toHaveLength(0); expect(within(slot).getByRole("group", { name: "Pages" })).toBeVisible(); }); }); // 08-12. A modified click is the browser's (new tab/window), not a page change. describe("Pagination modified clicks", () => { function renderWith(onChange: (page: number) => void) { render( `Page ${candidate}`} previousLabel="Previous" />, ); return screen.getByRole("link", { name: "Page 2" }); } it.each([ ["Ctrl", { ctrlKey: true }], ["Cmd", { metaKey: true }], ["Shift", { shiftKey: true }], ["Alt", { altKey: true }], ])("leaves a %s click to the browser", (_, modifier) => { const onChange = vi.fn(); const link = renderWith(onChange); const notPrevented = fireEvent.click(link, { button: 0, ...modifier }); expect(notPrevented).toBe(true); expect(onChange).not.toHaveBeenCalled(); }); it("changes the page on a plain primary click", () => { const onChange = vi.fn(); const link = renderWith(onChange); expect(fireEvent.click(link, { button: 0 })).toBe(false); expect(onChange).toHaveBeenCalledWith(2); }); });