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 deprecated dependencies`, () => {
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 {
167 query: (q) =>
168 enabled ? q.from({ collection }).findOne() : undefined,
169 },
170 [enabled],
171 ),
172 )
173
174 expectTypeOf(result.current.data).toMatchTypeOf<
175 OutputWithVirtual<Person> | undefined
176 >()
177 expectTypeOf(result.current.status).toEqualTypeOf<UseLiveQueryStatus>()
178 expectTypeOf(result.current.isEnabled).toEqualTypeOf<boolean>()
179 })
180
181 /**
182 * React's public disabled result is absent rather than empty-reactive. The
183 * observation cut is `result.current` from the real `useLiveQuery` hook; the
184 * negative `map` call rejects an array-only disabled type. Runtime lifecycle
185 * and scheduler behavior remain in React conformance tests.
186 */
187 it(`types disabled callbacks with React's absent result representation`, () => {
188 const collection = createCollection(
189 mockSyncCollectionOptions<Person>({
190 id: `test-conditional-callback`,
191 getKey: (person: Person) => person.id,
192 initialData: [],
193 }),
194 )
195 const enabled = null as unknown as boolean
196
197 const { result } = renderHook(() =>
198 useLiveQuery((q) => (enabled ? q.from({ collection }) : null)),
199 )
200
201 expectTypeOf(result.current.data).toEqualTypeOf<
202 Array<Prettify<OutputWithVirtual<Person>>> | undefined
203 >()
204 expectTypeOf(result.current.status).toEqualTypeOf<UseLiveQueryStatus>()
205 expectTypeOf(result.current.isEnabled).toEqualTypeOf<boolean>()
206
207 // @ts-expect-error React omits disabled query data until the callback enables it.
208 result.current.data.map((person) => person.id)
209 })
210
211 it(`rejects a conditional config with a top-level scalar result`, () => {
212 const collection = createCollection(
213 mockSyncCollectionOptions<Person>({
214 id: `test-conditional-scalar-config`,
215 getKey: (person: Person) => person.id,
216 initialData: [],
217 }),
218 )
219 const enabled = null as unknown as boolean
220
221 useLiveQuery({
222 // @ts-expect-error - top-level scalar results are not supported
223 query: (q) => {
224 if (!enabled) return undefined
225 return q.from({ collection }).select(({ collection: c }) => c.name)
226 },
227 })
228 })
229
230 it(`should type config object to return query rows without queryKey`, () => {
231 const collection = createCollection(
232 mockSyncCollectionOptions<Person>({
233 id: `test-persons-query-key`,
234 getKey: (person: Person) => person.id,
235 initialData: [],
236 }),
237 )
238
239 const { result } = renderHook(() => {
240 return useLiveQuery({
241 query: (q) =>
242 q
243 .from({ collection })
244 .where(({ collection: c }) => eq(c.team, `team-1`)),
245 })
246 })
247
248 expectTypeOf(result.current.data).toMatchTypeOf<
249 Array<OutputWithVirtual<Person>>
250 >()
251 })
252
253 it(`should type queryKey and a per-call DbClient override`, () => {
254 const descriptor = collectionOptions(
255 mockSyncCollectionOptions<Person>({
256 id: `test-persons-client-override`,
257 getKey: (person: Person) => person.id,
258 initialData: [],
259 }),
260 )
261 const client = null as unknown as DbClient
262
263 const { result } = renderHook(() => {
264 return useLiveQuery({
265 client,
266 queryKey: [descriptor.id, `team`, `team-1`],
267 query: (q) =>
268 q
269 .from({ person: descriptor })
270 .where(({ person }) => eq(person.team, `team-1`)),
271 })
272 })
273
274 expectTypeOf(result.current.data).toMatchTypeOf<
275 Array<OutputWithVirtual<Person>>
276 >()
277 })
278
279 it(`keeps the deprecated dependency-array overload typed`, () => {
280 const collection = createCollection(
281 mockSyncCollectionOptions<Person>({
282 id: `test-persons-deprecated-deps`,
283 getKey: (person: Person) => person.id,
284 initialData: [],
285 }),
286 )
287
288 const { result } = renderHook(() =>
289 useLiveQuery(
290 (q) =>
291 q
292 .from({ person: collection })
293 .where(({ person }) => eq(person.team, `team-1`)),
294 [`team-1`],
295 ),
296 )
297
298 expectTypeOf(result.current.data).toMatchTypeOf<
299 Array<OutputWithVirtual<Person>>
300 >()
301 })
302
303 it(`should type collection descriptors in query sources`, () => {
304 const collection = collectionOptions(
305 mockSyncCollectionOptions<Person>({
306 id: `test-persons-descriptor-query-source`,
307 getKey: (person: Person) => person.id,
308 initialData: [],
309 }),
310 )
311
312 const { result } = renderHook(() => {
313 return useLiveQuery({
314 query: (q) =>
315 q
316 .from({ collection })
317 .where(({ collection: c }) => eq(c.team, `team-1`)),
318 })
319 })
320
321 expectTypeOf(result.current.data).toMatchTypeOf<
322 Array<OutputWithVirtual<Person>>
323 >()
324 })
325
326 it(`should type suspense config object to return query rows without queryKey`, () => {
327 const collection = createCollection(
328 mockSyncCollectionOptions<Person>({
329 id: `test-persons-suspense-query-key`,
330 getKey: (person: Person) => person.id,
331 initialData: [],
332 }),
333 )
334
335 const { result } = renderHook(() => {
336 return useLiveSuspenseQuery({
337 query: (q) =>
338 q
339 .from({ collection })
340 .where(({ collection: c }) => eq(c.team, `team-1`)),
341 })
342 })
343
344 expectTypeOf(result.current.data).toMatchTypeOf<
345 Array<OutputWithVirtual<Person>>
346 >()
347 })
348
349 it(`should type infinite config object to return query rows without queryKey`, () => {
350 const collection = createCollection(
351 mockSyncCollectionOptions<Person>({
352 id: `test-persons-infinite-query-key`,
353 getKey: (person: Person) => person.id,
354 initialData: [],
355 }),
356 )
357
358 const { result } = renderHook(() => {
359 return useLiveInfiniteQuery(
360 (q) => q.from({ collection }).orderBy(({ collection: c }) => c.name),
361 {
362 pageSize: 10,
363 },
364 )
365 })
366
367 expectTypeOf(result.current.data).toMatchTypeOf<
368 Array<OutputWithVirtual<Person>>
369 >()
370 })
371
372 it(`should type findOne collection using liveQueryCollectionOptions to return a single row`, () => {
373 const collection = createCollection(
374 mockSyncCollectionOptions<Person>({
375 id: `test-persons-2`,
376 getKey: (person: Person) => person.id,
377 initialData: [],
378 }),
379 )
380
381 const options = liveQueryCollectionOptions({
382 query: (q) =>
383 q
384 .from({ collection })
385 .where(({ collection: c }) => eq(c.id, `3`))
386 .findOne(),
387 })
388
389 const liveQueryCollection = createCollection(options)
390
391 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
392
393 const { result } = renderHook(() => {
394 return useLiveQuery(liveQueryCollection)
395 })
396
397 expectTypeOf(result.current.data).toMatchTypeOf<
398 OutputWithVirtual<Person> | undefined
399 >()
400 })
401
402 it(`should type findOne collection using createLiveQueryCollection to return a single row`, () => {
403 const collection = createCollection(
404 mockSyncCollectionOptions<Person>({
405 id: `test-persons-2`,
406 getKey: (person: Person) => person.id,
407 initialData: [],
408 }),
409 )
410
411 const liveQueryCollection = createLiveQueryCollection({
412 query: (q) =>
413 q
414 .from({ collection })
415 .where(({ collection: c }) => eq(c.id, `3`))
416 .findOne(),
417 })
418
419 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
420
421 const { result } = renderHook(() => {
422 return useLiveQuery(liveQueryCollection)
423 })
424
425 expectTypeOf(result.current.data).toMatchTypeOf<
426 OutputWithVirtual<Person> | undefined
427 >()
428 })
429})