|
| 1 | +import type { Schemas } from "@posthog/api-client"; |
| 2 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 3 | +import { act, renderHook, waitFor } from "@testing-library/react"; |
| 4 | +import type { ReactNode } from "react"; |
| 5 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +const mockClient = vi.hoisted(() => ({ |
| 8 | + getDesktopFileSystemChannels: vi.fn(), |
| 9 | + createDesktopFileSystemChannel: vi.fn(), |
| 10 | +})); |
| 11 | +vi.mock("@posthog/ui/features/auth/authClient", () => ({ |
| 12 | + useOptionalAuthenticatedClient: () => mockClient, |
| 13 | +})); |
| 14 | + |
| 15 | +import { useChannelMutations, useChannels } from "./useChannels"; |
| 16 | + |
| 17 | +function folder(id: string, path: string): Schemas.FileSystem { |
| 18 | + return { |
| 19 | + id, |
| 20 | + path, |
| 21 | + type: "folder", |
| 22 | + depth: 1, |
| 23 | + created_at: "2026-01-01T00:00:00Z", |
| 24 | + last_viewed_at: null, |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +let queryClient: QueryClient; |
| 29 | +function wrapper({ children }: { children: ReactNode }) { |
| 30 | + return ( |
| 31 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +describe("useChannelMutations", () => { |
| 36 | + beforeEach(() => { |
| 37 | + vi.clearAllMocks(); |
| 38 | + queryClient = new QueryClient({ |
| 39 | + defaultOptions: { queries: { retry: false } }, |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + it("shows the created channel immediately, before the refetch resolves", async () => { |
| 44 | + // Seed the list with one existing channel. |
| 45 | + mockClient.getDesktopFileSystemChannels.mockResolvedValue([ |
| 46 | + folder("1", "alpha"), |
| 47 | + ]); |
| 48 | + |
| 49 | + const list = renderHook(() => useChannels(), { wrapper }); |
| 50 | + await waitFor(() => expect(list.result.current.isLoading).toBe(false)); |
| 51 | + expect(list.result.current.channels.map((c) => c.name)).toEqual(["alpha"]); |
| 52 | + |
| 53 | + // Make the create return the new channel, but hang any subsequent refetch |
| 54 | + // so we can prove the list updates without waiting on it. |
| 55 | + const created = folder("2", "beta"); |
| 56 | + mockClient.createDesktopFileSystemChannel.mockResolvedValue(created); |
| 57 | + mockClient.getDesktopFileSystemChannels.mockReturnValue( |
| 58 | + new Promise(() => {}), |
| 59 | + ); |
| 60 | + |
| 61 | + const mutations = renderHook(() => useChannelMutations(), { wrapper }); |
| 62 | + await act(async () => { |
| 63 | + await mutations.result.current.createChannel("beta"); |
| 64 | + }); |
| 65 | + |
| 66 | + // The new channel is present from the optimistic cache write, sorted |
| 67 | + // alphabetically alongside the existing one — without the hung refetch |
| 68 | + // having resolved. |
| 69 | + await waitFor(() => |
| 70 | + expect(list.result.current.channels.map((c) => c.name)).toEqual([ |
| 71 | + "alpha", |
| 72 | + "beta", |
| 73 | + ]), |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it("does not duplicate a channel the poll already landed", async () => { |
| 78 | + // The poll has already surfaced the channel we're about to create. |
| 79 | + const existing = folder("1", "alpha"); |
| 80 | + mockClient.getDesktopFileSystemChannels.mockResolvedValue([existing]); |
| 81 | + |
| 82 | + const list = renderHook(() => useChannels(), { wrapper }); |
| 83 | + await waitFor(() => expect(list.result.current.isLoading).toBe(false)); |
| 84 | + expect(list.result.current.channels.map((c) => c.id)).toEqual(["1"]); |
| 85 | + |
| 86 | + // Create returns the same id; hang the refetch so only the optimistic |
| 87 | + // cache write is exercised. |
| 88 | + mockClient.createDesktopFileSystemChannel.mockResolvedValue(existing); |
| 89 | + mockClient.getDesktopFileSystemChannels.mockReturnValue( |
| 90 | + new Promise(() => {}), |
| 91 | + ); |
| 92 | + |
| 93 | + const mutations = renderHook(() => useChannelMutations(), { wrapper }); |
| 94 | + await act(async () => { |
| 95 | + await mutations.result.current.createChannel("alpha"); |
| 96 | + }); |
| 97 | + |
| 98 | + // The duplicate-id guard keeps the list at one entry. |
| 99 | + expect(list.result.current.channels.map((c) => c.id)).toEqual(["1"]); |
| 100 | + }); |
| 101 | +}); |
0 commit comments