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 { 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(`injectLiveQuery 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-angular`,
27 getKey: (person: Person) => person.id,
28 initialData: [],
29 }),
30 )
31
32 const { data } = injectLiveQuery((q) =>
33 q
34 .from({ collection })
35 .where(({ collection: c }) => eq(c.id, `3`))
36 .findOne(),
37 )
38
39 expectTypeOf(data()).toMatchTypeOf<OutputWithVirtual<Person> | undefined>()
40 })
41
42 it(`should type findOne config object to return a single row`, () => {
43 const collection = createCollection(
44 mockSyncCollectionOptions<Person>({
45 id: `test-persons-findone-config-angular`,
46 getKey: (person: Person) => person.id,
47 initialData: [],
48 }),
49 )
50
51 const { data } = injectLiveQuery({
52 params: () => ({ id: `3` }),
53 query: ({ params, q }) =>
54 q
55 .from({ collection })
56 .where(({ collection: c }) => eq(c.id, params.id))
57 .findOne(),
58 })
59
60 expectTypeOf(data()).toMatchTypeOf<OutputWithVirtual<Person> | undefined>()
61 })
62
63 it(`should type findOne collection using liveQueryCollectionOptions to return a single row`, () => {
64 const collection = createCollection(
65 mockSyncCollectionOptions<Person>({
66 id: `test-persons-findone-options-angular`,
67 getKey: (person: Person) => person.id,
68 initialData: [],
69 }),
70 )
71
72 const options = liveQueryCollectionOptions({
73 query: (q) =>
74 q
75 .from({ collection })
76 .where(({ collection: c }) => eq(c.id, `3`))
77 .findOne(),
78 })
79
80 const liveQueryCollection = createCollection(options)
81
82 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
83
84 const { data } = injectLiveQuery(liveQueryCollection)
85
86 expectTypeOf(data()).toMatchTypeOf<OutputWithVirtual<Person> | undefined>()
87 })
88
89 it(`should type findOne collection using createLiveQueryCollection to return a single row`, () => {
90 const collection = createCollection(
91 mockSyncCollectionOptions<Person>({
92 id: `test-persons-findone-create-angular`,
93 getKey: (person: Person) => person.id,
94 initialData: [],
95 }),
96 )
97
98 const liveQueryCollection = createLiveQueryCollection({
99 query: (q) =>
100 q
101 .from({ collection })
102 .where(({ collection: c }) => eq(c.id, `3`))
103 .findOne(),
104 })
105
106 expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
107
108 const { data } = injectLiveQuery(liveQueryCollection)
109
110 expectTypeOf(data()).toMatchTypeOf<OutputWithVirtual<Person> | undefined>()
111 })
112
113 it(`should type regular query to return an array`, () => {
114 const collection = createCollection(
115 mockSyncCollectionOptions<Person>({
116 id: `test-persons-array-angular`,
117 getKey: (person: Person) => person.id,
118 initialData: [],
119 }),
120 )
121
122 const { data } = injectLiveQuery((q) =>
123 q
124 .from({ collection })
125 .where(({ collection: c }) => eq(c.isActive, true))
126 .select(({ collection: c }) => ({
127 id: c.id,
128 name: c.name,
129 })),
130 )
131
132 expectTypeOf(data()).toMatchTypeOf<
133 Array<OutputWithVirtual<{ id: string; name: string }>>
134 >()
135 })
136})