@ic-reactor/react 3.0.0-beta.7 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -10
- package/dist/createActorHooks.d.ts +2 -0
- package/dist/createActorHooks.d.ts.map +1 -1
- package/dist/createActorHooks.js +2 -0
- package/dist/createActorHooks.js.map +1 -1
- package/dist/createMutation.d.ts.map +1 -1
- package/dist/createMutation.js +4 -0
- package/dist/createMutation.js.map +1 -1
- package/dist/hooks/index.d.ts +18 -5
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +15 -5
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/useActorInfiniteQuery.d.ts +13 -11
- package/dist/hooks/useActorInfiniteQuery.d.ts.map +1 -1
- package/dist/hooks/useActorInfiniteQuery.js.map +1 -1
- package/dist/hooks/useActorMethod.d.ts +105 -0
- package/dist/hooks/useActorMethod.d.ts.map +1 -0
- package/dist/hooks/useActorMethod.js +192 -0
- package/dist/hooks/useActorMethod.js.map +1 -0
- package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts +13 -10
- package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts.map +1 -1
- package/dist/hooks/useActorSuspenseInfiniteQuery.js.map +1 -1
- package/package.json +9 -8
- package/src/createActorHooks.ts +146 -0
- package/src/createAuthHooks.ts +137 -0
- package/src/createInfiniteQuery.ts +471 -0
- package/src/createMutation.ts +163 -0
- package/src/createQuery.ts +197 -0
- package/src/createSuspenseInfiniteQuery.ts +478 -0
- package/src/createSuspenseQuery.ts +215 -0
- package/src/hooks/index.ts +93 -0
- package/src/hooks/useActorInfiniteQuery.test.tsx +457 -0
- package/src/hooks/useActorInfiniteQuery.ts +134 -0
- package/src/hooks/useActorMethod.test.tsx +798 -0
- package/src/hooks/useActorMethod.ts +397 -0
- package/src/hooks/useActorMutation.test.tsx +220 -0
- package/src/hooks/useActorMutation.ts +124 -0
- package/src/hooks/useActorQuery.test.tsx +287 -0
- package/src/hooks/useActorQuery.ts +110 -0
- package/src/hooks/useActorSuspenseInfiniteQuery.test.tsx +472 -0
- package/src/hooks/useActorSuspenseInfiniteQuery.ts +137 -0
- package/src/hooks/useActorSuspenseQuery.test.tsx +254 -0
- package/src/hooks/useActorSuspenseQuery.ts +112 -0
- package/src/index.ts +21 -0
- package/src/types.ts +435 -0
- package/src/validation.ts +202 -0
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest"
|
|
2
|
+
import { renderHook, waitFor, act } from "@testing-library/react"
|
|
3
|
+
import React from "react"
|
|
4
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
|
|
5
|
+
import { useActorInfiniteQuery } from "./useActorInfiniteQuery"
|
|
6
|
+
import { ActorMethod } from "@icp-sdk/core/agent"
|
|
7
|
+
import { Reactor } from "@ic-reactor/core"
|
|
8
|
+
|
|
9
|
+
// Define a test actor type with paginated methods
|
|
10
|
+
type TestActor = {
|
|
11
|
+
getPosts: ActorMethod<[{ page: number; limit: number }], string[]>
|
|
12
|
+
getItems: ActorMethod<
|
|
13
|
+
[number],
|
|
14
|
+
{ items: string[]; nextCursor: number | null }
|
|
15
|
+
>
|
|
16
|
+
getMessages: ActorMethod<
|
|
17
|
+
[string],
|
|
18
|
+
{ messages: string[]; nextToken: string | null }
|
|
19
|
+
>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Mock data generators
|
|
23
|
+
const generatePosts = (page: number, limit: number): string[] => {
|
|
24
|
+
const start = (page - 1) * limit
|
|
25
|
+
return Array.from({ length: limit }, (_, i) => `Post ${start + i + 1}`)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const generateItems = (
|
|
29
|
+
cursor: number
|
|
30
|
+
): { items: string[]; nextCursor: number | null } => {
|
|
31
|
+
if (cursor >= 50) {
|
|
32
|
+
return { items: [], nextCursor: null }
|
|
33
|
+
}
|
|
34
|
+
const items = Array.from({ length: 10 }, (_, i) => `Item ${cursor + i + 1}`)
|
|
35
|
+
return { items, nextCursor: cursor + 10 }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Mock Reactor
|
|
39
|
+
const createMockReactor = (queryClient: QueryClient) => {
|
|
40
|
+
const callMethod = vi
|
|
41
|
+
.fn()
|
|
42
|
+
.mockImplementation(async ({ functionName, args }) => {
|
|
43
|
+
if (functionName === "getPosts") {
|
|
44
|
+
const { page, limit } = args[0]
|
|
45
|
+
return generatePosts(page, limit)
|
|
46
|
+
}
|
|
47
|
+
if (functionName === "getItems") {
|
|
48
|
+
const cursor = args[0]
|
|
49
|
+
return generateItems(cursor)
|
|
50
|
+
}
|
|
51
|
+
if (functionName === "getMessages") {
|
|
52
|
+
const token = args[0]
|
|
53
|
+
const pageNum = token === "start" ? 0 : parseInt(token, 10)
|
|
54
|
+
const messages = Array.from(
|
|
55
|
+
{ length: 5 },
|
|
56
|
+
(_, i) => `Message ${pageNum * 5 + i + 1}`
|
|
57
|
+
)
|
|
58
|
+
const nextToken = pageNum < 3 ? String(pageNum + 1) : null
|
|
59
|
+
return { messages, nextToken }
|
|
60
|
+
}
|
|
61
|
+
return null
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
queryClient,
|
|
66
|
+
callMethod,
|
|
67
|
+
generateQueryKey: vi
|
|
68
|
+
.fn()
|
|
69
|
+
.mockImplementation(({ functionName, args }) => [
|
|
70
|
+
"test-canister",
|
|
71
|
+
functionName,
|
|
72
|
+
...(args ? [JSON.stringify(args)] : []),
|
|
73
|
+
]),
|
|
74
|
+
} as unknown as Reactor<TestActor>
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
describe("useActorInfiniteQuery", () => {
|
|
78
|
+
let queryClient: QueryClient
|
|
79
|
+
let mockReactor: ReturnType<typeof createMockReactor>
|
|
80
|
+
|
|
81
|
+
beforeEach(() => {
|
|
82
|
+
queryClient = new QueryClient({
|
|
83
|
+
defaultOptions: {
|
|
84
|
+
queries: {
|
|
85
|
+
retry: false,
|
|
86
|
+
gcTime: 0,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
})
|
|
90
|
+
mockReactor = createMockReactor(queryClient)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
94
|
+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
describe("basic functionality", () => {
|
|
98
|
+
it("should fetch initial page", async () => {
|
|
99
|
+
const { result } = renderHook(
|
|
100
|
+
() =>
|
|
101
|
+
useActorInfiniteQuery({
|
|
102
|
+
reactor: mockReactor,
|
|
103
|
+
functionName: "getPosts",
|
|
104
|
+
initialPageParam: 1,
|
|
105
|
+
getArgs: (page) => [{ page, limit: 10 }] as const,
|
|
106
|
+
getNextPageParam: (lastPage, allPages) => {
|
|
107
|
+
if (lastPage.length < 10) return undefined
|
|
108
|
+
return allPages.length + 1
|
|
109
|
+
},
|
|
110
|
+
}),
|
|
111
|
+
{ wrapper }
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
await waitFor(() => {
|
|
115
|
+
expect(result.current.isSuccess).toBe(true)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
expect(result.current.data?.pages).toHaveLength(1)
|
|
119
|
+
expect(result.current.data?.pages[0]).toHaveLength(10)
|
|
120
|
+
expect(result.current.data?.pages[0][0]).toBe("Post 1")
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it("should support fetchNextPage", async () => {
|
|
124
|
+
const { result } = renderHook(
|
|
125
|
+
() =>
|
|
126
|
+
useActorInfiniteQuery({
|
|
127
|
+
reactor: mockReactor,
|
|
128
|
+
functionName: "getPosts",
|
|
129
|
+
initialPageParam: 1,
|
|
130
|
+
getArgs: (page) => [{ page, limit: 10 }] as const,
|
|
131
|
+
getNextPageParam: (lastPage, allPages) => {
|
|
132
|
+
if (lastPage.length < 10) return undefined
|
|
133
|
+
return allPages.length + 1
|
|
134
|
+
},
|
|
135
|
+
}),
|
|
136
|
+
{ wrapper }
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
await waitFor(() => {
|
|
140
|
+
expect(result.current.isSuccess).toBe(true)
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
expect(result.current.hasNextPage).toBe(true)
|
|
144
|
+
|
|
145
|
+
// fetchNextPage should be available and callable
|
|
146
|
+
expect(typeof result.current.fetchNextPage).toBe("function")
|
|
147
|
+
expect(result.current.isFetchingNextPage).toBe(false)
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it("should call reactor.callMethod with correct args", async () => {
|
|
151
|
+
const { result } = renderHook(
|
|
152
|
+
() =>
|
|
153
|
+
useActorInfiniteQuery({
|
|
154
|
+
reactor: mockReactor,
|
|
155
|
+
functionName: "getPosts",
|
|
156
|
+
initialPageParam: 1,
|
|
157
|
+
getArgs: (page) => [{ page, limit: 5 }] as const,
|
|
158
|
+
getNextPageParam: () => undefined,
|
|
159
|
+
}),
|
|
160
|
+
{ wrapper }
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
await waitFor(() => {
|
|
164
|
+
expect(result.current.isSuccess).toBe(true)
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
expect(mockReactor.callMethod).toHaveBeenCalledWith({
|
|
168
|
+
functionName: "getPosts",
|
|
169
|
+
args: [{ page: 1, limit: 5 }],
|
|
170
|
+
callConfig: undefined,
|
|
171
|
+
})
|
|
172
|
+
})
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
describe("cursor-based pagination", () => {
|
|
176
|
+
it("should work with cursor-based pagination", async () => {
|
|
177
|
+
const { result } = renderHook(
|
|
178
|
+
() =>
|
|
179
|
+
useActorInfiniteQuery<TestActor, "getItems", "candid", number>({
|
|
180
|
+
reactor: mockReactor as unknown as Reactor<TestActor, "candid">,
|
|
181
|
+
functionName: "getItems",
|
|
182
|
+
initialPageParam: 0,
|
|
183
|
+
getArgs: (cursor) => [cursor],
|
|
184
|
+
getNextPageParam: (lastPage) => lastPage.nextCursor,
|
|
185
|
+
}),
|
|
186
|
+
{ wrapper }
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
await waitFor(() => {
|
|
190
|
+
expect(result.current.isSuccess).toBe(true)
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
expect(result.current.data?.pages[0].items[0]).toBe("Item 1")
|
|
194
|
+
expect(result.current.hasNextPage).toBe(true)
|
|
195
|
+
|
|
196
|
+
await act(async () => {
|
|
197
|
+
await result.current.fetchNextPage()
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
await waitFor(() => {
|
|
201
|
+
expect(result.current.data?.pages).toHaveLength(2)
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
expect(result.current.data?.pages[1].items[0]).toBe("Item 11")
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
it("should determine hasNextPage based on getNextPageParam", async () => {
|
|
208
|
+
// Create a mock that returns null nextCursor to indicate no more pages
|
|
209
|
+
const noMorePagesMock = createMockReactor(queryClient)
|
|
210
|
+
;(
|
|
211
|
+
noMorePagesMock.callMethod as ReturnType<typeof vi.fn>
|
|
212
|
+
).mockImplementation(async () => {
|
|
213
|
+
return { items: ["Last Item"], nextCursor: null }
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
const { result } = renderHook(
|
|
217
|
+
() =>
|
|
218
|
+
useActorInfiniteQuery<TestActor, "getItems", "candid", number>({
|
|
219
|
+
reactor: noMorePagesMock as unknown as Reactor<TestActor, "candid">,
|
|
220
|
+
functionName: "getItems",
|
|
221
|
+
initialPageParam: 0,
|
|
222
|
+
getArgs: (cursor) => [cursor],
|
|
223
|
+
getNextPageParam: (lastPage) => lastPage.nextCursor,
|
|
224
|
+
}),
|
|
225
|
+
{ wrapper }
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
await waitFor(() => {
|
|
229
|
+
expect(result.current.isSuccess).toBe(true)
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
// When getNextPageParam returns null, hasNextPage should be false
|
|
233
|
+
expect(result.current.hasNextPage).toBe(false)
|
|
234
|
+
})
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
describe("token-based pagination", () => {
|
|
238
|
+
it("should work with string token pagination", async () => {
|
|
239
|
+
const { result } = renderHook(
|
|
240
|
+
() =>
|
|
241
|
+
useActorInfiniteQuery<TestActor, "getMessages", "candid", string>({
|
|
242
|
+
reactor: mockReactor as unknown as Reactor<TestActor, "candid">,
|
|
243
|
+
functionName: "getMessages",
|
|
244
|
+
initialPageParam: "start",
|
|
245
|
+
getArgs: (token) => [token],
|
|
246
|
+
getNextPageParam: (lastPage) => lastPage.nextToken,
|
|
247
|
+
}),
|
|
248
|
+
{ wrapper }
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
await waitFor(() => {
|
|
252
|
+
expect(result.current.isSuccess).toBe(true)
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
expect(result.current.data?.pages[0].messages[0]).toBe("Message 1")
|
|
256
|
+
})
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
describe("query options", () => {
|
|
260
|
+
it("should respect enabled: false option", async () => {
|
|
261
|
+
const { result } = renderHook(
|
|
262
|
+
() =>
|
|
263
|
+
useActorInfiniteQuery({
|
|
264
|
+
reactor: mockReactor,
|
|
265
|
+
functionName: "getPosts",
|
|
266
|
+
initialPageParam: 1,
|
|
267
|
+
getArgs: (page) => [{ page, limit: 10 }] as const,
|
|
268
|
+
getNextPageParam: () => undefined,
|
|
269
|
+
enabled: false,
|
|
270
|
+
}),
|
|
271
|
+
{ wrapper }
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
expect(result.current.isPending).toBe(true)
|
|
275
|
+
expect(result.current.fetchStatus).toBe("idle")
|
|
276
|
+
expect(mockReactor.callMethod).not.toHaveBeenCalled()
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
it("should use custom staleTime", async () => {
|
|
280
|
+
const { result } = renderHook(
|
|
281
|
+
() =>
|
|
282
|
+
useActorInfiniteQuery({
|
|
283
|
+
reactor: mockReactor,
|
|
284
|
+
functionName: "getPosts",
|
|
285
|
+
initialPageParam: 1,
|
|
286
|
+
getArgs: (page) => [{ page, limit: 10 }] as const,
|
|
287
|
+
getNextPageParam: () => undefined,
|
|
288
|
+
staleTime: 1000 * 60 * 10, // 10 minutes
|
|
289
|
+
}),
|
|
290
|
+
{ wrapper }
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
await waitFor(() => {
|
|
294
|
+
expect(result.current.isSuccess).toBe(true)
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
expect(result.current.isStale).toBe(false)
|
|
298
|
+
})
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
describe("getPreviousPageParam", () => {
|
|
302
|
+
it("should support bi-directional pagination", async () => {
|
|
303
|
+
const { result } = renderHook(
|
|
304
|
+
() =>
|
|
305
|
+
useActorInfiniteQuery({
|
|
306
|
+
reactor: mockReactor,
|
|
307
|
+
functionName: "getPosts",
|
|
308
|
+
initialPageParam: 3,
|
|
309
|
+
getArgs: (page) => [{ page, limit: 10 }] as const,
|
|
310
|
+
getNextPageParam: (lastPage, allPages) => {
|
|
311
|
+
if (lastPage.length < 10) return undefined
|
|
312
|
+
const lastPageParam = allPages.length + 2 // Started at page 3
|
|
313
|
+
return lastPageParam + 1
|
|
314
|
+
},
|
|
315
|
+
getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => {
|
|
316
|
+
if (firstPageParam <= 1) return undefined
|
|
317
|
+
return firstPageParam - 1
|
|
318
|
+
},
|
|
319
|
+
}),
|
|
320
|
+
{ wrapper }
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
await waitFor(() => {
|
|
324
|
+
expect(result.current.isSuccess).toBe(true)
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
// Started at page 3, should have previous pages
|
|
328
|
+
expect(result.current.hasPreviousPage).toBe(true)
|
|
329
|
+
})
|
|
330
|
+
})
|
|
331
|
+
|
|
332
|
+
describe("select function", () => {
|
|
333
|
+
it("should apply select transformation to infinite data", async () => {
|
|
334
|
+
const { result } = renderHook(
|
|
335
|
+
() =>
|
|
336
|
+
useActorInfiniteQuery({
|
|
337
|
+
reactor: mockReactor,
|
|
338
|
+
functionName: "getPosts",
|
|
339
|
+
initialPageParam: 1,
|
|
340
|
+
getArgs: (page) => [{ page, limit: 10 }] as const,
|
|
341
|
+
getNextPageParam: () => undefined,
|
|
342
|
+
select: (data) => ({
|
|
343
|
+
...data,
|
|
344
|
+
pages: data.pages.map((page) =>
|
|
345
|
+
page.map((post) => post.toUpperCase())
|
|
346
|
+
),
|
|
347
|
+
}),
|
|
348
|
+
}),
|
|
349
|
+
{ wrapper }
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
await waitFor(() => {
|
|
353
|
+
expect(result.current.isSuccess).toBe(true)
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
expect(result.current.data?.pages[0][0]).toBe("POST 1")
|
|
357
|
+
})
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
describe("refetching", () => {
|
|
361
|
+
it("should support manual refetch", async () => {
|
|
362
|
+
const { result } = renderHook(
|
|
363
|
+
() =>
|
|
364
|
+
useActorInfiniteQuery({
|
|
365
|
+
reactor: mockReactor,
|
|
366
|
+
functionName: "getPosts",
|
|
367
|
+
initialPageParam: 1,
|
|
368
|
+
getArgs: (page) => [{ page, limit: 10 }] as const,
|
|
369
|
+
getNextPageParam: () => undefined,
|
|
370
|
+
}),
|
|
371
|
+
{ wrapper }
|
|
372
|
+
)
|
|
373
|
+
|
|
374
|
+
await waitFor(() => {
|
|
375
|
+
expect(result.current.isSuccess).toBe(true)
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
const initialCallCount = (
|
|
379
|
+
mockReactor.callMethod as ReturnType<typeof vi.fn>
|
|
380
|
+
).mock.calls.length
|
|
381
|
+
|
|
382
|
+
await act(async () => {
|
|
383
|
+
await result.current.refetch()
|
|
384
|
+
})
|
|
385
|
+
|
|
386
|
+
expect(
|
|
387
|
+
(mockReactor.callMethod as ReturnType<typeof vi.fn>).mock.calls.length
|
|
388
|
+
).toBeGreaterThan(initialCallCount)
|
|
389
|
+
})
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
describe("maxPages option", () => {
|
|
393
|
+
it("should accept maxPages option", async () => {
|
|
394
|
+
const { result } = renderHook(
|
|
395
|
+
() =>
|
|
396
|
+
useActorInfiniteQuery({
|
|
397
|
+
reactor: mockReactor,
|
|
398
|
+
functionName: "getPosts",
|
|
399
|
+
initialPageParam: 1,
|
|
400
|
+
getArgs: (page) => [{ page, limit: 10 }] as const,
|
|
401
|
+
getNextPageParam: (lastPage, allPages) => {
|
|
402
|
+
if (lastPage.length < 10) return undefined
|
|
403
|
+
return allPages.length + 1
|
|
404
|
+
},
|
|
405
|
+
maxPages: 2,
|
|
406
|
+
}),
|
|
407
|
+
{ wrapper }
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
await waitFor(() => {
|
|
411
|
+
expect(result.current.isSuccess).toBe(true)
|
|
412
|
+
})
|
|
413
|
+
|
|
414
|
+
// Initial page should be loaded
|
|
415
|
+
expect(result.current.data?.pages).toHaveLength(1)
|
|
416
|
+
// maxPages option should be respected by tanstack query
|
|
417
|
+
expect(result.current.hasNextPage).toBe(true)
|
|
418
|
+
})
|
|
419
|
+
})
|
|
420
|
+
|
|
421
|
+
describe("data structure", () => {
|
|
422
|
+
it("should return data in InfiniteData format", async () => {
|
|
423
|
+
const { result } = renderHook(
|
|
424
|
+
() =>
|
|
425
|
+
useActorInfiniteQuery({
|
|
426
|
+
reactor: mockReactor,
|
|
427
|
+
functionName: "getPosts",
|
|
428
|
+
initialPageParam: 1,
|
|
429
|
+
getArgs: (page) => [{ page, limit: 10 }] as const,
|
|
430
|
+
getNextPageParam: (lastPage, allPages) => {
|
|
431
|
+
if (lastPage.length < 10) return undefined
|
|
432
|
+
return allPages.length + 1
|
|
433
|
+
},
|
|
434
|
+
}),
|
|
435
|
+
{ wrapper }
|
|
436
|
+
)
|
|
437
|
+
|
|
438
|
+
await waitFor(() => {
|
|
439
|
+
expect(result.current.isSuccess).toBe(true)
|
|
440
|
+
})
|
|
441
|
+
|
|
442
|
+
// Data should have pages array and pageParams array
|
|
443
|
+
expect(result.current.data).toBeDefined()
|
|
444
|
+
expect(Array.isArray(result.current.data?.pages)).toBe(true)
|
|
445
|
+
expect(Array.isArray(result.current.data?.pageParams)).toBe(true)
|
|
446
|
+
|
|
447
|
+
// Initial page should be loaded
|
|
448
|
+
expect(result.current.data?.pages).toHaveLength(1)
|
|
449
|
+
expect(result.current.data?.pages[0]).toHaveLength(10)
|
|
450
|
+
|
|
451
|
+
// Pages can be flattened
|
|
452
|
+
const allPosts = result.current.data?.pages.flat()
|
|
453
|
+
expect(allPosts).toHaveLength(10)
|
|
454
|
+
expect(allPosts?.[0]).toBe("Post 1")
|
|
455
|
+
})
|
|
456
|
+
})
|
|
457
|
+
})
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { useMemo, useCallback } from "react"
|
|
2
|
+
import {
|
|
3
|
+
QueryKey,
|
|
4
|
+
useInfiniteQuery,
|
|
5
|
+
UseInfiniteQueryResult,
|
|
6
|
+
UseInfiniteQueryOptions,
|
|
7
|
+
InfiniteData,
|
|
8
|
+
} from "@tanstack/react-query"
|
|
9
|
+
import {
|
|
10
|
+
FunctionName,
|
|
11
|
+
Reactor,
|
|
12
|
+
TransformKey,
|
|
13
|
+
ReactorArgs,
|
|
14
|
+
ReactorReturnOk,
|
|
15
|
+
ReactorReturnErr,
|
|
16
|
+
} from "@ic-reactor/core"
|
|
17
|
+
import { CallConfig } from "@icp-sdk/core/agent"
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Parameters for useActorInfiniteQuery hook.
|
|
21
|
+
* Extends react-query's UseInfiniteQueryOptions with custom reactor params.
|
|
22
|
+
*/
|
|
23
|
+
export interface UseActorInfiniteQueryParameters<
|
|
24
|
+
A,
|
|
25
|
+
M extends FunctionName<A>,
|
|
26
|
+
T extends TransformKey = "candid",
|
|
27
|
+
TPageParam = unknown,
|
|
28
|
+
TSelected = InfiniteData<ReactorReturnOk<A, M, T>, TPageParam>,
|
|
29
|
+
> extends Omit<
|
|
30
|
+
UseInfiniteQueryOptions<
|
|
31
|
+
ReactorReturnOk<A, M, T>,
|
|
32
|
+
ReactorReturnErr<A, M, T>,
|
|
33
|
+
TSelected,
|
|
34
|
+
QueryKey,
|
|
35
|
+
TPageParam
|
|
36
|
+
>,
|
|
37
|
+
"queryKey" | "queryFn" | "getNextPageParam" | "initialPageParam"
|
|
38
|
+
> {
|
|
39
|
+
/** The reactor instance to use for method calls */
|
|
40
|
+
reactor: Reactor<A, T>
|
|
41
|
+
/** The method name to call on the canister */
|
|
42
|
+
functionName: M
|
|
43
|
+
/** Function to get args from page parameter */
|
|
44
|
+
getArgs: (pageParam: TPageParam) => ReactorArgs<A, M, T>
|
|
45
|
+
/** Agent call configuration (effectiveCanisterId, etc.) */
|
|
46
|
+
callConfig?: CallConfig
|
|
47
|
+
/** Custom query key (auto-generated if not provided) */
|
|
48
|
+
queryKey?: QueryKey
|
|
49
|
+
/** Initial page parameter */
|
|
50
|
+
initialPageParam: TPageParam
|
|
51
|
+
/** Function to determine next page parameter */
|
|
52
|
+
getNextPageParam: (
|
|
53
|
+
lastPage: ReactorReturnOk<A, M, T>,
|
|
54
|
+
allPages: ReactorReturnOk<A, M, T>[],
|
|
55
|
+
lastPageParam: TPageParam,
|
|
56
|
+
allPageParams: TPageParam[]
|
|
57
|
+
) => TPageParam | undefined | null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type UseActorInfiniteQueryConfig<
|
|
61
|
+
A,
|
|
62
|
+
M extends FunctionName<A>,
|
|
63
|
+
T extends TransformKey = "candid",
|
|
64
|
+
TPageParam = unknown,
|
|
65
|
+
> = Omit<UseActorInfiniteQueryParameters<A, M, T, TPageParam>, "reactor">
|
|
66
|
+
|
|
67
|
+
export type UseActorInfiniteQueryResult<
|
|
68
|
+
A,
|
|
69
|
+
M extends FunctionName<A>,
|
|
70
|
+
T extends TransformKey = "candid",
|
|
71
|
+
TPageParam = unknown,
|
|
72
|
+
> = UseInfiniteQueryResult<
|
|
73
|
+
InfiniteData<ReactorReturnOk<A, M, T>, TPageParam>,
|
|
74
|
+
ReactorReturnErr<A, M, T>
|
|
75
|
+
>
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Hook for executing infinite/paginated query calls on a canister.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* const { data, fetchNextPage, hasNextPage } = useActorInfiniteQuery({
|
|
82
|
+
* reactor,
|
|
83
|
+
* functionName: "getItems",
|
|
84
|
+
* getArgs: (pageParam) => [{ offset: pageParam, limit: 10 }],
|
|
85
|
+
* initialPageParam: 0,
|
|
86
|
+
* getNextPageParam: (lastPage) => lastPage.nextOffset,
|
|
87
|
+
* })
|
|
88
|
+
*/
|
|
89
|
+
export const useActorInfiniteQuery = <
|
|
90
|
+
A,
|
|
91
|
+
M extends FunctionName<A>,
|
|
92
|
+
T extends TransformKey = "candid",
|
|
93
|
+
TPageParam = unknown,
|
|
94
|
+
>({
|
|
95
|
+
reactor,
|
|
96
|
+
functionName,
|
|
97
|
+
getArgs,
|
|
98
|
+
callConfig,
|
|
99
|
+
queryKey,
|
|
100
|
+
...options
|
|
101
|
+
}: UseActorInfiniteQueryParameters<
|
|
102
|
+
A,
|
|
103
|
+
M,
|
|
104
|
+
T,
|
|
105
|
+
TPageParam
|
|
106
|
+
>): UseActorInfiniteQueryResult<A, M, T, TPageParam> => {
|
|
107
|
+
// Memoize queryKey to prevent unnecessary re-calculations
|
|
108
|
+
const baseQueryKey = useMemo(
|
|
109
|
+
() => queryKey ?? reactor.generateQueryKey({ functionName }),
|
|
110
|
+
[queryKey, reactor, functionName]
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
// Memoize queryFn to prevent recreation on every render
|
|
114
|
+
const queryFn = useCallback(
|
|
115
|
+
async ({ pageParam }: { pageParam: TPageParam }) => {
|
|
116
|
+
const args = getArgs(pageParam)
|
|
117
|
+
return reactor.callMethod({
|
|
118
|
+
functionName,
|
|
119
|
+
args,
|
|
120
|
+
callConfig,
|
|
121
|
+
})
|
|
122
|
+
},
|
|
123
|
+
[reactor, functionName, getArgs, callConfig]
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
return useInfiniteQuery(
|
|
127
|
+
{
|
|
128
|
+
queryKey: baseQueryKey,
|
|
129
|
+
queryFn,
|
|
130
|
+
...options,
|
|
131
|
+
} as any,
|
|
132
|
+
reactor.queryClient
|
|
133
|
+
) as UseActorInfiniteQueryResult<A, M, T, TPageParam>
|
|
134
|
+
}
|