import { beforeEach, expect, it, vi } from 'vitest'; const hooks = vi.hoisted(() => ({slots: [] as any[], cursor: 0, cleanup: undefined as undefined | (() => void)})); vi.mock('react', async original => ({ ...await original(), useState(value: unknown) { const i = hooks.cursor++; return [hooks.slots[i] ?? value, (next: unknown) => { hooks.slots[i] = next; }]; }, useRef(value: unknown) { const i = hooks.cursor++; return hooks.slots[i] ??= {current: value}; }, useEffect(effect: () => () => void) { hooks.cleanup = effect(); }, })); import { useConfirm } from './components/ConfirmDialog'; beforeEach(() => { hooks.slots = []; hooks.cursor = 0; hooks.cleanup = undefined; }); it('cancels a superseded confirmation and only accepts the current request', async () => { const confirm = useConfirm(); const first = confirm.ask({title: 'first'}); const second = confirm.ask({title: 'second'}); expect(await first).toBe(false); confirm.element.props.onClose(true); expect(await second).toBe(true); }); it('unmount cancels a pending action instead of leaving it suspended', async () => { const confirm = useConfirm(); const pending = confirm.ask({title: 'delete'}); hooks.cleanup?.(); expect(await pending).toBe(false); confirm.element.props.onClose(true); expect(await pending).toBe(false); });