Files
clean-architecture-frontend…/tests/component/pagination.test.tsx
donghyeon-kaandClaude Opus 5.5 cef6744299 fix(presentation): leave modified pagination clicks to the browser (08-12)
The page links prevented every click, so Ctrl/Cmd/Shift-click changed the page
in place instead of opening a new tab.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 13:06:01 +09:00

119 lines
3.3 KiB
TypeScript

// @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 (
<Pagination
label="Pages"
nextLabel="Next"
onChange={() => {}}
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(
<CollectionPage heading={{ title: "Collection" }} pagination={pagination(1, 2)}>
Results
</CollectionPage>,
);
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(
<Pagination
label="Pages"
nextLabel="Next"
onChange={onChange}
page={1}
pageCount={3}
pageLabel={(candidate) => `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);
});
});