|
| 1 | +import type { Task } from "@posthog/shared/domain-types"; |
| 2 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 3 | +import { renderHook, waitFor } from "@testing-library/react"; |
| 4 | +import type { PropsWithChildren } from "react"; |
| 5 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import { useRefreshedTask } from "./useRefreshedTask"; |
| 7 | + |
| 8 | +const mocks = vi.hoisted(() => ({ getTask: vi.fn() })); |
| 9 | + |
| 10 | +vi.mock("@posthog/ui/features/auth/authClientImperative", () => ({ |
| 11 | + getAuthenticatedClient: vi.fn(async () => ({ getTask: mocks.getTask })), |
| 12 | +})); |
| 13 | + |
| 14 | +function task(runId: string, status: "failed" | "in_progress"): Task { |
| 15 | + return { |
| 16 | + id: "task-123", |
| 17 | + title: "Cloud task", |
| 18 | + description: "Keep working", |
| 19 | + repository: null, |
| 20 | + latest_run: { |
| 21 | + id: runId, |
| 22 | + task: "task-123", |
| 23 | + environment: "cloud", |
| 24 | + status, |
| 25 | + state: {}, |
| 26 | + }, |
| 27 | + } as Task; |
| 28 | +} |
| 29 | + |
| 30 | +describe("useRefreshedTask", () => { |
| 31 | + beforeEach(() => { |
| 32 | + mocks.getTask.mockReset(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("replaces a cached failed run with the authoritative resumed run", async () => { |
| 36 | + const failedParent = task("run-parent", "failed"); |
| 37 | + const resumedChild = task("run-child", "in_progress"); |
| 38 | + mocks.getTask.mockResolvedValue(resumedChild); |
| 39 | + const queryClient = new QueryClient({ |
| 40 | + defaultOptions: { queries: { retry: false } }, |
| 41 | + }); |
| 42 | + const wrapper = ({ children }: PropsWithChildren) => ( |
| 43 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 44 | + ); |
| 45 | + |
| 46 | + const { result } = renderHook( |
| 47 | + () => useRefreshedTask("task-123", failedParent), |
| 48 | + { wrapper }, |
| 49 | + ); |
| 50 | + |
| 51 | + expect(result.current.latest_run?.id).toBe("run-parent"); |
| 52 | + await waitFor(() => { |
| 53 | + expect(result.current.latest_run?.id).toBe("run-child"); |
| 54 | + }); |
| 55 | + expect(mocks.getTask).toHaveBeenCalledWith("task-123"); |
| 56 | + }); |
| 57 | +}); |
0 commit comments