-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseEventPolling.test.tsx
More file actions
89 lines (79 loc) · 2.7 KB
/
Copy pathuseEventPolling.test.tsx
File metadata and controls
89 lines (79 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { httpLink } from '@trpc/client';
import { act, renderHook, waitFor } from '@testing-library/react';
import type { ReactNode } from 'react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { eventsTailStore } from '@/lib/telemetry/eventsTail';
import { trpc } from '@/lib/trpc';
import { useEventPolling } from './useEventPolling';
type Fetch = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
function buildWrapper(fetchImpl: Fetch) {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false, staleTime: 0 } },
});
const client = trpc.createClient({
links: [
httpLink({
url: 'http://localhost/api/trpc',
fetch: fetchImpl,
}),
],
});
return function Wrapper({ children }: { children: ReactNode }) {
return (
<trpc.Provider client={client} queryClient={queryClient}>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</trpc.Provider>
);
};
}
function tRpcJsonResponse(payload: unknown): Response {
const body = JSON.stringify({ result: { data: payload } });
return new Response(body, {
status: 200,
headers: { 'content-type': 'application/json' },
});
}
beforeEach(() => {
eventsTailStore.reset();
});
afterEach(() => {
vi.useRealTimers();
});
describe('useEventPolling', () => {
it('pushes events returned by getActivityEvents into the tail store', async () => {
const fetchImpl = vi.fn<Fetch>().mockResolvedValue(
tRpcJsonResponse({
events: [
{
seq: 7,
kind: 'partner_webhook_received',
at: '2026-04-22T00:00:00Z',
actor: { kind: 'partner', slug: 'gokart' },
offer_id: 'o-1',
event_name: 'attribution_verified',
idempotency_key: 'k-1',
},
],
latest_seq: 7,
}),
);
const Wrapper = buildWrapper(fetchImpl as Fetch);
renderHook(() => useEventPolling({ intervalMs: 10_000 }), { wrapper: Wrapper });
await waitFor(() => {
expect(eventsTailStore.getSnapshot()).toHaveLength(1);
});
const tail = eventsTailStore.getSnapshot();
expect(tail[0].seq).toBe(7);
expect(tail[0].kind).toBe('partner_webhook_received');
});
it('does nothing when disabled', async () => {
const fetchImpl = vi.fn<Fetch>();
const Wrapper = buildWrapper(fetchImpl as Fetch);
renderHook(() => useEventPolling({ enabled: false }), { wrapper: Wrapper });
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 20));
});
expect(fetchImpl).not.toHaveBeenCalled();
});
});