File size: 1,258 Bytes
689359a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import { afterEach, describe, expect, it, vi } from "vitest";
import { celebrate } from "./confetti";
afterEach(() => { vi.restoreAllMocks(); document.body.innerHTML = ""; });
describe("celebrate", () => {
it("draws over the page without catching clicks, then cleans up", () => {
vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue({ scale() {}, clearRect() {}, save() {}, restore() {}, translate() {}, rotate() {}, fillRect() {}, beginPath() {}, arc() {}, fill() {} } as never);
const frames: FrameRequestCallback[] = [];
vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => { frames.push(callback); return frames.length; });
celebrate({ x: 10, y: 10 });
const canvas = document.querySelector("canvas")!;
expect(canvas.style.pointerEvents).toBe("none");
expect(canvas).toHaveAttribute("aria-hidden", "true");
frames.shift()!(performance.now() + 10_000);
expect(document.querySelector("canvas")).toBeNull();
vi.unstubAllGlobals();
});
it("does nothing for people who asked for less motion", () => {
vi.stubGlobal("matchMedia", () => ({ matches: true }));
celebrate();
expect(document.querySelector("canvas")).toBeNull();
vi.unstubAllGlobals();
});
});
|