Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

useLiveQuery.test-d.tsx

128 lines3.4 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { renderHook } from '@testing-library/react'
3import { createCollection } from '../../db/src/collection/index'
4import { mockSyncCollectionOptions } from '../../db/tests/utils'
5import {
6 createLiveQueryCollection,
7 eq,
8 liveQueryCollectionOptions,
9} from '../../db/src/query/index'
10import { useLiveQuery } from '../src/useLiveQuery'
11import type { OutputWithVirtual } from '../../db/tests/utils'
12import type { SingleResult } from '../../db/src/types'
13​
14type Person = {
15 id: string
16 name: string
17 age: number
18 email: string
19 isActive: boolean
20 team: string
22​
23describe(`useLiveQuery type assertions`, () => {
24 it(`should type findOne query builder to return a single row`, () => {
25 const collection = createCollection(
26 mockSyncCollectionOptions<Person>({
27 id: `test-persons-2`,
28 getKey: (person: Person) => person.id,
29 initialData: [],
30 }),
31 )
32​
33 const { result } = renderHook(() => {
34 return useLiveQuery((q) =>
35 q
36 .from({ collection })
37 .where(({ collection: c }) => eq(c.id, `3`))
38 .findOne(),
39 )
40 })
41​
42 expectTypeOf(result.current.data).toMatchTypeOf<
43 OutputWithVirtual<Person> | undefined
44 >()
45 })
46​
47 it(`should type findOne config object to return a single row`, () => {
48 const collection = createCollection(
49 mockSyncCollectionOptions<Person>({
50 id: `test-persons-2`,
51 getKey: (person: Person) => person.id,
52 initialData: [],
53 }),
54 )
55​
56 const { result } = renderHook(() => {
57 return useLiveQuery({
58 query: (q) =>
59 q
60 .from({ collection })
61 .where(({ collection: c }) => eq(c.id, `3`))
62 .findOne(),
63 })
64 })
65​
66 expectTypeOf(result.current.data).toMatchTypeOf<
67 OutputWithVirtual<Person> | undefined
68 >()
69 })
70​
71 it(`should type findOne collection using liveQueryCollectionOptions to return a single row`, () => {
72 const collection = createCollection(
73 mockSyncCollectionOptions<Person>({
74 id: `test-persons-2`,
75 getKey: (person: Person) => person.id,
76 initialData: [],
77 }),
78 )
79​
80 const options = liveQueryCollectionOptions({
81 query: (q) =>
82 q
83 .from({ collection })
84 .where(({ collection: c }) => eq(c.id, `3`))
85 .findOne(),
86 })
87​
88 const liveQueryCollection = createCollection(options)
89​
90 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
91​
92 const { result } = renderHook(() => {
93 return useLiveQuery(liveQueryCollection)
94 })
95​
96 expectTypeOf(result.current.data).toMatchTypeOf<
97 OutputWithVirtual<Person> | undefined
98 >()
99 })
100​
101 it(`should type findOne collection using createLiveQueryCollection to return a single row`, () => {
102 const collection = createCollection(
103 mockSyncCollectionOptions<Person>({
104 id: `test-persons-2`,
105 getKey: (person: Person) => person.id,
106 initialData: [],
107 }),
108 )
109​
110 const liveQueryCollection = createLiveQueryCollection({
111 query: (q) =>
112 q
113 .from({ collection })
114 .where(({ collection: c }) => eq(c.id, `3`))
115 .findOne(),
116 })
117​
118 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
119​
120 const { result } = renderHook(() => {
121 return useLiveQuery(liveQueryCollection)
122 })
123​
124 expectTypeOf(result.current.data).toMatchTypeOf<
125 OutputWithVirtual<Person> | undefined
126 >()
127 })
128})