-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathusePredictMarketData.test.tsx
More file actions
403 lines (347 loc) · 11.2 KB
/
usePredictMarketData.test.tsx
File metadata and controls
403 lines (347 loc) · 11.2 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import {
renderHook,
act,
waitFor,
cleanup,
} from '@testing-library/react-native';
import DevLogger from '../../../../core/SDKConnect/utils/DevLogger';
import Engine from '../../../../core/Engine';
import { usePredictMarketData } from './usePredictMarketData';
import { PredictMarket, Recurrence } from '../types';
import { POLYMARKET_PROVIDER_ID } from '../providers/polymarket/constants';
// Mock dependencies
jest.mock('../../../../core/SDKConnect/utils/DevLogger');
jest.mock('../../../../core/Engine', () => ({
context: {
PredictController: {
getMarkets: jest.fn(),
},
},
}));
describe('usePredictMarketData', () => {
const mockGetMarkets = jest.fn();
const mockMarketData: PredictMarket[] = [
{
id: 'market-1',
providerId: POLYMARKET_PROVIDER_ID,
slug: 'bitcoin-price-prediction',
title: 'Will Bitcoin reach $100k by end of 2024?',
description: 'Bitcoin price prediction market',
image: 'https://example.com/btc.png',
status: 'open',
recurrence: Recurrence.NONE,
category: 'crypto',
tags: ['trending'],
outcomes: [
{
id: 'outcome-1',
providerId: POLYMARKET_PROVIDER_ID,
marketId: 'market-1',
title: 'Yes',
description: 'Bitcoin will reach $100k',
image: '',
status: 'open',
tokens: [
{
id: 'token-1',
title: 'Yes',
price: 0.65,
},
],
volume: 1000000,
groupItemTitle: 'Yes/No',
},
{
id: 'outcome-2',
providerId: POLYMARKET_PROVIDER_ID,
marketId: 'market-1',
title: 'No',
description: 'Bitcoin will not reach $100k',
image: '',
status: 'open',
tokens: [
{
id: 'token-2',
title: 'No',
price: 0.35,
},
],
volume: 1000000,
groupItemTitle: 'Yes/No',
},
],
liquidity: 1000000,
volume: 1000000,
},
{
id: 'market-2',
providerId: POLYMARKET_PROVIDER_ID,
slug: 'ethereum-price-prediction',
title: 'Will Ethereum reach $100000 by end of 2025?',
description: 'Ethereum price prediction market',
image: 'https://example.com/eth.png',
status: 'open',
recurrence: Recurrence.NONE,
category: 'crypto',
tags: ['trending'],
outcomes: [
{
id: 'outcome-3',
providerId: POLYMARKET_PROVIDER_ID,
marketId: 'market-2',
title: 'Yes',
description: 'Ethereum will reach $100k',
image: '',
status: 'open',
tokens: [
{
id: 'token-3',
title: 'Yes',
price: 0.45,
},
],
volume: 500000,
groupItemTitle: 'Yes/No',
},
{
id: 'outcome-4',
providerId: POLYMARKET_PROVIDER_ID,
marketId: 'market-2',
title: 'No',
description: 'Ethereum will not reach $100k',
image: '',
status: 'open',
tokens: [
{
id: 'token-4',
title: 'No',
price: 0.55,
},
],
volume: 500000,
groupItemTitle: 'Yes/No',
},
],
liquidity: 1000000,
volume: 1000000,
},
];
beforeEach(() => {
jest.clearAllMocks();
// Setup Engine mocks
(Engine.context.PredictController.getMarkets as jest.Mock) = mockGetMarkets;
// Setup DevLogger mock
(DevLogger.log as jest.Mock).mockImplementation(() => {
// Mock implementation
});
});
afterEach(async () => {
// Force-unmount any hooks left over from the test so their pending
// promises / state-setters don't leak into the next test (e.g. the
// "marks fetching when enabled becomes true" case below uses a
// never-resolving promise; without cleanup the hook stays mounted
// and the worker can be force-killed by jest's watchdog under load,
// showing up as an unrelated `waitFor` timeout in a sibling test).
await act(async () => {
cleanup();
});
jest.clearAllMocks();
});
it('does not fetch when enabled is false', () => {
renderHook(() => usePredictMarketData({ enabled: false }));
expect(mockGetMarkets).not.toHaveBeenCalled();
});
it('marks fetching when enabled becomes true before async fetch settles (no empty flash)', () => {
mockGetMarkets.mockImplementation(
() =>
new Promise<PredictMarket[]>((_resolve) => {
/* unresolved until test ends */
}),
);
const { result, rerender, unmount } = renderHook(
({ enabled }: { enabled: boolean }) => usePredictMarketData({ enabled }),
{ initialProps: { enabled: false } },
);
expect(result.current.isFetching).toBe(false);
rerender({ enabled: true });
expect(result.current.isFetching).toBe(true);
expect(result.current.marketData).toEqual([]);
// Explicitly release the hook so the never-resolving promise above
// doesn't keep an in-flight fetch alive across tests.
unmount();
});
it('should fetch market data successfully', async () => {
mockGetMarkets.mockResolvedValue(mockMarketData);
const { result } = renderHook(() => usePredictMarketData());
// Initially loading
expect(result.current.isFetching).toBe(true);
expect(result.current.marketData).toEqual([]);
expect(result.current.error).toBe(null);
// Wait for data to load
await waitFor(() => {
expect(result.current.isFetching).toBe(false);
});
expect(result.current.marketData).toEqual(mockMarketData);
expect(result.current.error).toBe(null);
expect(mockGetMarkets).toHaveBeenCalledTimes(1);
expect(DevLogger.log).toHaveBeenCalledWith(
'Fetching market data for category:',
'trending',
'search:',
undefined,
'offset:',
0,
'limit:',
20,
);
expect(DevLogger.log).toHaveBeenCalledWith(
'Market data received:',
mockMarketData,
);
});
it('filters child more-market cards without disabling pagination', async () => {
const rawMarkets = Array.from({ length: 20 }, (_, index) => ({
...mockMarketData[0],
id: `market-${index}`,
slug: `market-${index}`,
parentMarketId: index >= 18 ? 'parent-market' : undefined,
}));
mockGetMarkets.mockResolvedValue(rawMarkets);
const { result } = renderHook(() => usePredictMarketData({ pageSize: 20 }));
await waitFor(() => {
expect(result.current.isFetching).toBe(false);
});
expect(result.current.marketData).toHaveLength(18);
expect(result.current.marketData.map((market) => market.id)).toEqual(
rawMarkets.slice(0, 18).map((market) => market.id),
);
expect(result.current.hasMore).toBe(true);
});
it('uses raw page offsets when loading more after child cards are filtered', async () => {
const firstRawPage = Array.from({ length: 20 }, (_, index) => ({
...mockMarketData[0],
id: `first-page-market-${index}`,
slug: `first-page-market-${index}`,
parentMarketId: index >= 18 ? 'parent-market' : undefined,
}));
const secondRawPage = Array.from({ length: 5 }, (_, index) => ({
...mockMarketData[0],
id: `second-page-market-${index}`,
slug: `second-page-market-${index}`,
}));
mockGetMarkets
.mockResolvedValueOnce(firstRawPage)
.mockResolvedValueOnce(secondRawPage);
const { result } = renderHook(() => usePredictMarketData({ pageSize: 20 }));
await waitFor(() => {
expect(result.current.isFetching).toBe(false);
});
await act(async () => {
await result.current.fetchMore();
});
expect(mockGetMarkets).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ limit: 20, offset: 20 }),
);
expect(result.current.marketData).toHaveLength(23);
expect(result.current.hasMore).toBe(false);
});
it('handle null market data', async () => {
mockGetMarkets.mockResolvedValue(null);
const { result } = renderHook(() => usePredictMarketData());
await waitFor(() => {
expect(result.current.isFetching).toBe(false);
});
expect(result.current.marketData).toEqual([]);
expect(result.current.error).toBe(null);
});
it('handle empty market data array', async () => {
mockGetMarkets.mockResolvedValue([]);
const { result } = renderHook(() => usePredictMarketData());
await waitFor(() => {
expect(result.current.isFetching).toBe(false);
});
expect(result.current.marketData).toEqual([]);
expect(result.current.error).toBe(null);
});
it('refetch data when calling refetch', async () => {
mockGetMarkets.mockResolvedValue(mockMarketData);
const { result } = renderHook(() => usePredictMarketData());
await waitFor(() => {
expect(mockGetMarkets).toHaveBeenCalledTimes(1);
});
// Call refetch
await act(async () => {
await result.current.refetch();
});
expect(mockGetMarkets).toHaveBeenCalledTimes(2);
});
it('maintain stable refetch function reference', () => {
mockGetMarkets.mockResolvedValue(mockMarketData);
const { result, rerender } = renderHook(() => usePredictMarketData());
const firstRefetch = result.current.refetch;
// Trigger a re-render
rerender(undefined);
expect(result.current.refetch).toBe(firstRefetch);
});
describe('customQueryParams option', () => {
it('passes customQueryParams to getMarkets', async () => {
mockGetMarkets.mockResolvedValue(mockMarketData);
renderHook(() =>
usePredictMarketData({
category: 'hot',
customQueryParams: 'tag_id=149&order=volume24hr',
}),
);
await waitFor(() => {
expect(mockGetMarkets).toHaveBeenCalledWith(
expect.objectContaining({
category: 'hot',
customQueryParams: 'tag_id=149&order=volume24hr',
}),
);
});
});
it('refetches when customQueryParams changes', async () => {
mockGetMarkets.mockResolvedValue(mockMarketData);
const { rerender } = renderHook(
({ customQueryParams }) =>
usePredictMarketData({
category: 'hot',
customQueryParams,
}),
{
initialProps: { customQueryParams: 'tag_id=149' },
},
);
await waitFor(() => {
expect(mockGetMarkets).toHaveBeenCalledTimes(1);
});
rerender({ customQueryParams: 'tag_id=200' });
await waitFor(() => {
expect(mockGetMarkets).toHaveBeenCalledTimes(2);
});
expect(mockGetMarkets).toHaveBeenLastCalledWith(
expect.objectContaining({
customQueryParams: 'tag_id=200',
}),
);
});
it('does not pass customQueryParams when undefined', async () => {
mockGetMarkets.mockResolvedValue(mockMarketData);
renderHook(() =>
usePredictMarketData({
category: 'trending',
}),
);
await waitFor(() => {
expect(mockGetMarkets).toHaveBeenCalledWith(
expect.objectContaining({
category: 'trending',
customQueryParams: undefined,
}),
);
});
});
});
});