exportLogsAction.test.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import { runExportLogsAction } from "@/lib/exportLogsAction.js";
  3. const toastMock = vi.hoisted(() => vi.fn(() => 7));
  4. const dismissToastMock = vi.hoisted(() => vi.fn());
  5. vi.mock("@/components/ui/toast.js", () => ({
  6. toast: toastMock,
  7. dismissToast: dismissToastMock,
  8. }));
  9. const intl = {
  10. formatMessage: ({ id }: { id: string }, values?: Record<string, string>) =>
  11. values?.error ? `${id}:${values.error}` : id,
  12. };
  13. describe("runExportLogsAction", () => {
  14. beforeEach(() => {
  15. toastMock.mockClear();
  16. dismissToastMock.mockClear();
  17. });
  18. it("keeps the pending toast open until export finishes", async () => {
  19. const exportLogs = vi.fn(async () => ({ success: true }));
  20. await runExportLogsAction({ exportLogs }, intl);
  21. expect(exportLogs).toHaveBeenCalledOnce();
  22. expect(toastMock).toHaveBeenCalledWith("sidebar.exportLogs.pending", {
  23. durationMs: Number.POSITIVE_INFINITY,
  24. });
  25. expect(dismissToastMock).toHaveBeenCalledWith(7);
  26. });
  27. it("shows the platform error when export fails", async () => {
  28. const exportLogs = vi.fn(async () => ({
  29. success: false,
  30. error: "not supported",
  31. }));
  32. await runExportLogsAction({ exportLogs }, intl);
  33. expect(exportLogs).toHaveBeenCalledOnce();
  34. expect(toastMock).toHaveBeenCalledWith("sidebar.exportLogs.error:not supported");
  35. expect(dismissToastMock).toHaveBeenCalledWith(7);
  36. });
  37. });