import { describe, expect, it, vi, beforeEach, afterEach } from "vitest" import { act, renderHook } from "@testing-library/react" import { useStagedGreeting } from "./use-staged-greeting" /** * Two things are worth pinning, because breaking either one is what made the * greeting invisible before: it must not fire in the mount frame, and it must * let go afterwards so a hover can still move the star. */ describe("useStagedGreeting", () => { beforeEach(() => { vi.useFakeTimers() }) afterEach(() => { vi.useRealTimers() }) it("does not greet in the mount frame", () => { const { result } = renderHook(() => useStagedGreeting(true)) expect(result.current).toBe(false) }) it("greets inside the chip's opening beat, not before it", () => { // The box starts widening 140ms in and takes 380ms (`globals.css`), so the // gesture has to have started by then and still be running while it moves. const { result } = renderHook(() => useStagedGreeting(true)) act(() => { vi.advanceTimersByTime(160) }) expect(result.current).toBe(true) act(() => { vi.advanceTimersByTime(360) }) expect(result.current).toBe(true) }) it("lets go, so hover is a state change again", () => { const { result } = renderHook(() => useStagedGreeting(true)) act(() => { vi.advanceTimersByTime(160 + 600) }) expect(result.current).toBe(false) }) it("stays still when there is no arrival to ride", () => { const { result } = renderHook(() => useStagedGreeting(false)) act(() => { vi.advanceTimersByTime(10_000) }) expect(result.current).toBe(false) }) it("drops its timers on unmount", () => { const clear = vi.spyOn(window, "clearTimeout") const { unmount } = renderHook(() => useStagedGreeting(true)) unmount() expect(clear).toHaveBeenCalled() clear.mockRestore() }) })