|
| 1 | +/** |
| 2 | + * StatusBar Backup button — Task 9 of the database-backup slice. |
| 3 | + * |
| 4 | + * Branches under test: |
| 5 | + * - Click Backup → api.backupDatabase resolves Ok → info toast with |
| 6 | + * absolute_path and formatted MB. |
| 7 | + * - Click Backup → api.backupDatabase resolves Err(BackupFailed/TargetExists) |
| 8 | + * → error toast that includes "already exists". |
| 9 | + * |
| 10 | + * WHY mock `../api` (not `@tauri-apps/api/core`): the perima frontend test |
| 11 | + * pattern mocks the API layer with `vi.importActual` partial spread, returning |
| 12 | + * `okAsync`/`errAsync` from neverthrow. Mocking `invoke` directly bypasses |
| 13 | + * `fromInvoke`'s `parseCoreError` and produces a different shape than |
| 14 | + * `useBackupDatabase`'s `.match(...)` branch consumes. |
| 15 | + */ |
| 16 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 17 | +import { fireEvent, screen, waitFor } from "@testing-library/react"; |
| 18 | +import { errAsync, okAsync } from "neverthrow"; |
| 19 | +import StatusBar from "../components/StatusBar"; |
| 20 | +import * as api from "../api"; |
| 21 | +import { useUiStore } from "../stores/ui"; |
| 22 | +import { renderWithProviders, resetUiStore } from "./test-utils"; |
| 23 | +import type { CoreError } from "../bindings"; |
| 24 | + |
| 25 | +vi.mock("../api", async () => { |
| 26 | + const actual = await vi.importActual<typeof import("../api")>("../api"); |
| 27 | + return { |
| 28 | + ...actual, |
| 29 | + backupDatabase: vi.fn(), |
| 30 | + // WHY listQuickHashCollisions: StatusBar renders <CollisionPill> which |
| 31 | + // calls useCollisions() → listQuickHashCollisions. Without a mock the |
| 32 | + // real `invoke` fires, which fails in jsdom. Return an empty list so the |
| 33 | + // pill renders in its neutral "0 groups" state without affecting the |
| 34 | + // backup-button assertions. |
| 35 | + listQuickHashCollisions: vi.fn(), |
| 36 | + }; |
| 37 | +}); |
| 38 | + |
| 39 | +const mockBackupDatabase = vi.mocked(api.backupDatabase); |
| 40 | +const mockListCollisions = vi.mocked(api.listQuickHashCollisions); |
| 41 | + |
| 42 | +beforeEach(() => { |
| 43 | + vi.clearAllMocks(); |
| 44 | + resetUiStore(); |
| 45 | + mockListCollisions.mockReturnValue(okAsync([])); |
| 46 | +}); |
| 47 | + |
| 48 | +describe("StatusBar Backup button", () => { |
| 49 | + it("notifies success with path + MB on Ok", async () => { |
| 50 | + mockBackupDatabase.mockReturnValueOnce( |
| 51 | + okAsync({ |
| 52 | + absolute_path: "/tmp/backup.sqlite", |
| 53 | + size_bytes: 1024 * 1024 * 5, // 5 MB |
| 54 | + }), |
| 55 | + ); |
| 56 | + |
| 57 | + renderWithProviders(<StatusBar />); |
| 58 | + |
| 59 | + fireEvent.click(screen.getByRole("button", { name: /backup/i })); |
| 60 | + |
| 61 | + await waitFor(() => { |
| 62 | + const notifications = useUiStore.getState().notifications; |
| 63 | + expect( |
| 64 | + notifications.some( |
| 65 | + (n) => |
| 66 | + n.kind === "info" && |
| 67 | + n.message.includes("/tmp/backup.sqlite") && |
| 68 | + n.message.includes("5.0 MB"), |
| 69 | + ), |
| 70 | + ).toBe(true); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it("notifies typed error on TargetExists", async () => { |
| 75 | + const err: CoreError = { |
| 76 | + kind: "BackupFailed", |
| 77 | + data: { |
| 78 | + reason: { |
| 79 | + kind: "TargetExists", |
| 80 | + data: { path: "/tmp/backup.sqlite" }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }; |
| 84 | + mockBackupDatabase.mockReturnValueOnce(errAsync(err)); |
| 85 | + |
| 86 | + renderWithProviders(<StatusBar />); |
| 87 | + |
| 88 | + fireEvent.click(screen.getByRole("button", { name: /backup/i })); |
| 89 | + |
| 90 | + await waitFor(() => { |
| 91 | + const notifications = useUiStore.getState().notifications; |
| 92 | + expect( |
| 93 | + notifications.some( |
| 94 | + (n) => n.kind === "error" && n.message.includes("already exists"), |
| 95 | + ), |
| 96 | + ).toBe(true); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments