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