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

inject-queries.test-d.ts

307 lines8.6 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { injectQueries, skipToken, toResource } from '..'
3import { queryOptions } from '../query-options'
4import type { CreateQueryOptions, CreateQueryResult, OmitKeyof } from '..'
5import type { Resource, Signal } from '@angular/core'
6​
7describe('InjectQueries config object overload', () => {
8 it('infers tagged custom errors in parallel queries and combine', () => {
9 class CustomError extends Error {
10 detail = 'detail'
11 }
12 const options = queryOptions<number, CustomError>({
13 queryKey: ['tagged'],
14 queryFn: () => 1,
15 })
16 const queries = injectQueries(() => ({ queries: [options] }))
17 expectTypeOf(queries()[0].error()).toEqualTypeOf<CustomError | null>()
18 expectTypeOf(queries()[0].data()).toEqualTypeOf<number | undefined>()
19 const combined = injectQueries(() => ({
20 queries: [options],
21 combine: (results) => results[0].error,
22 }))
23 expectTypeOf(combined()).toEqualTypeOf<CustomError | null>()
24 })
25 it('preserves custom errors from annotated options without a query-key tag', () => {
26 class CustomError extends Error {
27 detail = 'detail'
28 }
29 const options: CreateQueryOptions<number, CustomError> = {
30 queryKey: ['annotated'],
31 queryFn: () => 1,
32 }
33 const queries = injectQueries(() => ({ queries: [options] }))
34 expectTypeOf(queries()[0].error()).toEqualTypeOf<CustomError | null>()
35 expectTypeOf(queries()[0].data()).toEqualTypeOf<number | undefined>()
36 const combined = injectQueries(() => ({
37 queries: [options],
38 combine: (results) => results[0].error,
39 }))
40 expectTypeOf(combined()).toEqualTypeOf<CustomError | null>()
41 })
42​
43 it('should expose Angular resource views', () => {
44 const queryResults = injectQueries(() => ({
45 queries: [
46 {
47 queryKey: ['key'],
48 queryFn: () => Promise.resolve('data'),
49 },
50 ],
51 }))
52​
53 expectTypeOf(toResource(queryResults()[0])).toMatchTypeOf<
54 Resource<string | undefined>
55 >()
56​
57 // @ts-expect-error Resources are created explicitly with toResource.
58 queryResults()[0].resource
59 })
60​
61 it('TData should always be defined when initialData is provided as an object', () => {
62 const query1 = {
63 queryKey: ['key1'],
64 queryFn: () => {
65 return {
66 wow: true,
67 }
68 },
69 initialData: {
70 wow: false,
71 },
72 }
73​
74 const query2 = {
75 queryKey: ['key2'],
76 queryFn: () => 'Query Data',
77 initialData: 'initial data',
78 }
79​
80 const query3 = {
81 queryKey: ['key2'],
82 queryFn: () => 'Query Data',
83 }
84​
85 const queryResults = injectQueries(() => ({
86 queries: [query1, query2, query3],
87 }))
88​
89 const query1Data = queryResults()[0].data()
90 const query2Data = queryResults()[1].data()
91 const query3Data = queryResults()[2].data()
92​
93 expectTypeOf(query1Data).toEqualTypeOf<{ wow: boolean }>()
94 expectTypeOf(query2Data).toEqualTypeOf<string>()
95 expectTypeOf(query3Data).toEqualTypeOf<string | undefined>()
96 })
97​
98 it('TData should be defined when passed through queryOptions', () => {
99 const options = queryOptions({
100 queryKey: ['key'],
101 queryFn: () => {
102 return {
103 wow: true,
104 }
105 },
106 initialData: {
107 wow: true,
108 },
109 })
110 const queryResults = injectQueries(() => ({ queries: [options] }))
111​
112 const data = queryResults()[0].data()
113​
114 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
115 })
116​
117 it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into injectQuery', () => {
118 const query1 = queryOptions({
119 queryKey: ['key'],
120 queryFn: () => Promise.resolve(1),
121 select: (data) => data > 1,
122 })
123​
124 const query2 = {
125 queryKey: ['key'],
126 queryFn: () => Promise.resolve(1),
127 select: (data: number) => data > 1,
128 }
129​
130 const queryResults = injectQueries(() => ({ queries: [query1, query2] }))
131 const query1Data = queryResults()[0].data()
132 const query2Data = queryResults()[1].data()
133​
134 expectTypeOf(query1Data).toEqualTypeOf<boolean | undefined>()
135 expectTypeOf(query2Data).toEqualTypeOf<boolean | undefined>()
136 })
137​
138 it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
139 const queryResults = injectQueries(() => ({
140 queries: [
141 {
142 queryKey: ['key'],
143 queryFn: () => {
144 return {
145 wow: true,
146 }
147 },
148 initialData: () => undefined as { wow: boolean } | undefined,
149 },
150 ],
151 }))
152​
153 const data = queryResults()[0].data()
154​
155 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
156 })
157​
158 describe('custom injectable', () => {
159 it('should allow custom hooks using UseQueryOptions', () => {
160 type Data = string
161​
162 const injectCustomQueries = (
163 options?: OmitKeyof<CreateQueryOptions<Data>, 'queryKey' | 'queryFn'>,
164 ) => {
165 return injectQueries(() => ({
166 queries: [
167 {
168 ...options,
169 queryKey: ['todos-key'],
170 queryFn: () => Promise.resolve('data'),
171 },
172 ],
173 }))
174 }
175​
176 const queryResults = injectCustomQueries()
177 const data = queryResults()[0].data()
178​
179 expectTypeOf(data).toEqualTypeOf<Data | undefined>()
180 })
181 })
182​
183 it('TData should have correct type when conditional skipToken is passed', () => {
184 const queryResults = injectQueries(() => ({
185 queries: [
186 {
187 queryKey: ['withSkipToken'],
188 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
189 },
190 ],
191 }))
192​
193 const firstResult = queryResults()[0]
194​
195 expectTypeOf(firstResult).toEqualTypeOf<CreateQueryResult<number, Error>>()
196 expectTypeOf(firstResult.data()).toEqualTypeOf<number | undefined>()
197 })
198​
199 it('should return correct data for dynamic queries with mixed result types', () => {
200 const Queries1 = {
201 get: () =>
202 queryOptions({
203 queryKey: ['key1'],
204 queryFn: () => Promise.resolve(1),
205 }),
206 }
207 const Queries2 = {
208 get: () =>
209 queryOptions({
210 queryKey: ['key2'],
211 queryFn: () => Promise.resolve(true),
212 }),
213 }
214​
215 const queries1List = [1, 2, 3].map(() => ({ ...Queries1.get() }))
216 const result = injectQueries(() => ({
217 queries: [...queries1List, { ...Queries2.get() }],
218 }))
219​
220 expectTypeOf(result).toBeFunction()
221 })
222})
223​
224describe('InjectQueries combine', () => {
225 it('should provide the correct type for the combine function', () => {
226 injectQueries(() => ({
227 queries: [
228 {
229 queryKey: ['key'],
230 queryFn: () => Promise.resolve(1),
231 },
232 {
233 queryKey: ['key2'],
234 queryFn: () => Promise.resolve(true),
235 },
236 ],
237 combine: (results) => {
238 expectTypeOf(results[0].data).toEqualTypeOf<number | undefined>()
239 expectTypeOf(results[0].refetch).toBeCallableWith()
240 expectTypeOf(results[1].data).toEqualTypeOf<boolean | undefined>()
241 expectTypeOf(results[1].refetch).toBeCallableWith()
242 },
243 }))
244 })
245​
246 it('should provide the correct types on the combined result with initial data', () => {
247 injectQueries(() => ({
248 queries: [
249 {
250 queryKey: ['key'],
251 queryFn: () => Promise.resolve(1),
252 initialData: 1,
253 },
254 ],
255 combine: (results) => {
256 expectTypeOf(results[0].data).toEqualTypeOf<number>()
257 expectTypeOf(results[0].refetch).toBeCallableWith()
258 },
259 }))
260 })
261​
262 it('should provide the correct result type', () => {
263 const queryResults = injectQueries(() => ({
264 queries: [
265 {
266 queryKey: ['key'],
267 queryFn: () => Promise.resolve(1),
268 },
269 {
270 queryKey: ['key2'],
271 queryFn: () => Promise.resolve(true),
272 },
273 ],
274 combine: (results) => ({
275 data: {
276 1: results[0].data,
277 2: results[1].data,
278 },
279 fn: () => {},
280 }),
281 }))
282​
283 expectTypeOf(queryResults).branded.toEqualTypeOf<
284 Signal<{
285 data: {
286 1: number | undefined
287 2: boolean | undefined
288 }
289 fn: () => void
290 }>
291 >()
292 })
293​
294 it('should provide the correct types on the result with initial data', () => {
295 const queryResults = injectQueries(() => ({
296 queries: [
297 {
298 queryKey: ['key'],
299 queryFn: () => Promise.resolve(1),
300 initialData: 1,
301 },
302 ],
303 }))
304​
305 expectTypeOf(queryResults()[0].data()).toEqualTypeOf<number>()
306 })
307})