mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-19 11:54:22 +01:00
Eleven tests split by theme: the five that assert the loop declines to start or tears down cleanly stay put; the six that actually play the game move to usePuzzleGameLoopPlay.test.ts. Fixtures went to src/test/puzzleLoopFixtures.ts (62) -- that path is required, not stylistic, since coverage excludes src/test/** and a fixture beside the hook would be counted as production code and break the 100% gate. flushTick could not move as-is: it closed over the describe block's rafCallbacks. It is now installRafHarness(), which owns the queue and hands back a bound flushTick, so the two suites cannot leak frames into each other. The check that matters for a test split is the pair (15 files, 145 tests): file count up by one, test count unchanged. A name outside vitest's glob would have passed just as green with six tests never running. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
126 lines
3.3 KiB
TypeScript
126 lines
3.3 KiB
TypeScript
/** Lifecycle and guard paths: when the loop declines to start, and teardown. */
|
|
|
|
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
import { renderHook, act } from "@testing-library/react";
|
|
import type { PuzzlePiece } from "../types";
|
|
import { usePuzzleGameLoop } from "./usePuzzleGameLoop";
|
|
|
|
vi.mock("../lib/sliceImage");
|
|
import { sliceImage } from "../lib/sliceImage";
|
|
import {
|
|
flushPromises,
|
|
installRafHarness,
|
|
makeCanvas,
|
|
makePiece,
|
|
makeRef,
|
|
} from "../test/puzzleLoopFixtures";
|
|
|
|
describe("usePuzzleGameLoop lifecycle", () => {
|
|
beforeEach(() => {
|
|
installRafHarness();
|
|
vi.mocked(sliceImage).mockResolvedValue([makePiece(0, 0)]);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it("returns null when active=false", () => {
|
|
const { result } = renderHook(() =>
|
|
usePuzzleGameLoop(
|
|
makeRef<HTMLCanvasElement | null>(makeCanvas()),
|
|
makeRef(400),
|
|
new File([""], "img.png"),
|
|
1,
|
|
false,
|
|
),
|
|
);
|
|
expect(result.current).toBeNull();
|
|
expect(sliceImage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns null when canvas is null", async () => {
|
|
const { result } = renderHook(() =>
|
|
usePuzzleGameLoop(
|
|
makeRef<HTMLCanvasElement | null>(null),
|
|
makeRef(400),
|
|
new File([""], "img.png"),
|
|
1,
|
|
true,
|
|
),
|
|
);
|
|
await flushPromises();
|
|
expect(result.current).toBeNull();
|
|
expect(requestAnimationFrame).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does nothing when unmounted before sliceImage resolves (cancelled branch)", async () => {
|
|
let resolveSlice!: (pieces: PuzzlePiece[]) => void;
|
|
vi.mocked(sliceImage).mockReturnValue(
|
|
new Promise((res) => { resolveSlice = res; }),
|
|
);
|
|
|
|
const { unmount } = renderHook(() =>
|
|
usePuzzleGameLoop(
|
|
makeRef<HTMLCanvasElement | null>(makeCanvas()),
|
|
makeRef(400),
|
|
new File([""], "img.png"),
|
|
1,
|
|
true,
|
|
),
|
|
);
|
|
|
|
unmount(); // sets cancelled=true before sliceImage resolves
|
|
await act(async () => {
|
|
resolveSlice([makePiece(0, 0)]);
|
|
await Promise.resolve();
|
|
});
|
|
|
|
expect(requestAnimationFrame).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("cancels RAF on unmount after game started", async () => {
|
|
vi.spyOn(Math, "random").mockReturnValue(0);
|
|
|
|
const { unmount } = renderHook(() =>
|
|
usePuzzleGameLoop(
|
|
makeRef<HTMLCanvasElement | null>(makeCanvas()),
|
|
makeRef(400),
|
|
new File([""], "img.png"),
|
|
1,
|
|
true,
|
|
),
|
|
);
|
|
|
|
await flushPromises();
|
|
unmount();
|
|
expect(cancelAnimationFrame).toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not start RAF when startLoop canvas.getContext returns null", async () => {
|
|
// A plain fake canvas whose getContext returns null avoids prototype contamination
|
|
const fakeCanvas = {
|
|
width: 800,
|
|
height: 300,
|
|
clientWidth: 800,
|
|
clientHeight: 300,
|
|
getContext: () => null,
|
|
} as unknown as HTMLCanvasElement;
|
|
|
|
renderHook(() =>
|
|
usePuzzleGameLoop(
|
|
makeRef<HTMLCanvasElement | null>(fakeCanvas),
|
|
makeRef(400),
|
|
new File([""], "img.png"),
|
|
1,
|
|
true,
|
|
),
|
|
);
|
|
|
|
await flushPromises(); // sliceImage resolves → startLoop called → null ctx → early return
|
|
|
|
expect(requestAnimationFrame).not.toHaveBeenCalled();
|
|
});
|
|
});
|