Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081import { afterEach, beforeEach, expect, it, vi } from "vitest";import { cleanup, render } from "@solidjs/testing-library";import { createSignal } from "solid-js";
import { AlbumGrid } from "../../components/AlbumGrid";import { albumCells } from "./grid";import type { LibraryRow } from "./rows";import type { GridTraceConsole } from "./gridtrace";
/** * The `[grid]` line has to be trustworthy before anyone debugs with it: it must * say something when the gallery rebuilds itself for nothing, and stay quiet * when the gallery is simply standing there. Both halves are asserted here, * because a probe that prints on every mount gets muted within a session and a * probe that never prints sends someone hunting a bug that is not there. */
const WINDOW = 500; // gridtrace's WINDOW_MS; the burst is flushed this far outconst SHELF = 12;
const rows: LibraryRow[] = Array.from({ length: SHELF }, (_, i) => ({ kind: "album", key: `al-${i}`, label: `Album ${i}`, artist: `Artist ${i}`, albumId: `al-${i}`, songs: 3,})) as unknown as LibraryRow[];
const api = () => (window as unknown as { __gridTrace: GridTraceConsole }).__gridTrace;
const gridLines = (spy: { mock: { calls: unknown[][] } }) => spy.mock.calls.map((c) => String(c[0])).filter((line) => line.startsWith("[grid]"));
/** Let the burst's window elapse so the line is flushed. */async function settled(): Promise<void> { await new Promise((resolve) => setTimeout(resolve, WINDOW * 1.6));}
beforeEach(() => { api().reset();});
afterEach(() => { cleanup(); vi.restoreAllMocks();});
it("prints one line when the cells are rebuilt with nothing changed", async () => { const debug = vi.spyOn(console, "debug").mockImplementation(() => {}); const [cells, setCells] = createSignal(albumCells(rows)); render(() => <AlbumGrid cells={cells()} onSelect={() => {}} columns={4} viewportHeight={400} />);
// Fresh arrays, identical content: what an upstream memo republishing looks // like to <For>, which keys rows by array identity and so rebuilds the window. for (let i = 0; i < 5; i++) { setCells(albumCells(rows)); await Promise.resolve(); } await settled();
const lines = gridLines(debug).filter((l) => !l.includes("counters reset")); expect(lines).toHaveLength(1); const [line] = lines; expect(line).toContain("grid-rows"); // Every rebuild mints every cell again: 6 runs × 4 columns of the 12 albums. expect(line).toMatch(/cells \d+/); expect(api().dump).toBeTypeOf("function");});
it("prints nothing about a grid that is only standing still", async () => { const debug = vi.spyOn(console, "debug").mockImplementation(() => {}); render(() => ( <AlbumGrid cells={albumCells(rows)} onSelect={() => {}} columns={4} viewportHeight={400} /> )); await settled();
// One mount, one recompute: a grid arriving is not a grid churning. expect(gridLines(debug)).toEqual([]);});