|
| 1 | +import { AsyncResult } from "effect/unstable/reactivity"; |
| 2 | +import * as Cause from "effect/Cause"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
| 4 | + |
| 5 | +import { toastManager } from "../components/ui/toast"; |
| 6 | +import { showUndoToast } from "./showUndoToast"; |
| 7 | +import * as ThreadUndo from "./threadUndo"; |
| 8 | + |
| 9 | +afterEach(() => vi.restoreAllMocks()); |
| 10 | + |
| 11 | +function setup() { |
| 12 | + const add = vi.spyOn(toastManager, "add").mockReturnValue("undo-toast"); |
| 13 | + const close = vi.spyOn(toastManager, "close").mockImplementation(() => {}); |
| 14 | + const undo = vi.fn(async () => AsyncResult.success(undefined)); |
| 15 | + const claim = ThreadUndo.begin("pin", "env/thread"); |
| 16 | + const options = { |
| 17 | + title: "Thread unpinned", |
| 18 | + description: "Thread", |
| 19 | + failureTitle: "Restore failed", |
| 20 | + undo, |
| 21 | + claim, |
| 22 | + }; |
| 23 | + return { add, close, undo, claim, options }; |
| 24 | +} |
| 25 | + |
| 26 | +function click(add: ReturnType<typeof setup>["add"], index = 0) { |
| 27 | + const handler = add.mock.calls[index]?.[0].actionProps?.onClick; |
| 28 | + if (!handler) throw new Error("Undo action is missing"); |
| 29 | + return handler({} as Parameters<typeof handler>[0]); |
| 30 | +} |
| 31 | + |
| 32 | +describe("showUndoToast", () => { |
| 33 | + it("ignores a stale toast and lets the latest action run only once", async () => { |
| 34 | + const { add, close, undo, options } = setup(); |
| 35 | + showUndoToast(options); |
| 36 | + ThreadUndo.invalidate("pin", "env/thread"); |
| 37 | + showUndoToast({ ...options, claim: ThreadUndo.begin("pin", "env/thread") }); |
| 38 | + await click(add); |
| 39 | + expect(undo).not.toHaveBeenCalled(); |
| 40 | + await click(add, 1); |
| 41 | + await click(add, 1); |
| 42 | + expect(undo).toHaveBeenCalledOnce(); |
| 43 | + expect(close).toHaveBeenCalledExactlyOnceWith("undo-toast"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("releases the claim on close and rejects a later click", async () => { |
| 47 | + const { add, undo, claim, options } = setup(); |
| 48 | + showUndoToast(options); |
| 49 | + add.mock.calls[0]?.[0].onClose?.(); |
| 50 | + expect(claim.isCurrent()).toBe(false); |
| 51 | + await click(add); |
| 52 | + expect(undo).not.toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("does not show a toast for a late completion after a newer action", () => { |
| 56 | + const { add, options } = setup(); |
| 57 | + ThreadUndo.invalidate("pin", "env/thread"); |
| 58 | + showUndoToast(options); |
| 59 | + expect(add).not.toHaveBeenCalled(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("reports a failed restore and releases its claim", async () => { |
| 63 | + const { add, claim, options } = setup(); |
| 64 | + showUndoToast({ |
| 65 | + ...options, |
| 66 | + undo: async () => AsyncResult.failure(Cause.fail(new Error("offline"))), |
| 67 | + }); |
| 68 | + await click(add); |
| 69 | + expect(add).toHaveBeenLastCalledWith( |
| 70 | + expect.objectContaining({ type: "error", title: "Restore failed", description: "offline" }), |
| 71 | + ); |
| 72 | + expect(claim.isCurrent()).toBe(false); |
| 73 | + }); |
| 74 | + |
| 75 | + it("reports a rejected restore promise", async () => { |
| 76 | + const { add, options } = setup(); |
| 77 | + showUndoToast({ |
| 78 | + ...options, |
| 79 | + undo: async () => { |
| 80 | + throw new Error("disconnected"); |
| 81 | + }, |
| 82 | + }); |
| 83 | + await click(add); |
| 84 | + expect(add).toHaveBeenLastCalledWith( |
| 85 | + expect.objectContaining({ type: "error", description: "disconnected" }), |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it("does not report interrupted restores as errors", async () => { |
| 90 | + const { add, options } = setup(); |
| 91 | + showUndoToast({ ...options, undo: async () => AsyncResult.failure(Cause.interrupt()) }); |
| 92 | + await click(add); |
| 93 | + expect(add).toHaveBeenCalledOnce(); |
| 94 | + }); |
| 95 | +}); |
0 commit comments