|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { UseUnarchiveTask } from "./useUnarchiveTask"; |
| 3 | + |
| 4 | +const toastSuccess = vi.hoisted(() => vi.fn()); |
| 5 | +const toastError = vi.hoisted(() => vi.fn()); |
| 6 | + |
| 7 | +vi.mock("@posthog/ui/primitives/toast", () => ({ |
| 8 | + toast: { success: toastSuccess, error: toastError }, |
| 9 | +})); |
| 10 | +vi.mock("@posthog/ui/shell/logger", () => ({ |
| 11 | + logger: { scope: () => ({ error: vi.fn(), info: vi.fn(), warn: vi.fn() }) }, |
| 12 | +})); |
| 13 | + |
| 14 | +import { undoArchive } from "./undoArchive"; |
| 15 | + |
| 16 | +type Restore = UseUnarchiveTask["restore"]; |
| 17 | + |
| 18 | +describe("undoArchive", () => { |
| 19 | + beforeEach(() => { |
| 20 | + toastSuccess.mockClear(); |
| 21 | + toastError.mockClear(); |
| 22 | + }); |
| 23 | + |
| 24 | + it("restores on the first attempt and confirms with a toast", async () => { |
| 25 | + const restore = vi |
| 26 | + .fn() |
| 27 | + .mockResolvedValue({ kind: "restored", navigateToTaskId: "task-1" }); |
| 28 | + |
| 29 | + await undoArchive("task-1", restore as unknown as Restore); |
| 30 | + |
| 31 | + expect(restore).toHaveBeenCalledTimes(1); |
| 32 | + expect(restore).toHaveBeenCalledWith("task-1", true); |
| 33 | + expect(toastSuccess).toHaveBeenCalledWith("Task archive undone"); |
| 34 | + expect(toastError).not.toHaveBeenCalled(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("retries with branch recreation when the branch is missing", async () => { |
| 38 | + const restore = vi |
| 39 | + .fn() |
| 40 | + .mockResolvedValueOnce({ |
| 41 | + kind: "branch-not-found", |
| 42 | + taskId: "task-1", |
| 43 | + branchName: "feat/x", |
| 44 | + }) |
| 45 | + .mockResolvedValueOnce({ kind: "restored", navigateToTaskId: "task-1" }); |
| 46 | + |
| 47 | + await undoArchive("task-1", restore as unknown as Restore); |
| 48 | + |
| 49 | + expect(restore).toHaveBeenNthCalledWith(1, "task-1", true); |
| 50 | + expect(restore).toHaveBeenNthCalledWith(2, "task-1", true, { |
| 51 | + recreateBranch: true, |
| 52 | + }); |
| 53 | + expect(toastSuccess).toHaveBeenCalledWith("Task archive undone"); |
| 54 | + expect(toastError).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it("shows an error toast when the branch is still missing after retry", async () => { |
| 58 | + const restore = vi.fn().mockResolvedValue({ |
| 59 | + kind: "branch-not-found", |
| 60 | + taskId: "task-1", |
| 61 | + branchName: "feat/x", |
| 62 | + }); |
| 63 | + |
| 64 | + await undoArchive("task-1", restore as unknown as Restore); |
| 65 | + |
| 66 | + expect(restore).toHaveBeenCalledTimes(2); |
| 67 | + expect(toastError).toHaveBeenCalledWith( |
| 68 | + "Failed to restore task: branch 'feat/x' not found", |
| 69 | + ); |
| 70 | + expect(toastSuccess).not.toHaveBeenCalled(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("surfaces the error message when restore reports an error", async () => { |
| 74 | + const restore = vi |
| 75 | + .fn() |
| 76 | + .mockResolvedValue({ kind: "error", message: "server boom" }); |
| 77 | + |
| 78 | + await undoArchive("task-1", restore as unknown as Restore); |
| 79 | + |
| 80 | + expect(toastError).toHaveBeenCalledWith( |
| 81 | + "Failed to restore task: server boom", |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it("shows a generic error toast when restore throws", async () => { |
| 86 | + const restore = vi.fn().mockRejectedValue(new Error("network down")); |
| 87 | + |
| 88 | + await undoArchive("task-1", restore as unknown as Restore); |
| 89 | + |
| 90 | + expect(toastError).toHaveBeenCalledWith("Failed to restore task"); |
| 91 | + expect(toastSuccess).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("ignores a repeated undo while the first is still in flight", async () => { |
| 95 | + let resolveRestore: (value: unknown) => void = () => {}; |
| 96 | + const restore = vi.fn().mockImplementation( |
| 97 | + () => |
| 98 | + new Promise((resolve) => { |
| 99 | + resolveRestore = resolve; |
| 100 | + }), |
| 101 | + ); |
| 102 | + |
| 103 | + const first = undoArchive("task-1", restore as unknown as Restore); |
| 104 | + const second = undoArchive("task-1", restore as unknown as Restore); |
| 105 | + resolveRestore({ kind: "restored", navigateToTaskId: "task-1" }); |
| 106 | + await Promise.all([first, second]); |
| 107 | + |
| 108 | + expect(restore).toHaveBeenCalledTimes(1); |
| 109 | + expect(toastSuccess).toHaveBeenCalledTimes(1); |
| 110 | + }); |
| 111 | +}); |
0 commit comments