|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { act } from 'react' |
| 6 | +import { QueryClient, QueryClientProvider, useQuery } from '@tanstack/react-query' |
| 7 | +import { createRoot, type Root } from 'react-dom/client' |
| 8 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | + |
| 10 | +const { mockUseSocket } = vi.hoisted(() => ({ mockUseSocket: vi.fn() })) |
| 11 | + |
| 12 | +vi.mock('@/app/workspace/providers/socket-provider', () => ({ |
| 13 | + useSocket: () => mockUseSocket(), |
| 14 | +})) |
| 15 | + |
| 16 | +import { INVALIDATION_COALESCE_MS } from '@/app/workspace/[workspaceId]/hooks/use-workspace-invalidation-room' |
| 17 | +import { useWorkspaceTablesRoom } from '@/app/workspace/[workspaceId]/tables/hooks/use-workspace-tables-room' |
| 18 | +import { tableKeys } from '@/hooks/queries/utils/table-keys' |
| 19 | + |
| 20 | +function fakeSocket() { |
| 21 | + return { connected: true, emit: vi.fn(), on: vi.fn(), off: vi.fn() } |
| 22 | +} |
| 23 | + |
| 24 | +let socket: ReturnType<typeof fakeSocket> |
| 25 | +let root: Root | null = null |
| 26 | + |
| 27 | +function broadcast(workspaceId: string) { |
| 28 | + for (const [name, handler] of socket.on.mock.calls) { |
| 29 | + if (name === 'workspace-tables-changed') handler({ workspaceId }) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +describe('useWorkspaceTablesRoom', () => { |
| 34 | + beforeEach(() => { |
| 35 | + vi.useFakeTimers() |
| 36 | + socket = fakeSocket() |
| 37 | + mockUseSocket.mockImplementation(() => ({ socket })) |
| 38 | + }) |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + if (root) act(() => root?.unmount()) |
| 42 | + root = null |
| 43 | + vi.useRealTimers() |
| 44 | + }) |
| 45 | + |
| 46 | + /* |
| 47 | + * The callback used to fire both invalidations and return nothing, so the room's delivery never |
| 48 | + * waited: a broadcast arriving while the list refetch was still loading invalidated it again, |
| 49 | + * which cancels and restarts the in-flight fetch. Under a burst of table writes a slow list never |
| 50 | + * finished loading. |
| 51 | + */ |
| 52 | + it('does not restart a list refetch still in flight when the next broadcast arrives', async () => { |
| 53 | + const pending: Array<() => void> = [] |
| 54 | + const fetchList = vi.fn( |
| 55 | + () => |
| 56 | + new Promise<string[]>((resolve) => { |
| 57 | + pending.push(() => resolve([])) |
| 58 | + }) |
| 59 | + ) |
| 60 | + const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }) |
| 61 | + queryClient.setQueryData([...tableKeys.lists(), 'ws-1'], []) |
| 62 | + |
| 63 | + function Probe() { |
| 64 | + useQuery({ |
| 65 | + queryKey: [...tableKeys.lists(), 'ws-1'], |
| 66 | + queryFn: fetchList, |
| 67 | + staleTime: Number.POSITIVE_INFINITY, |
| 68 | + }) |
| 69 | + useWorkspaceTablesRoom('ws-1') |
| 70 | + return null |
| 71 | + } |
| 72 | + const container = document.createElement('div') |
| 73 | + document.body.appendChild(container) |
| 74 | + root = createRoot(container) |
| 75 | + act(() => |
| 76 | + root?.render( |
| 77 | + <QueryClientProvider client={queryClient}> |
| 78 | + <Probe /> |
| 79 | + </QueryClientProvider> |
| 80 | + ) |
| 81 | + ) |
| 82 | + |
| 83 | + broadcast('ws-1') |
| 84 | + await vi.advanceTimersByTimeAsync(0) |
| 85 | + expect(fetchList).toHaveBeenCalledTimes(1) |
| 86 | + |
| 87 | + broadcast('ws-1') |
| 88 | + await vi.advanceTimersByTimeAsync(5 * INVALIDATION_COALESCE_MS) |
| 89 | + expect(fetchList).toHaveBeenCalledTimes(1) |
| 90 | + |
| 91 | + for (const resolve of pending.splice(0)) resolve() |
| 92 | + await vi.advanceTimersByTimeAsync(INVALIDATION_COALESCE_MS) |
| 93 | + expect(fetchList).toHaveBeenCalledTimes(2) |
| 94 | + }) |
| 95 | +}) |
0 commit comments