4e0058d46c
- Unit: repoint moved screen imports (lib/screens, lib/game), mock $lib/app-nav (appScreen/activeView) instead of $app/navigation, drop the removed gameId props, assert screen/view selection. - e2e: add a dev-only window.__galaxyNav affordance; specs enter a game via enterGame(...) instead of a /games/:id URL; URL assertions become content assertions (the URL stays /game/); reload uses waitUntil:"commit" (shallow routing) and mocks /rpc on game entry. - Remove the obsolete report scroll-restore test (it relied on a SvelteKit route Snapshot that no longer exists); update the missing-membership test to the new lobby-redirect+toast behaviour. Fix a stale report.svelte docstring. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
159 lines
5.4 KiB
TypeScript
159 lines
5.4 KiB
TypeScript
// Vitest coverage for the Phase 23 Report View's table of contents.
|
|
// Smokes the anchor list, the active-link state, the back-to-map
|
|
// navigation, and the mobile <select> fallback. The
|
|
// IntersectionObserver-driven active-section computation lives in
|
|
// the orchestrator (`report.svelte`); this test only checks the
|
|
// presentational pieces of the TOC.
|
|
|
|
import "@testing-library/jest-dom/vitest";
|
|
import { fireEvent, render } from "@testing-library/svelte";
|
|
import { beforeEach, describe, expect, test, vi } from "vitest";
|
|
|
|
import { i18n } from "../src/lib/i18n/index.svelte";
|
|
import type { TranslationKey } from "../src/lib/i18n/index.svelte";
|
|
|
|
// The TOC's "back to map" button switches the active in-game view via
|
|
// `activeView.select("map")` (the single-URL app-shell has no
|
|
// `/games/:id/map` route). Mock the nav store so the spy captures the
|
|
// view switch and no real `pushState` runs.
|
|
const activeViewSelectMock = vi.hoisted(() => vi.fn());
|
|
vi.mock("$lib/app-nav.svelte", () => ({
|
|
activeView: { select: activeViewSelectMock },
|
|
}));
|
|
|
|
import ReportToc, {
|
|
type TocEntry,
|
|
} from "../src/lib/active-view/report/report-toc.svelte";
|
|
|
|
const ENTRIES: readonly TocEntry[] = [
|
|
{ slug: "galaxy-summary", titleKey: "game.report.section.galaxy_summary.title" },
|
|
{ slug: "votes", titleKey: "game.report.section.votes.title" },
|
|
{ slug: "bombings", titleKey: "game.report.section.bombings.title" },
|
|
];
|
|
|
|
beforeEach(() => {
|
|
i18n.resetForTests("en");
|
|
activeViewSelectMock.mockClear();
|
|
});
|
|
|
|
describe("report TOC", () => {
|
|
test("renders one anchor per entry and one option in the mobile select", () => {
|
|
const ui = render(ReportToc, {
|
|
props: { entries: ENTRIES, activeSlug: "galaxy-summary" },
|
|
});
|
|
for (const e of ENTRIES) {
|
|
expect(ui.getByTestId(`report-toc-${e.slug}`)).toBeInTheDocument();
|
|
}
|
|
const mobile = ui.getByTestId("report-toc-mobile") as HTMLSelectElement;
|
|
expect(mobile.options).toHaveLength(ENTRIES.length);
|
|
expect(mobile.value).toBe("galaxy-summary");
|
|
});
|
|
|
|
test("marks the active anchor with aria-current=location and a class", () => {
|
|
const ui = render(ReportToc, {
|
|
props: { entries: ENTRIES, activeSlug: "bombings" },
|
|
});
|
|
const active = ui.getByTestId("report-toc-bombings");
|
|
expect(active).toHaveAttribute("aria-current", "location");
|
|
expect(active).toHaveClass("active");
|
|
|
|
const inactive = ui.getByTestId("report-toc-votes");
|
|
expect(inactive).not.toHaveAttribute("aria-current");
|
|
expect(inactive).not.toHaveClass("active");
|
|
});
|
|
|
|
test("back-to-map button switches the active view to the map", async () => {
|
|
const ui = render(ReportToc, {
|
|
props: {
|
|
entries: ENTRIES,
|
|
activeSlug: "galaxy-summary",
|
|
},
|
|
});
|
|
const button = ui.getByTestId("report-back-to-map");
|
|
await fireEvent.click(button);
|
|
expect(activeViewSelectMock).toHaveBeenCalledWith("map");
|
|
});
|
|
|
|
test("anchor click cancels the default jump and calls scrollIntoView on the target", async () => {
|
|
// Stub `scrollIntoView` on the target — jsdom does not
|
|
// implement it. The TOC also reads
|
|
// `prefers-reduced-motion`; the matchMedia stub forces a
|
|
// stable `behavior: "auto"` so the assertion is reproducible.
|
|
const scrollSpy = vi.fn();
|
|
const target = document.createElement("section");
|
|
target.id = "report-bombings";
|
|
target.scrollIntoView = scrollSpy;
|
|
document.body.appendChild(target);
|
|
|
|
Object.defineProperty(window, "matchMedia", {
|
|
writable: true,
|
|
value: (query: string) => ({
|
|
matches: query.includes("reduce"),
|
|
media: query,
|
|
onchange: null,
|
|
addListener: () => {},
|
|
removeListener: () => {},
|
|
addEventListener: () => {},
|
|
removeEventListener: () => {},
|
|
dispatchEvent: () => false,
|
|
}),
|
|
});
|
|
|
|
const ui = render(ReportToc, {
|
|
props: { entries: ENTRIES, activeSlug: "galaxy-summary" },
|
|
});
|
|
await fireEvent.click(ui.getByTestId("report-toc-bombings"));
|
|
expect(scrollSpy).toHaveBeenCalledWith({
|
|
behavior: "auto",
|
|
block: "start",
|
|
});
|
|
target.remove();
|
|
});
|
|
|
|
test("mobile select scrolls to the chosen section without navigating", async () => {
|
|
const scrollSpy = vi.fn();
|
|
const target = document.createElement("section");
|
|
target.id = "report-votes";
|
|
target.scrollIntoView = scrollSpy;
|
|
document.body.appendChild(target);
|
|
|
|
Object.defineProperty(window, "matchMedia", {
|
|
writable: true,
|
|
value: () => ({
|
|
matches: false,
|
|
media: "(prefers-reduced-motion: no-preference)",
|
|
onchange: null,
|
|
addListener: () => {},
|
|
removeListener: () => {},
|
|
addEventListener: () => {},
|
|
removeEventListener: () => {},
|
|
dispatchEvent: () => false,
|
|
}),
|
|
});
|
|
|
|
const ui = render(ReportToc, {
|
|
props: {
|
|
entries: ENTRIES,
|
|
activeSlug: "galaxy-summary",
|
|
},
|
|
});
|
|
const select = ui.getByTestId("report-toc-mobile") as HTMLSelectElement;
|
|
await fireEvent.change(select, { target: { value: "votes" } });
|
|
expect(scrollSpy).toHaveBeenCalled();
|
|
expect(activeViewSelectMock).not.toHaveBeenCalled();
|
|
target.remove();
|
|
});
|
|
|
|
// Tests intentionally validate the *type* of the entries prop is
|
|
// exposed correctly so future widening of the list does not
|
|
// silently drop entries. TypeScript already enforces this through
|
|
// `TocEntry`; the assertion below is a soft check so a stray
|
|
// `as unknown as ...` cast surfaces fast.
|
|
test("TocEntry exposes a slug and a TranslationKey", () => {
|
|
const slug: string = ENTRIES[0]!.slug;
|
|
const key: TranslationKey = ENTRIES[0]!.titleKey;
|
|
expect(typeof slug).toBe("string");
|
|
expect(typeof key).toBe("string");
|
|
});
|
|
});
|