Files
clean-architecture-frontend…/tests/component/design-system-links.test.tsx
T
DongHyeonkaandClaude Opus 5.5 a535db6ea1 fix(presentation): render design-system links through the router (Q-6.8-9)
LinkButton, Breadcrumbs items and page link actions accept an additive
`to` target rendered as a router Link, so in-app links keep the router
basename and navigate without a document reload. `href` still renders a
plain anchor. Pagination builds its page links from the current search
(router location, or the document URL outside a router) and only replaces
`page`, so other search parameters survive.

Verified with tests/component/design-system-links.test.tsx (RED: 5 of 6
failed before the change) and the lane gates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 13:44:38 +09:00

157 lines
4.7 KiB
TypeScript

// @vitest-environment jsdom
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import type { ReactNode } from "react";
import { MemoryRouter, Route, Routes, useLocation } from "react-router-dom";
import { describe, expect, it, vi } from "vitest";
import {
Breadcrumbs,
LinkButton,
Pagination,
} from "../../src/presentation/design-system/index.ts";
import { StandardPage } from "../../src/presentation/templates/index.ts";
function LocationProbe() {
const location = useLocation();
return <output data-testid="location">{`${location.pathname}${location.search}`}</output>;
}
function renderInRouter(ui: ReactNode, entry = "/app/items?q=term&sort=name") {
return render(
<MemoryRouter basename="/app" initialEntries={[entry]}>
<Routes>
<Route
path="*"
element={
<>
{ui}
<LocationProbe />
</>
}
/>
</Routes>
</MemoryRouter>,
);
}
describe("design-system links inside the router", () => {
it("renders LinkButton `to` as a router link that keeps the basename", async () => {
const user = userEvent.setup();
renderInRouter(<LinkButton to="/examples/states">States</LinkButton>);
const link = screen.getByRole("link", { name: "States" });
expect(link).toHaveAttribute("href", "/app/examples/states");
expect(link).toHaveClass("ui-button", "ui-button--primary");
await user.click(link);
expect(screen.getByTestId("location")).toHaveTextContent(
/^\/examples\/states$/,
);
});
it("keeps LinkButton `href` as a plain anchor", () => {
renderInRouter(<LinkButton href="https://example.com/docs">Docs</LinkButton>);
expect(screen.getByRole("link", { name: "Docs" })).toHaveAttribute(
"href",
"https://example.com/docs",
);
});
it("renders breadcrumb `to` items as router links", async () => {
const user = userEvent.setup();
renderInRouter(
<Breadcrumbs
items={[
{ label: "Home", to: "/" },
{ label: "Examples", to: "/examples" },
{ label: "States" },
]}
label="Location"
/>,
);
expect(screen.getByRole("link", { name: "Home" })).toHaveAttribute(
"href",
"/app",
);
await user.click(screen.getByRole("link", { name: "Examples" }));
expect(screen.getByTestId("location")).toHaveTextContent(/^\/examples$/);
expect(screen.getByText("States")).toHaveAttribute("aria-current", "page");
});
it("renders page link actions with `to` as router links", async () => {
const user = userEvent.setup();
renderInRouter(
<StandardPage
actions={[
{ kind: "link", label: "Open states", to: "/examples/states" },
{ kind: "link", label: "External", href: "https://example.com" },
]}
heading={{ title: "Page" }}
/>,
);
expect(screen.getByRole("link", { name: "External" })).toHaveAttribute(
"href",
"https://example.com",
);
const link = screen.getByRole("link", { name: "Open states" });
expect(link).toHaveAttribute("href", "/app/examples/states");
await user.click(link);
expect(screen.getByTestId("location")).toHaveTextContent(
/^\/examples\/states$/,
);
});
it("keeps the other search parameters and the basename in pagination links", async () => {
const user = userEvent.setup();
const onChange = vi.fn();
renderInRouter(
<Pagination
label="Pages"
nextLabel="Next"
onChange={onChange}
page={1}
pageCount={3}
pageLabel={(page) => `Page ${page}`}
previousLabel="Previous"
/>,
"/app/items?q=term&sort=name&page=1",
);
expect(screen.getByRole("link", { name: "Page 2" })).toHaveAttribute(
"href",
"/app/items?q=term&sort=name&page=2",
);
await user.click(screen.getByRole("link", { name: "Page 3" }));
expect(onChange).toHaveBeenCalledWith(3);
// Page changes stay with the owner: the link itself does not navigate.
expect(screen.getByTestId("location")).toHaveTextContent(
"/items?q=term&sort=name&page=1",
);
});
});
describe("pagination outside a router", () => {
it("keeps the other search parameters of the document URL", () => {
window.history.pushState({}, "", "/list?q=term");
render(
<Pagination
label="Pages"
nextLabel="Next"
onChange={() => {}}
page={1}
pageCount={2}
pageLabel={(page) => `Page ${page}`}
previousLabel="Previous"
/>,
);
expect(screen.getByRole("link", { name: "Page 2" })).toHaveAttribute(
"href",
"?q=term&page=2",
);
});
});