Skip to content

Commit 73258c6

Browse files
authored
chore(angular-query): add type tests for injectQuery (#7947)
1 parent d5a0342 commit 73258c6

File tree

1 file changed

+119
-2
lines changed

1 file changed

+119
-2
lines changed

packages/angular-query-experimental/src/__tests__/inject-query.test-d.ts

+119-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,125 @@
1-
import { describe, expectTypeOf } from 'vitest'
2-
import { injectQuery } from '..'
1+
import { describe, expectTypeOf, it } from 'vitest'
2+
import { injectQuery, queryOptions } from '..'
33
import { simpleFetcher } from './test-utils'
44
import type { Signal } from '@angular/core'
55

6+
describe('initialData', () => {
7+
describe('Config object overload', () => {
8+
it('TData should always be defined when initialData is provided as an object', () => {
9+
const { data } = injectQuery(() => ({
10+
queryKey: ['key'],
11+
queryFn: () => ({ wow: true }),
12+
initialData: { wow: true },
13+
}))
14+
15+
expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
16+
})
17+
18+
it('TData should be defined when passed through queryOptions', () => {
19+
const options = () =>
20+
queryOptions({
21+
queryKey: ['key'],
22+
queryFn: () => {
23+
return {
24+
wow: true,
25+
}
26+
},
27+
initialData: {
28+
wow: true,
29+
},
30+
})
31+
const { data } = injectQuery(options)
32+
33+
expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
34+
})
35+
36+
it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
37+
const options = queryOptions({
38+
queryKey: ['key'],
39+
queryFn: () => Promise.resolve(1),
40+
})
41+
42+
const query = injectQuery(() => ({
43+
...options,
44+
select: (data) => data > 1,
45+
}))
46+
47+
expectTypeOf(query.data).toEqualTypeOf<Signal<boolean | undefined>>()
48+
})
49+
50+
it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
51+
const { data } = injectQuery(() => ({
52+
queryKey: ['key'],
53+
queryFn: () => {
54+
return {
55+
wow: true,
56+
}
57+
},
58+
initialData: () => ({
59+
wow: true,
60+
}),
61+
}))
62+
63+
expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
64+
})
65+
66+
it('TData should have undefined in the union when initialData is NOT provided', () => {
67+
const { data } = injectQuery(() => ({
68+
queryKey: ['key'],
69+
queryFn: () => {
70+
return {
71+
wow: true,
72+
}
73+
},
74+
}))
75+
76+
expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
77+
})
78+
79+
it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
80+
const { data } = injectQuery(() => ({
81+
queryKey: ['key'],
82+
queryFn: () => {
83+
return {
84+
wow: true,
85+
}
86+
},
87+
initialData: () => undefined as { wow: boolean } | undefined,
88+
}))
89+
90+
expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
91+
})
92+
93+
it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
94+
const query = injectQuery(() => ({
95+
queryKey: ['key'],
96+
queryFn: () => {
97+
return {
98+
wow: true,
99+
}
100+
},
101+
initialData: () => undefined as { wow: boolean } | undefined,
102+
}))
103+
104+
if (query.isSuccess()) {
105+
expectTypeOf(query.data).toEqualTypeOf<Signal<{ wow: boolean }>>()
106+
}
107+
})
108+
})
109+
110+
describe('structuralSharing', () => {
111+
it('should restrict to same types', () => {
112+
injectQuery(() => ({
113+
queryKey: ['key'],
114+
queryFn: () => 5,
115+
structuralSharing: (_oldData, newData) => {
116+
return newData
117+
},
118+
}))
119+
})
120+
})
121+
})
122+
6123
describe('Discriminated union return type', () => {
7124
test('data should be possibly undefined by default', () => {
8125
const query = injectQuery(() => ({

0 commit comments

Comments
 (0)