// @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 {`${location.pathname}${location.search}`}; } function renderInRouter(ui: ReactNode, entry = "/app/items?q=term&sort=name") { return render( {ui} } /> , ); } 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(States); 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(Docs); 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( , ); 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( , ); 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( `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( {}} page={1} pageCount={2} pageLabel={(page) => `Page ${page}`} previousLabel="Previous" />, ); expect(screen.getByRole("link", { name: "Page 2" })).toHaveAttribute( "href", "?q=term&page=2", ); }); });