Open History to see the last commit
1/**
2 * React driver for the shared infinite-query conformance suite.
3 *
4 * `renderHook`, `act`, and the hook result are the only React-specific layer.
5 * Sources and query operators come from this package's module realm. The shared
6 * suite owns the page model and histories; this driver defines when React has
7 * committed enough work for those public observations to be read.
8 */
9import { act, renderHook } from '@testing-library/react'
10import {
11 BTreeIndex,
12 createCollection,
13 createLiveQueryCollection,
14 gt,
15} from '@tanstack/db'
16import { mockSyncCollectionOptions } from '../../db/tests/utils'
17import { runInfiniteQuerySuite } from '../../db/tests/conformance/infinite-suite'
18import { makeInfiniteOnDemandSource } from '../../db/tests/conformance/infinite-on-demand'
19import { useLiveInfiniteQuery } from '../src/useLiveInfiniteQuery'
20import type { RenderHookResult } from '@testing-library/react'
21import type {
22 InfiniteQueryConfig,
23 InfiniteQueryDriver,
24 InfiniteQueryHandle,
25} from '../../db/tests/conformance/infinite-contract'
26import type {
27 QueryBuild,
28 SourceHandle,
29} from '../../db/tests/conformance/contract'
30
31let sourceSequence = 0
32
33function makeSource<T extends { id: string }>(
34 initialData: ReadonlyArray<T>,
35): SourceHandle<T> {
36 const collection = createCollection(
37 mockSyncCollectionOptions<T>({
38 autoIndex: `eager`,
39 id: `infinite-conformance-react-${sourceSequence++}`,
40 getKey: (row) => row.id,
41 initialData: [...initialData],
42 }),
43 )
44 const write = (type: `insert` | `update` | `delete`, value: T) => {
45 collection.utils.begin()
46 collection.utils.write({ type, value })
47 collection.utils.commit()
48 }
49 return {
50 collection,
51 insert: (row) => write(`insert`, row),
52 update: (row) => write(`update`, row),
53 remove: (row) => write(`delete`, row),
54 }
55}
56
57function makePrecreated(build: QueryBuild) {
58 return {
59 collection: createLiveQueryCollection({ query: build as any }),
60 }
61}
62
63function makeHandle(hook: RenderHookResult<any, any>): InfiniteQueryHandle {
64 return {
65 current() {
66 const result = hook.result.current
67 return {
68 data: result.data,
69 pages: result.pages,
70 pageParams: result.pageParams,
71 hasNextPage: result.hasNextPage,
72 isFetchingNextPage: result.isFetchingNextPage,
73 error: result.error,
74 status: result.status,
75 collection: result.collection,
76 }
77 },
78 fetchNextPage() {
79 let request!: Promise<void>
80 act(() => {
81 request = hook.result.current.fetchNextPage()
82 })
83 return request
84 },
85 async flush() {
86 await act(async () => {
87 await new Promise((resolve) => setTimeout(resolve, 0))
88 })
89 },
90 async apply(fn) {
91 await act(async () => {
92 fn()
93 await Promise.resolve()
94 })
95 },
96 unmount() {
97 hook.unmount()
98 },
99 }
100}
101
102function mount(build: QueryBuild, config: InfiniteQueryConfig = {}) {
103 return makeHandle(
104 renderHook(() => useLiveInfiniteQuery(build as any, config as any)),
105 )
106}
107
108function mountControllable<P>(
109 build: (q: any, param: P) => any,
110 initial: P,
111 config: InfiniteQueryConfig = {},
112) {
113 const hook = renderHook(
114 ({ param }: { param: P }) =>
115 useLiveInfiniteQuery((q: any) => build(q, param), config as any, [param]),
116 { initialProps: { param: initial } },
117 )
118 const handle = makeHandle(hook)
119 return {
120 ...handle,
121 setParamSync(param: P) {
122 act(() => hook.rerender({ param }))
123 },
124 }
125}
126
127function mountCollection(collection: any, config: InfiniteQueryConfig = {}) {
128 return makeHandle(
129 renderHook(() => useLiveInfiniteQuery(collection, config as any)),
130 )
131}
132
133function mountCollectionControllable(
134 initial: any,
135 config: InfiniteQueryConfig = {},
136) {
137 const hook = renderHook(
138 ({ collection }) => useLiveInfiniteQuery(collection, config as any),
139 { initialProps: { collection: initial } },
140 )
141 const handle = makeHandle(hook)
142 return {
143 ...handle,
144 replaceCollectionSync(collection: any) {
145 act(() => hook.rerender({ collection }))
146 },
147 }
148}
149
150function mountConfigControllable(
151 build: QueryBuild,
152 initial: InfiniteQueryConfig,
153) {
154 const hook = renderHook(
155 ({ config }: { config: InfiniteQueryConfig }) =>
156 useLiveInfiniteQuery(build as any, config as any),
157 { initialProps: { config: initial } },
158 )
159 const handle = makeHandle(hook)
160 return {
161 ...handle,
162 setConfigSync(config: InfiniteQueryConfig) {
163 act(() => hook.rerender({ config }))
164 },
165 }
166}
167
168function mountInputControllable(
169 collection: any,
170 build: QueryBuild,
171 config: InfiniteQueryConfig = {},
172) {
173 const hook = renderHook(
174 ({ kind }: { kind: `collection` | `query` }) =>
175 useLiveInfiniteQuery(
176 kind === `collection` ? collection : build,
177 config as any,
178 [kind],
179 ),
180 {
181 initialProps: {
182 kind: `collection` as `collection` | `query`,
183 },
184 },
185 )
186 const handle = makeHandle(hook)
187 return {
188 ...handle,
189 setInputKindSync(kind: `collection` | `query`) {
190 act(() => hook.rerender({ kind }))
191 },
192 }
193}
194
195const reactInfiniteDriver: InfiniteQueryDriver = {
196 name: `react`,
197 gt,
198 makeSource,
199 makeOnDemandSource: (data, delay) =>
200 makeInfiniteOnDemandSource({ createCollection, BTreeIndex }, data, delay),
201 makePrecreated,
202 mount,
203 mountControllable,
204 mountCollection,
205 mountCollectionControllable,
206 mountConfigControllable,
207 mountInputControllable,
208 knownGaps: [],
209}
210
211runInfiniteQuerySuite(reactInfiniteDriver)