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 { useLiveQuery } from '../src/useLiveQuery'
10import type { OutputWithVirtual } from '../../db/tests/utils'
11import type { SingleResult } from '../../db/src/types'
12
13type Person = {
14 id: string
15 name: string
16 age: number
17 email: string
18 isActive: boolean
19 team: string
20}
21
22describe(`useLiveQuery type assertions`, () => {
23 it(`should type findOne query builder to return a single row`, () => {
24 const collection = createCollection(
25 mockSyncCollectionOptions<Person>({
26 id: `test-persons-findone-vue`,
27 getKey: (person: Person) => person.id,
28 initialData: [],
29 }),
30 )
31
32 const { data } = useLiveQuery((q) =>
33 q
34 .from({ collection })
35 .where(({ collection: c }) => eq(c.id, `3`))
36 .findOne(),
37 )
38
39 // BUG: Currently returns ComputedRef<Array<Person>> but should be ComputedRef<Person | undefined>
40 expectTypeOf(data.value).toMatchTypeOf<
41 OutputWithVirtual<Person> | undefined
42 >()
43 })
44
45 it(`should type findOne config object to return a single row`, () => {
46 const collection = createCollection(
47 mockSyncCollectionOptions<Person>({
48 id: `test-persons-findone-config-vue`,
49 getKey: (person: Person) => person.id,
50 initialData: [],
51 }),
52 )
53
54 const { data } = useLiveQuery({
55 query: (q) =>
56 q
57 .from({ collection })
58 .where(({ collection: c }) => eq(c.id, `3`))
59 .findOne(),
60 })
61
62 // BUG: Currently returns ComputedRef<Array<Person>> but should be ComputedRef<Person | undefined>
63 expectTypeOf(data.value).toMatchTypeOf<
64 OutputWithVirtual<Person> | undefined
65 >()
66 })
67
68 it(`should type findOne collection using liveQueryCollectionOptions to return a single row`, () => {
69 const collection = createCollection(
70 mockSyncCollectionOptions<Person>({
71 id: `test-persons-findone-options-vue`,
72 getKey: (person: Person) => person.id,
73 initialData: [],
74 }),
75 )
76
77 const options = liveQueryCollectionOptions({
78 query: (q) =>
79 q
80 .from({ collection })
81 .where(({ collection: c }) => eq(c.id, `3`))
82 .findOne(),
83 })
84
85 const liveQueryCollection = createCollection(options)
86
87 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
88
89 const { data } = useLiveQuery(liveQueryCollection)
90
91 // BUG: Currently returns ComputedRef<Array<Person>> but should be ComputedRef<Person | undefined>
92 expectTypeOf(data.value).toMatchTypeOf<
93 OutputWithVirtual<Person> | undefined
94 >()
95 })
96
97 it(`should type findOne collection using createLiveQueryCollection to return a single row`, () => {
98 const collection = createCollection(
99 mockSyncCollectionOptions<Person>({
100 id: `test-persons-findone-create-vue`,
101 getKey: (person: Person) => person.id,
102 initialData: [],
103 }),
104 )
105
106 const liveQueryCollection = createLiveQueryCollection({
107 query: (q) =>
108 q
109 .from({ collection })
110 .where(({ collection: c }) => eq(c.id, `3`))
111 .findOne(),
112 })
113
114 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
115
116 const { data } = useLiveQuery(liveQueryCollection)
117
118 // BUG: Currently returns ComputedRef<Array<Person>> but should be ComputedRef<Person | undefined>
119 expectTypeOf(data.value).toMatchTypeOf<
120 OutputWithVirtual<Person> | undefined
121 >()
122 })
123
124 it(`should type regular query to return an array`, () => {
125 const collection = createCollection(
126 mockSyncCollectionOptions<Person>({
127 id: `test-persons-array-vue`,
128 getKey: (person: Person) => person.id,
129 initialData: [],
130 }),
131 )
132
133 const { data } = useLiveQuery((q) =>
134 q
135 .from({ collection })
136 .where(({ collection: c }) => eq(c.isActive, true))
137 .select(({ collection: c }) => ({
138 id: c.id,
139 name: c.name,
140 })),
141 )
142
143 // Regular queries should return an array
144 expectTypeOf(data.value).toMatchTypeOf<
145 Array<OutputWithVirtual<{ id: string; name: string }>>
146 >()
147 })
148})