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

useLiveQuery.test-d.ts

215 lines6.6 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { createCollection } from '../../db/src/collection/index'
3import { mockSyncCollectionOptions } from '../../db/tests/utils'
4import {
5 createLiveQueryCollection,
6 eq,
7 liveQueryCollectionOptions,
8} from '../../db/src/query/index'
9import { useLiveQuery } from '../src/useLiveQuery'
10import type { ConditionalUseLiveQueryReturn } from '../src/index'
11import type { Prettify } from '../../db/src/query/index'
12import type {
13 Collection,
14 CollectionStatus,
15 InitialQueryBuilder,
16 QueryBuilder,
17} from '@tanstack/db'
18import type { OutputWithVirtual } from '../../db/tests/utils'
19import type { SingleResult } from '../../db/src/types'
20​
21type Person = {
22 id: string
23 name: string
24 age: number
25 email: string
26 isActive: boolean
27 team: string
29​
30describe(`useLiveQuery type assertions`, () => {
31 it(`should type findOne query builder to return a single row`, () => {
32 const collection = createCollection(
33 mockSyncCollectionOptions<Person>({
34 id: `test-persons-findone-vue`,
35 getKey: (person: Person) => person.id,
36 initialData: [],
37 }),
38 )
39​
40 const { data } = useLiveQuery((q) =>
41 q
42 .from({ collection })
43 .where(({ collection: c }) => eq(c.id, `3`))
44 .findOne(),
45 )
46​
47 // BUG: Currently returns ComputedRef<Array<Person>> but should be ComputedRef<Person | undefined>
48 expectTypeOf(data.value).toMatchTypeOf<
49 OutputWithVirtual<Person> | undefined
50 >()
51 })
52​
53 it(`should type findOne config object to return a single row`, () => {
54 const collection = createCollection(
55 mockSyncCollectionOptions<Person>({
56 id: `test-persons-findone-config-vue`,
57 getKey: (person: Person) => person.id,
58 initialData: [],
59 }),
60 )
61​
62 const { data } = useLiveQuery({
63 query: (q) =>
64 q
65 .from({ collection })
66 .where(({ collection: c }) => eq(c.id, `3`))
67 .findOne(),
68 })
69​
70 // BUG: Currently returns ComputedRef<Array<Person>> but should be ComputedRef<Person | undefined>
71 expectTypeOf(data.value).toMatchTypeOf<
72 OutputWithVirtual<Person> | undefined
73 >()
74 })
75​
76 it(`should type findOne collection using liveQueryCollectionOptions to return a single row`, () => {
77 const collection = createCollection(
78 mockSyncCollectionOptions<Person>({
79 id: `test-persons-findone-options-vue`,
80 getKey: (person: Person) => person.id,
81 initialData: [],
82 }),
83 )
84​
85 const options = liveQueryCollectionOptions({
86 query: (q) =>
87 q
88 .from({ collection })
89 .where(({ collection: c }) => eq(c.id, `3`))
90 .findOne(),
91 })
92​
93 const liveQueryCollection = createCollection(options)
94​
95 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
96​
97 const { data } = useLiveQuery(liveQueryCollection)
98​
99 // BUG: Currently returns ComputedRef<Array<Person>> but should be ComputedRef<Person | undefined>
100 expectTypeOf(data.value).toMatchTypeOf<
101 OutputWithVirtual<Person> | undefined
102 >()
103 })
104​
105 it(`should type findOne collection using createLiveQueryCollection to return a single row`, () => {
106 const collection = createCollection(
107 mockSyncCollectionOptions<Person>({
108 id: `test-persons-findone-create-vue`,
109 getKey: (person: Person) => person.id,
110 initialData: [],
111 }),
112 )
113​
114 const liveQueryCollection = createLiveQueryCollection({
115 query: (q) =>
116 q
117 .from({ collection })
118 .where(({ collection: c }) => eq(c.id, `3`))
119 .findOne(),
120 })
121​
122 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
123​
124 const { data } = useLiveQuery(liveQueryCollection)
125​
126 // BUG: Currently returns ComputedRef<Array<Person>> but should be ComputedRef<Person | undefined>
127 expectTypeOf(data.value).toMatchTypeOf<
128 OutputWithVirtual<Person> | undefined
129 >()
130 })
131​
132 it(`should type regular query to return an array`, () => {
133 const collection = createCollection(
134 mockSyncCollectionOptions<Person>({
135 id: `test-persons-array-vue`,
136 getKey: (person: Person) => person.id,
137 initialData: [],
138 }),
139 )
140​
141 const { data } = useLiveQuery((q) =>
142 q
143 .from({ collection })
144 .where(({ collection: c }) => eq(c.isActive, true))
145 .select(({ collection: c }) => ({
146 id: c.id,
147 name: c.name,
148 })),
149 )
150​
151 // Regular queries should return an array
152 expectTypeOf(data.value).toMatchTypeOf<
153 Array<OutputWithVirtual<{ id: string; name: string }>>
154 >()
155 })
156​
157 it(`types disabled-capable callbacks from their empty reactive runtime`, () => {
158 const collection = createCollection(
159 mockSyncCollectionOptions<Person>({
160 id: `test-conditional-vue`,
161 getKey: (person: Person) => person.id,
162 initialData: [],
163 }),
164 )
165 const enabled = null as unknown as boolean
166​
167 // Compile-time observation cut: the public refs returned by the real
168 // `useLiveQuery` hook; the preload error proves the live-query Collection
169 // is absent while disabled.
170 const result = useLiveQuery((q) =>
171 enabled ? q.from({ collection }) : null,
172 )
173​
174 expectTypeOf(result.data.value).toEqualTypeOf<
175 Array<Prettify<OutputWithVirtual<Person>>>
176 >()
177 expectTypeOf(result.collection.value).toEqualTypeOf<Collection<
178 Prettify<OutputWithVirtual<Person>>,
179 string | number,
180 {}
181 > | null>()
182 expectTypeOf(result.status.value).toEqualTypeOf<
183 CollectionStatus | `disabled`
184 >()
185​
186 // @ts-expect-error Disabled callbacks expose a null collection until enabled.
187 result.collection.value.preload()
188 })
189​
190 it(`types conditional findOne data with its empty disabled representation`, () => {
191 const collection = createCollection(
192 mockSyncCollectionOptions<Person>({
193 id: `test-conditional-find-one-vue`,
194 getKey: (person: Person) => person.id,
195 initialData: [],
196 }),
197 )
198 const enabled = null as unknown as boolean
199 const build = (q: InitialQueryBuilder) => q.from({ collection }).findOne()
200 type QueryContext =
201 ReturnType<typeof build> extends QueryBuilder<infer TContext>
202 ? TContext
203 : never
204​
205 // The exact public result combines enabled `findOne` cardinality with the
206 // empty-reactive disabled value. A paired framework test owns transitions.
207 const result = useLiveQuery((q) => (enabled ? build(q) : null))
208 const annotated: ConditionalUseLiveQueryReturn<QueryContext> = result
209​
210 expectTypeOf(annotated).toEqualTypeOf<typeof result>()
211 expectTypeOf(result.data.value).toEqualTypeOf<
212 Prettify<OutputWithVirtual<Person>> | undefined | []
213 >()
214 })
215})