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 { 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 it(`rejects a conditional config with a top-level scalar result`, () => {
182 const collection = createCollection(
183 mockSyncCollectionOptions<Person>({
184 id: `test-conditional-scalar-config`,
185 getKey: (person: Person) => person.id,
186 initialData: [],
187 }),
188 )
189 const enabled = null as unknown as boolean
190
191 useLiveQuery({
192 // @ts-expect-error - top-level scalar results are not supported
193 query: (q) => {
194 if (!enabled) return undefined
195 return q.from({ collection }).select(({ collection: c }) => c.name)
196 },
197 })
198 })
199
200 it(`should type config object to return query rows without queryKey`, () => {
201 const collection = createCollection(
202 mockSyncCollectionOptions<Person>({
203 id: `test-persons-query-key`,
204 getKey: (person: Person) => person.id,
205 initialData: [],
206 }),
207 )
208
209 const { result } = renderHook(() => {
210 return useLiveQuery({
211 query: (q) =>
212 q
213 .from({ collection })
214 .where(({ collection: c }) => eq(c.team, `team-1`)),
215 })
216 })
217
218 expectTypeOf(result.current.data).toMatchTypeOf<
219 Array<OutputWithVirtual<Person>>
220 >()
221 })
222
223 it(`should type queryKey and a per-call DbClient override`, () => {
224 const descriptor = collectionOptions(
225 mockSyncCollectionOptions<Person>({
226 id: `test-persons-client-override`,
227 getKey: (person: Person) => person.id,
228 initialData: [],
229 }),
230 )
231 const client = null as unknown as DbClient
232
233 const { result } = renderHook(() => {
234 return useLiveQuery({
235 client,
236 queryKey: [descriptor.id, `team`, `team-1`],
237 query: (q) =>
238 q
239 .from({ person: descriptor })
240 .where(({ person }) => eq(person.team, `team-1`)),
241 })
242 })
243
244 expectTypeOf(result.current.data).toMatchTypeOf<
245 Array<OutputWithVirtual<Person>>
246 >()
247 })
248
249 it(`keeps the deprecated dependency-array overload typed`, () => {
250 const collection = createCollection(
251 mockSyncCollectionOptions<Person>({
252 id: `test-persons-deprecated-deps`,
253 getKey: (person: Person) => person.id,
254 initialData: [],
255 }),
256 )
257
258 const { result } = renderHook(() =>
259 useLiveQuery(
260 (q) =>
261 q
262 .from({ person: collection })
263 .where(({ person }) => eq(person.team, `team-1`)),
264 [`team-1`],
265 ),
266 )
267
268 expectTypeOf(result.current.data).toMatchTypeOf<
269 Array<OutputWithVirtual<Person>>
270 >()
271 })
272
273 it(`should type collection descriptors in query sources`, () => {
274 const collection = collectionOptions(
275 mockSyncCollectionOptions<Person>({
276 id: `test-persons-descriptor-query-source`,
277 getKey: (person: Person) => person.id,
278 initialData: [],
279 }),
280 )
281
282 const { result } = renderHook(() => {
283 return useLiveQuery({
284 query: (q) =>
285 q
286 .from({ collection })
287 .where(({ collection: c }) => eq(c.team, `team-1`)),
288 })
289 })
290
291 expectTypeOf(result.current.data).toMatchTypeOf<
292 Array<OutputWithVirtual<Person>>
293 >()
294 })
295
296 it(`should type suspense config object to return query rows without queryKey`, () => {
297 const collection = createCollection(
298 mockSyncCollectionOptions<Person>({
299 id: `test-persons-suspense-query-key`,
300 getKey: (person: Person) => person.id,
301 initialData: [],
302 }),
303 )
304
305 const { result } = renderHook(() => {
306 return useLiveSuspenseQuery({
307 query: (q) =>
308 q
309 .from({ collection })
310 .where(({ collection: c }) => eq(c.team, `team-1`)),
311 })
312 })
313
314 expectTypeOf(result.current.data).toMatchTypeOf<
315 Array<OutputWithVirtual<Person>>
316 >()
317 })
318
319 it(`should type infinite config object to return query rows without queryKey`, () => {
320 const collection = createCollection(
321 mockSyncCollectionOptions<Person>({
322 id: `test-persons-infinite-query-key`,
323 getKey: (person: Person) => person.id,
324 initialData: [],
325 }),
326 )
327
328 const { result } = renderHook(() => {
329 return useLiveInfiniteQuery(
330 (q) => q.from({ collection }).orderBy(({ collection: c }) => c.name),
331 {
332 pageSize: 10,
333 },
334 )
335 })
336
337 expectTypeOf(result.current.data).toMatchTypeOf<
338 Array<OutputWithVirtual<Person>>
339 >()
340 })
341
342 it(`should type findOne collection using liveQueryCollectionOptions to return a single row`, () => {
343 const collection = createCollection(
344 mockSyncCollectionOptions<Person>({
345 id: `test-persons-2`,
346 getKey: (person: Person) => person.id,
347 initialData: [],
348 }),
349 )
350
351 const options = liveQueryCollectionOptions({
352 query: (q) =>
353 q
354 .from({ collection })
355 .where(({ collection: c }) => eq(c.id, `3`))
356 .findOne(),
357 })
358
359 const liveQueryCollection = createCollection(options)
360
361 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
362
363 const { result } = renderHook(() => {
364 return useLiveQuery(liveQueryCollection)
365 })
366
367 expectTypeOf(result.current.data).toMatchTypeOf<
368 OutputWithVirtual<Person> | undefined
369 >()
370 })
371
372 it(`should type findOne collection using createLiveQueryCollection 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 liveQueryCollection = createLiveQueryCollection({
382 query: (q) =>
383 q
384 .from({ collection })
385 .where(({ collection: c }) => eq(c.id, `3`))
386 .findOne(),
387 })
388
389 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
390
391 const { result } = renderHook(() => {
392 return useLiveQuery(liveQueryCollection)
393 })
394
395 expectTypeOf(result.current.data).toMatchTypeOf<
396 OutputWithVirtual<Person> | undefined
397 >()
398 })
399})