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

useLiveQuery.test-d.tsx

146 lines4.4 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { renderHook } from '@solidjs/testing-library'
3import { createCollection } from '../../db/src/collection/index'
4import { mockSyncCollectionOptions } from '../../db/tests/utils'
5import { createLiveQueryCollection, eq } from '../../db/src/query/index'
6import { useLiveQuery } from '../src/useLiveQuery'
7import type { Prettify } from '../../db/src/query/index'
8import type { Collection, CollectionStatus } from '@tanstack/db'
9import type { OutputWithVirtual } from '../../db/tests/utils'
10import type { SingleResult } from '../../db/src/types'
11​
12type Person = {
13 id: string
14 name: string
15 age: number
16 email: string
17 isActive: boolean
18 team: string
20​
21describe(`useLiveQuery type assertions`, () => {
22 it(`should type findOne query builder to return a single row`, () => {
23 const collection = createCollection(
24 mockSyncCollectionOptions<Person>({
25 id: `test-persons-2`,
26 getKey: (person: Person) => person.id,
27 initialData: [],
28 }),
29 )
30​
31 const rendered = renderHook(() => {
32 return useLiveQuery((q) =>
33 q
34 .from({ collection })
35 .where(({ collection: c }) => eq(c.id, `3`))
36 .findOne(),
37 )
38 })
39​
40 expectTypeOf(rendered.result()).toMatchTypeOf<
41 OutputWithVirtual<Person> | undefined
42 >()
43 })
44​
45 it(`should type findOne collection using createLiveQueryCollection to return a single row`, () => {
46 const collection = createCollection(
47 mockSyncCollectionOptions<Person>({
48 id: `test-persons-2`,
49 getKey: (person: Person) => person.id,
50 initialData: [],
51 }),
52 )
53​
54 const liveQueryCollection = createLiveQueryCollection({
55 query: (q) =>
56 q
57 .from({ collection })
58 .where(({ collection: c }) => eq(c.id, `3`))
59 .findOne(),
60 })
61​
62 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
63​
64 const rendered = renderHook(() => {
65 return useLiveQuery(() => liveQueryCollection)
66 })
67​
68 expectTypeOf(rendered.result()).toMatchTypeOf<
69 OutputWithVirtual<Person> | undefined
70 >()
71 })
72​
73 it(`should type non-findOne queries to return an array`, () => {
74 const collection = createCollection(
75 mockSyncCollectionOptions<Person>({
76 id: `test-persons-2`,
77 getKey: (person: Person) => person.id,
78 initialData: [],
79 }),
80 )
81​
82 const rendered = renderHook(() => {
83 return useLiveQuery((q) => q.from({ collection }))
84 })
85​
86 expectTypeOf(rendered.result()).toMatchTypeOf<
87 Array<OutputWithVirtual<Person>>
88 >()
89 })
90​
91 it(`types disabled callbacks from their empty reactive runtime`, () => {
92 const collection = createCollection(
93 mockSyncCollectionOptions<Person>({
94 id: `test-conditional-solid`,
95 getKey: (person: Person) => person.id,
96 initialData: [],
97 }),
98 )
99 const enabled = null as unknown as boolean
100​
101 // Compile-time observation cut: the public accessors returned by the real
102 // `useLiveQuery` hook; the preload error proves the live-query Collection
103 // is absent while disabled.
104 const rendered = renderHook(() =>
105 useLiveQuery((q) => (enabled ? q.from({ collection }) : null)),
106 )
107​
108 expectTypeOf(rendered.result()).toEqualTypeOf<
109 Array<Prettify<OutputWithVirtual<Person>>>
110 >()
111 expectTypeOf(rendered.result.collection).toEqualTypeOf<Collection<
112 Prettify<OutputWithVirtual<Person>>,
113 string | number,
114 {}
115 > | null>()
116 expectTypeOf(rendered.result.status).toEqualTypeOf<
117 CollectionStatus | `disabled`
118 >()
119​
120 // @ts-expect-error Disabled callbacks expose a null collection until enabled.
121 rendered.result.collection.preload()
122 })
123​
124 it(`types conditional findOne data with its empty disabled representation`, () => {
125 const collection = createCollection(
126 mockSyncCollectionOptions<Person>({
127 id: `test-conditional-find-one-solid`,
128 getKey: (person: Person) => person.id,
129 initialData: [],
130 }),
131 )
132 const enabled = null as unknown as boolean
133​
134 // The exact public result combines enabled `findOne` cardinality with the
135 // empty-reactive disabled value. A paired framework test owns transitions.
136 const rendered = renderHook(() =>
137 useLiveQuery((q) =>
138 enabled ? q.from({ collection }).findOne() : undefined,
139 ),
140 )
141​
142 expectTypeOf(rendered.result()).toEqualTypeOf<
143 Prettify<OutputWithVirtual<Person>> | undefined | []
144 >()
145 })
146})