Files
horus-3d/tests/unit/rafBatcher.test.ts
Luke The Dev 4fa4f13558 First Release of Claw3D (#11)
Co-authored-by: iamlukethedev <iamlukethedev@users.noreply.github.com>
2026-03-19 23:14:04 -05:00

48 lines
1.4 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { createRafBatcher } from "@/lib/dom";
describe("createRafBatcher", () => {
const originalRaf = globalThis.requestAnimationFrame;
const originalCaf = globalThis.cancelAnimationFrame;
afterEach(() => {
globalThis.requestAnimationFrame = originalRaf;
globalThis.cancelAnimationFrame = originalCaf;
});
it("flushes at most once per animation frame", () => {
const flush = vi.fn();
let queued: unknown = null;
globalThis.requestAnimationFrame = vi.fn((cb: (time: number) => void) => {
queued = cb;
return 1;
});
globalThis.cancelAnimationFrame = vi.fn();
const batcher = createRafBatcher(flush);
batcher.schedule();
batcher.schedule();
batcher.schedule();
expect(flush).not.toHaveBeenCalled();
if (typeof queued !== "function") {
throw new Error("requestAnimationFrame was not scheduled.");
}
(queued as (time: number) => void)(0);
expect(flush).toHaveBeenCalledTimes(1);
});
it("cancels a scheduled flush", () => {
const flush = vi.fn();
globalThis.requestAnimationFrame = vi.fn(() => 123);
globalThis.cancelAnimationFrame = vi.fn();
const batcher = createRafBatcher(flush);
batcher.schedule();
batcher.cancel();
expect(globalThis.cancelAnimationFrame).toHaveBeenCalledWith(123);
});
});