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