90 lines
2.7 KiB
TypeScript
90 lines
2.7 KiB
TypeScript
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import {
|
|
clearNavigationBlockerForTests,
|
|
setNavigationBlocker,
|
|
} from "@/lib/routing/navigation-guard";
|
|
import Link from "./app-link";
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
clearNavigationBlockerForTests();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe("AppLink", () => {
|
|
it("navigates same-origin links through browser history", () => {
|
|
window.history.replaceState({}, "", "/");
|
|
const scrollTo = vi.spyOn(window, "scrollTo").mockImplementation(() => undefined);
|
|
render(<Link href="/tasks">Tasks</Link>);
|
|
|
|
fireEvent.click(screen.getByText("Tasks"));
|
|
|
|
expect(window.location.pathname).toBe("/tasks");
|
|
expect(scrollTo).toHaveBeenCalledWith({ top: 0, left: 0 });
|
|
});
|
|
|
|
it("preserves caller click handlers", () => {
|
|
const onClick = vi.fn();
|
|
render(
|
|
<Link href="/tasks" onClick={onClick}>
|
|
Tasks
|
|
</Link>,
|
|
);
|
|
|
|
fireEvent.click(screen.getByText("Tasks"));
|
|
|
|
expect(onClick).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("does not intercept modified clicks", () => {
|
|
const pushState = vi.spyOn(window.history, "pushState");
|
|
render(<Link href="/tasks">Tasks</Link>);
|
|
|
|
fireEvent.click(screen.getByText("Tasks"), { metaKey: true });
|
|
|
|
expect(pushState).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not intercept hash-only links", () => {
|
|
const pushState = vi.spyOn(window.history, "pushState");
|
|
const scrollTo = vi.spyOn(window, "scrollTo").mockImplementation(() => undefined);
|
|
render(<Link href="#details">Details</Link>);
|
|
|
|
fireEvent.click(screen.getByText("Details"));
|
|
|
|
expect(pushState).not.toHaveBeenCalled();
|
|
expect(scrollTo).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("forwards refs to the anchor element", () => {
|
|
const ref = { current: null as HTMLAnchorElement | null };
|
|
render(
|
|
<Link ref={ref} href="/tasks">
|
|
Tasks
|
|
</Link>,
|
|
);
|
|
|
|
expect(ref.current).toBeInstanceOf(HTMLAnchorElement);
|
|
expect(ref.current?.getAttribute("href")).toBe("/tasks");
|
|
});
|
|
|
|
it("waits for a navigation blocker before changing routes", () => {
|
|
window.history.replaceState({}, "", "/settings/general/appearance");
|
|
let proceed: () => void = () => undefined;
|
|
const onLocationChange = vi.fn();
|
|
window.addEventListener("kandev:navigation", onLocationChange, { once: true });
|
|
setNavigationBlocker((intent) => {
|
|
proceed = intent.proceed;
|
|
});
|
|
render(<Link href="/tasks">Tasks</Link>);
|
|
|
|
fireEvent.click(screen.getByText("Tasks"));
|
|
|
|
expect(window.location.pathname).toBe("/settings/general/appearance");
|
|
proceed();
|
|
expect(window.location.pathname).toBe("/tasks");
|
|
expect(onLocationChange).toHaveBeenCalledOnce();
|
|
});
|
|
});
|