Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { renderHook } from '@testing-library/react'
3import { createCollection } from '../../db/src/collection/index'
4import { collectionOptions } from '../../db/src/index'
5import { mockSyncCollectionOptions } from '../../db/tests/utils'
6import {
7 Query,
8 createLiveQueryCollection,
9 eq,
10 liveQueryCollectionOptions,
11} from '../../db/src/query/index'
12import { useLiveQuery } from '../src/useLiveQuery'
13import { useLiveInfiniteQuery } from '../src/useLiveInfiniteQuery'
14import { useLiveSuspenseQuery } from '../src/useLiveSuspenseQuery'
15import { useDbClient } from '../src/DbProvider'
16import { HydrationBoundary } from '../src/HydrationBoundary'
17import type { DbClient, DehydratedDbState } from '../../db/src/index'
18import type { JSX } from 'react'
19import type { OutputWithVirtual } from '../../db/tests/utils'
20import type { SingleResult } from '../../db/src/types'
21import type { Prettify, QueryBuilder } from '../../db/src/query/index'
22import type {
23 ConditionalUseLiveQueryConfig,
24 UseLiveQueryConfig,
25 UseLiveQueryStatus,
26} from '../src/index'
27
28type Person = {
29 id: string
30 name: string
31 age: number
32 email: string
33 isActive: boolean
34 team: string
35}
36
37describe(`useLiveQuery type assertions`, () => {
38 it(`should type useDbClient as DbClient`, () => {
39 const client = useDbClient()
40 expectTypeOf(client).toEqualTypeOf<DbClient>()
41 })
42
43 it(`types HydrationBoundary state`, () => {
44 const state: DehydratedDbState = { collections: [] }
45 const boundary = (
46 <HydrationBoundary state={state}>
47 <div />
48 </HydrationBoundary>
49 )
50
51 expectTypeOf(boundary).toEqualTypeOf<JSX.Element>()
52 })
53
54 it(`should type findOne query builder to return a single row`, () => {
55 const collection = createCollection(
56 mockSyncCollectionOptions<Person>({
57 id: `test-persons-2`,
58 getKey: (person: Person) => person.id,
59 initialData: [],
60 }),
61 )
62
63 const { result } = renderHook(() => {
64 return useLiveQuery((q) =>
65 q
66 .from({ collection })
67 .where(({ collection: c }) => eq(c.id, `3`))
68 .findOne(),
69 )
70 })
71
72 expectTypeOf(result.current.data).toMatchTypeOf<
73 OutputWithVirtual<Person> | undefined
74 >()
75 })
76
77 it(`should type findOne config object to return a single row`, () => {
78 const collection = createCollection(
79 mockSyncCollectionOptions<Person>({
80 id: `test-persons-2`,
81 getKey: (person: Person) => person.id,
82 initialData: [],
83 }),
84 )
85
86 const { result } = renderHook(() => {
87 return useLiveQuery({
88 query: (q) =>
89 q
90 .from({ collection })
91 .where(({ collection: c }) => eq(c.id, `3`))
92 .findOne(),
93 })
94 })
95
96 expectTypeOf(result.current.data).toMatchTypeOf<
97 OutputWithVirtual<Person> | undefined
98 >()
99 })
100
101 it(`types a conditional findOne config object as disabled-capable`, () => {
102 const collection = createCollection(
103 mockSyncCollectionOptions<Person>({
104 id: `test-conditional-person-config`,
105 getKey: (person: Person) => person.id,
106 initialData: [],
107 }),
108 )
109 const enabled = null as unknown as boolean
110 const query = new Query()
111 .from({ collection })
112 .where(({ collection: c }) => eq(c.id, `3`))
113 .findOne()
114 type QueryContext =
115 typeof query extends QueryBuilder<infer TContext> ? TContext : never
116 const config: ConditionalUseLiveQueryConfig<QueryContext> = {
117 queryKey: [collection.id, enabled],
118 query: () => (enabled ? query : undefined),
119 }
120
121 const { result } = renderHook(() => {
122 return useLiveQuery(config)
123 })
124
125 expectTypeOf(result.current.data).toMatchTypeOf<
126 OutputWithVirtual<Person> | undefined
127 >()
128 expectTypeOf(result.current.status).toEqualTypeOf<UseLiveQueryStatus>()
129 expectTypeOf(result.current.isEnabled).toEqualTypeOf<boolean>()
130 })
131
132 it(`accepts an annotated enabled config in useLiveSuspenseQuery`, () => {
133 const collection = createCollection(
134 mockSyncCollectionOptions<Person>({
135 id: `test-annotated-suspense-config`,
136 getKey: (person: Person) => person.id,
137 initialData: [],
138 }),
139 )
140 const query = new Query().from({ collection })
141 type QueryContext =
142 typeof query extends QueryBuilder<infer TContext> ? TContext : never
143 const config: UseLiveQueryConfig<QueryContext> = {
144 query: () => query,
145 }
146
147 const { result } = renderHook(() => useLiveSuspenseQuery(config))
148
149 expectTypeOf(result.current.data).toMatchTypeOf<
150 Array<OutputWithVirtual<Person>>
151 >()
152 })
153
154 it(`types a conditional config with an explicit query key`, () => {
155 const collection = createCollection(
156 mockSyncCollectionOptions<Person>({
157 id: `test-conditional-config-deps`,
158 getKey: (person: Person) => person.id,
159 initialData: [],
160 }),
161 )
162 const enabled = null as unknown as boolean
163
164 const { result } = renderHook(() =>
165 useLiveQuery({
166 query: (q) => (enabled ? q.from({ collection }).findOne() : undefined),
167 queryKey: [enabled],
168 }),
169 )
170
171 expectTypeOf(result.current.data).toMatchTypeOf<
172 OutputWithVirtual<Person> | undefined
173 >()
174 expectTypeOf(result.current.status).toEqualTypeOf<UseLiveQueryStatus>()
175 expectTypeOf(result.current.isEnabled).toEqualTypeOf<boolean>()
176 })
177
178 /**
179 * React's public disabled result is absent rather than empty-reactive. The
180 * observation cut is `result.current` from the real `useLiveQuery` hook; the
181 * negative `map` call rejects an array-only disabled type. Runtime lifecycle
182 * and scheduler behavior remain in React conformance tests.
183 */
184 it(`types disabled callbacks with React's absent result representation`, () => {
185 const collection = createCollection(
186 mockSyncCollectionOptions<Person>({
187 id: `test-conditional-callback`,
188 getKey: (person: Person) => person.id,
189 initialData: [],
190 }),
191 )
192 const enabled = null as unknown as boolean
193
194 const { result } = renderHook(() =>
195 useLiveQuery((q) => (enabled ? q.from({ collection }) : null)),
196 )
197
198 expectTypeOf(result.current.data).toEqualTypeOf<
199 Array<Prettify<OutputWithVirtual<Person>>> | undefined
200 >()
201 expectTypeOf(result.current.status).toEqualTypeOf<UseLiveQueryStatus>()
202 expectTypeOf(result.current.isEnabled).toEqualTypeOf<boolean>()
203
204 // @ts-expect-error React omits disabled query data until the callback enables it.
205 result.current.data.map((person) => person.id)
206 })
207
208 it(`rejects a conditional config with a top-level scalar result`, () => {
209 const collection = createCollection(
210 mockSyncCollectionOptions<Person>({
211 id: `test-conditional-scalar-config`,
212 getKey: (person: Person) => person.id,
213 initialData: [],
214 }),
215 )
216 const enabled = null as unknown as boolean
217
218 useLiveQuery({
219 // @ts-expect-error - top-level scalar results are not supported
220 query: (q) => {
221 if (!enabled) return undefined
222 return q.from({ collection }).select(({ collection: c }) => c.name)
223 },
224 })
225 })
226
227 it(`should type config object to return query rows without queryKey`, () => {
228 const collection = createCollection(
229 mockSyncCollectionOptions<Person>({
230 id: `test-persons-query-key`,
231 getKey: (person: Person) => person.id,
232 initialData: [],
233 }),
234 )
235
236 const { result } = renderHook(() => {
237 return useLiveQuery({
238 query: (q) =>
239 q
240 .from({ collection })
241 .where(({ collection: c }) => eq(c.team, `team-1`)),
242 })
243 })
244
245 expectTypeOf(result.current.data).toMatchTypeOf<
246 Array<OutputWithVirtual<Person>>
247 >()
248 })
249
250 it(`should type queryKey and a per-call DbClient override`, () => {
251 const descriptor = collectionOptions(
252 mockSyncCollectionOptions<Person>({
253 id: `test-persons-client-override`,
254 getKey: (person: Person) => person.id,
255 initialData: [],
256 }),
257 )
258 const client = null as unknown as DbClient
259
260 const { result } = renderHook(() => {
261 return useLiveQuery({
262 client,
263 queryKey: [descriptor.id, `team`, `team-1`],
264 query: (q) =>
265 q
266 .from({ person: descriptor })
267 .where(({ person }) => eq(person.team, `team-1`)),
268 })
269 })
270
271 expectTypeOf(result.current.data).toMatchTypeOf<
272 Array<OutputWithVirtual<Person>>
273 >()
274 })
275
276 it(`types an explicit queryKey config`, () => {
277 const collection = createCollection(
278 mockSyncCollectionOptions<Person>({
279 id: `test-persons-deprecated-deps`,
280 getKey: (person: Person) => person.id,
281 initialData: [],
282 }),
283 )
284
285 const { result } = renderHook(() =>
286 useLiveQuery({
287 query: (q) =>
288 q
289 .from({ person: collection })
290 .where(({ person }) => eq(person.team, `team-1`)),
291 queryKey: [`team-1`],
292 }),
293 )
294
295 expectTypeOf(result.current.data).toMatchTypeOf<
296 Array<OutputWithVirtual<Person>>
297 >()
298 })
299
300 it(`should type collection descriptors in query sources`, () => {
301 const collection = collectionOptions(
302 mockSyncCollectionOptions<Person>({
303 id: `test-persons-descriptor-query-source`,
304 getKey: (person: Person) => person.id,
305 initialData: [],
306 }),
307 )
308
309 const { result } = renderHook(() => {
310 return useLiveQuery({
311 query: (q) =>
312 q
313 .from({ collection })
314 .where(({ collection: c }) => eq(c.team, `team-1`)),
315 })
316 })
317
318 expectTypeOf(result.current.data).toMatchTypeOf<
319 Array<OutputWithVirtual<Person>>
320 >()
321 })
322
323 it(`should type suspense config object to return query rows without queryKey`, () => {
324 const collection = createCollection(
325 mockSyncCollectionOptions<Person>({
326 id: `test-persons-suspense-query-key`,
327 getKey: (person: Person) => person.id,
328 initialData: [],
329 }),
330 )
331
332 const { result } = renderHook(() => {
333 return useLiveSuspenseQuery({
334 query: (q) =>
335 q
336 .from({ collection })
337 .where(({ collection: c }) => eq(c.team, `team-1`)),
338 })
339 })
340
341 expectTypeOf(result.current.data).toMatchTypeOf<
342 Array<OutputWithVirtual<Person>>
343 >()
344 })
345
346 it(`should type infinite config object to return query rows without queryKey`, () => {
347 const collection = createCollection(
348 mockSyncCollectionOptions<Person>({
349 id: `test-persons-infinite-query-key`,
350 getKey: (person: Person) => person.id,
351 initialData: [],
352 }),
353 )
354
355 const { result } = renderHook(() => {
356 return useLiveInfiniteQuery(
357 (q) => q.from({ collection }).orderBy(({ collection: c }) => c.name),
358 {
359 pageSize: 10,
360 },
361 )
362 })
363
364 expectTypeOf(result.current.data).toMatchTypeOf<
365 Array<OutputWithVirtual<Person>>
366 >()
367 })
368
369 it(`should type findOne collection using liveQueryCollectionOptions to return a single row`, () => {
370 const collection = createCollection(
371 mockSyncCollectionOptions<Person>({
372 id: `test-persons-2`,
373 getKey: (person: Person) => person.id,
374 initialData: [],
375 }),
376 )
377
378 const options = liveQueryCollectionOptions({
379 query: (q) =>
380 q
381 .from({ collection })
382 .where(({ collection: c }) => eq(c.id, `3`))
383 .findOne(),
384 })
385
386 const liveQueryCollection = createCollection(options)
387
388 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
389
390 const { result } = renderHook(() => {
391 return useLiveQuery(liveQueryCollection)
392 })
393
394 expectTypeOf(result.current.data).toMatchTypeOf<
395 OutputWithVirtual<Person> | undefined
396 >()
397 })
398
399 it(`should type findOne collection using createLiveQueryCollection to return a single row`, () => {
400 const collection = createCollection(
401 mockSyncCollectionOptions<Person>({
402 id: `test-persons-2`,
403 getKey: (person: Person) => person.id,
404 initialData: [],
405 }),
406 )
407
408 const liveQueryCollection = createLiveQueryCollection({
409 query: (q) =>
410 q
411 .from({ collection })
412 .where(({ collection: c }) => eq(c.id, `3`))
413 .findOne(),
414 })
415
416 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
417
418 const { result } = renderHook(() => {
419 return useLiveQuery(liveQueryCollection)
420 })
421
422 expectTypeOf(result.current.data).toMatchTypeOf<
423 OutputWithVirtual<Person> | undefined
424 >()
425 })
426})