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

inject-queries.test-d.ts

359 lines10.7 KB

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