Skip to content

Commit 0689ccc

Browse files
kevin-dpclaude
andcommitted
test: add type assertions for injectLiveQuery findOne in angular-db
Adds inject-live-query.test-d.ts with expectTypeOf assertions verifying that injectLiveQuery with findOne() types data as Signal<Person | undefined> and regular queries type data as Signal<Array<T>>. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 78734ba commit 0689ccc

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { describe, expectTypeOf, it } from 'vitest'
2+
import { createCollection } from '../../db/src/collection/index'
3+
import { mockSyncCollectionOptions } from '../../db/tests/utils'
4+
import {
5+
createLiveQueryCollection,
6+
eq,
7+
liveQueryCollectionOptions,
8+
} from '../../db/src/query/index'
9+
import { injectLiveQuery } from '../src/index'
10+
import type { SingleResult } from '../../db/src/types'
11+
12+
type Person = {
13+
id: string
14+
name: string
15+
age: number
16+
email: string
17+
isActive: boolean
18+
team: string
19+
}
20+
21+
describe(`injectLiveQuery 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-findone-angular`,
26+
getKey: (person: Person) => person.id,
27+
initialData: [],
28+
}),
29+
)
30+
31+
const { data } = injectLiveQuery((q) =>
32+
q
33+
.from({ collection })
34+
.where(({ collection: c }) => eq(c.id, `3`))
35+
.findOne(),
36+
)
37+
38+
// findOne returns a single result or undefined
39+
expectTypeOf(data()).toEqualTypeOf<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+
// findOne returns a single result or undefined
61+
expectTypeOf(data()).toEqualTypeOf<Person | undefined>()
62+
})
63+
64+
it(`should type findOne collection using liveQueryCollectionOptions to return a single row`, () => {
65+
const collection = createCollection(
66+
mockSyncCollectionOptions<Person>({
67+
id: `test-persons-findone-options-angular`,
68+
getKey: (person: Person) => person.id,
69+
initialData: [],
70+
}),
71+
)
72+
73+
const options = liveQueryCollectionOptions({
74+
query: (q) =>
75+
q
76+
.from({ collection })
77+
.where(({ collection: c }) => eq(c.id, `3`))
78+
.findOne(),
79+
})
80+
81+
const liveQueryCollection = createCollection(options)
82+
83+
expectTypeOf(liveQueryCollection).toExtend<SingleResult>()
84+
85+
const { data } = injectLiveQuery(liveQueryCollection)
86+
87+
// findOne returns a single result or undefined
88+
expectTypeOf(data()).toEqualTypeOf<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+
// findOne returns a single result or undefined
113+
expectTypeOf(data()).toEqualTypeOf<Person | undefined>()
114+
})
115+
116+
it(`should type regular query to return an array`, () => {
117+
const collection = createCollection(
118+
mockSyncCollectionOptions<Person>({
119+
id: `test-persons-array-angular`,
120+
getKey: (person: Person) => person.id,
121+
initialData: [],
122+
}),
123+
)
124+
125+
const { data } = injectLiveQuery((q) =>
126+
q
127+
.from({ collection })
128+
.where(({ collection: c }) => eq(c.isActive, true))
129+
.select(({ collection: c }) => ({
130+
id: c.id,
131+
name: c.name,
132+
})),
133+
)
134+
135+
// Regular queries should return an array
136+
expectTypeOf(data()).toEqualTypeOf<Array<{ id: string; name: string }>>()
137+
})
138+
})

0 commit comments

Comments
 (0)