Open History to see the last commit
1import { createKey } from '@tanstack/query-test-utils'
2import { afterEach, beforeEach, describe, expectTypeOf, it } from 'vitest'
3import { QueryClient } from '..'
4import type {
5 CancelOptions,
6 DefaultError,
7 Query,
8 QueryKey,
9 QueryObserver,
10 QueryOptions,
11 QueryState,
12} from '..'
13import type {
14 Action,
15 FetchContext,
16 FetchDirection,
17 FetchMeta,
18 FetchOptions,
19 QueryBehavior,
20 fetchState,
21} from '../query'
22β
23class CustomError extends Error {
24 override name = 'CustomError' as const
25}
26β
27describe('query', () => {
28 let queryClient: QueryClient
29β
30 beforeEach(() => {
31 queryClient = new QueryClient()
32 queryClient.mount()
33 })
34β
35 afterEach(() => {
36 queryClient.clear()
37 })
38β
39 describe('QueryState', () => {
40 it('should declare every field with its own type', () => {
41 expectTypeOf<QueryState<{ value: string }, CustomError>>().toEqualTypeOf<{
42 data: { value: string } | undefined
43 dataUpdateCount: number
44 dataUpdatedAt: number
45 error: CustomError | null
46 errorUpdateCount: number
47 errorUpdatedAt: number
48 fetchFailureCount: number
49 fetchFailureReason: CustomError | null
50 fetchMeta: FetchMeta | null
51 isInvalidated: boolean
52 status: 'pending' | 'error' | 'success'
53 fetchStatus: 'fetching' | 'paused' | 'idle'
54 }>()
55 })
56β
57 it('should default its data to unknown and its error to the default error', () => {
58 expectTypeOf<QueryState>().toEqualTypeOf<
59 QueryState<unknown, DefaultError>
60 >()
61 expectTypeOf<QueryState['data']>().toEqualTypeOf<unknown>()
62 expectTypeOf<QueryState['error']>().toEqualTypeOf<DefaultError | null>()
63 })
64β
65 it('should keep every field required and writable', () => {
66 type State = QueryState<{ value: string }, CustomError>
67 type OptionalKeys = {
68 [K in keyof State]-?: {} extends Pick<State, K> ? K : never
69 }[keyof State]
70β
71 expectTypeOf<[OptionalKeys]>().toEqualTypeOf<[never]>()
72 expectTypeOf<State>().toEqualTypeOf<{
73 -readonly [K in keyof State]: State[K]
74 }>()
75 })
76 })
77β
78 describe('FetchMeta', () => {
79 it('should carry an optional fetchMore direction', () => {
80 expectTypeOf<FetchMeta>().toEqualTypeOf<{
81 fetchMore?: { direction: 'forward' | 'backward' }
82 }>()
83 })
84 })
85β
86 describe('FetchDirection', () => {
87 it('should be the forward and backward literals', () => {
88 expectTypeOf<FetchDirection>().toEqualTypeOf<'forward' | 'backward'>()
89 })
90 })
91β
92 describe('FetchOptions', () => {
93 it('should type its initialPromise from its data type', () => {
94 expectTypeOf<FetchOptions<{ value: string }>>().toEqualTypeOf<{
95 cancelRefetch?: boolean
96 meta?: FetchMeta
97 initialPromise?: Promise<{ value: string }>
98 }>()
99 })
100β
101 it('should default its data type to unknown', () => {
102 expectTypeOf<FetchOptions['initialPromise']>().toEqualTypeOf<
103 Promise<unknown> | undefined
104 >()
105 })
106 })
107β
108 describe('QueryBehavior', () => {
109 it('should type the fetch context given to onFetch', () => {
110 const behavior: QueryBehavior<
111 { value: string },
112 CustomError,
113 { value: string },
114 readonly ['a', 1]
115 > = {
116 onFetch: (context, query) => {
117 expectTypeOf(context).toEqualTypeOf<
118 FetchContext<
119 { value: string },
120 CustomError,
121 { value: string },
122 readonly ['a', 1]
123 >
124 >()
125 expectTypeOf(query).toEqualTypeOf<Query>()
126 },
127 }
128β
129 expectTypeOf(behavior.onFetch).returns.toEqualTypeOf<void>()
130 })
131β
132 it('should declare onFetch as its only, required member', () => {
133 expectTypeOf<keyof QueryBehavior>().toEqualTypeOf<'onFetch'>()
134 expectTypeOf<QueryBehavior>().toEqualTypeOf<{
135 onFetch: (
136 context: FetchContext<unknown, DefaultError, unknown, QueryKey>,
137 query: Query,
138 ) => void
139 }>()
140 })
141β
142 it('should default its type parameters', () => {
143 expectTypeOf<QueryBehavior>().toEqualTypeOf<
144 QueryBehavior<unknown, DefaultError, unknown, QueryKey>
145 >()
146 })
147β
148 it('should default its data type to its query fn data type', () => {
149 expectTypeOf<QueryBehavior<{ value: string }>>().toEqualTypeOf<
150 QueryBehavior<
151 { value: string },
152 DefaultError,
153 { value: string },
154 QueryKey
155 >
156 >()
157 })
158 })
159β
160 describe('FetchContext', () => {
161 it('should declare exactly its own keys', () => {
162 type TestFetchContext = FetchContext<
163 { value: string },
164 CustomError,
165 { value: string },
166 readonly ['a', 1]
167 >
168β
169 expectTypeOf<keyof TestFetchContext>().toEqualTypeOf<
170 | 'fetchFn'
171 | 'fetchOptions'
172 | 'signal'
173 | 'options'
174 | 'client'
175 | 'queryKey'
176 | 'state'
177 >()
178 })
179β
180 it('should type its fetchFn, signal and client', () => {
181 type TestFetchContext = FetchContext<
182 { value: string },
183 CustomError,
184 { value: string },
185 readonly ['a', 1]
186 >
187β
188 expectTypeOf<TestFetchContext['fetchFn']>().toEqualTypeOf<
189 () => unknown | Promise<unknown>
190 >()
191 expectTypeOf<TestFetchContext['signal']>().toEqualTypeOf<AbortSignal>()
192 expectTypeOf<TestFetchContext['client']>().toEqualTypeOf<QueryClient>()
193 })
194β
195 it('should type its fetchOptions as optional and data-agnostic', () => {
196 type TestFetchContext = FetchContext<
197 { value: string },
198 CustomError,
199 { value: string },
200 readonly ['a', 1]
201 >
202β
203 // `Pick` rather than an index access: `T['k']` strips the `?` modifier,
204 // so an index access cannot tell `fetchOptions?: X` from
205 // `fetchOptions: X | undefined`.
206 expectTypeOf<Pick<TestFetchContext, 'fetchOptions'>>().toEqualTypeOf<{
207 fetchOptions?: FetchOptions<unknown>
208 }>()
209 })
210β
211 it('should carry its data and error types into its state', () => {
212 type TestFetchContext = FetchContext<
213 { value: string },
214 CustomError,
215 { value: string },
216 readonly ['a', 1]
217 >
218β
219 expectTypeOf<TestFetchContext['state']>().toEqualTypeOf<
220 QueryState<{ value: string }, CustomError>
221 >()
222 })
223β
224 it('should keep its own queryKey while widening the queryKey of its options', () => {
225 type TestFetchContext = FetchContext<
226 { value: string },
227 CustomError,
228 { value: string },
229 readonly ['a', 1]
230 >
231β
232 expectTypeOf<TestFetchContext['queryKey']>().toEqualTypeOf<
233 readonly ['a', 1]
234 >()
235 expectTypeOf<TestFetchContext['options']>().toEqualTypeOf<
236 QueryOptions<{ value: string }, CustomError, { value: string }, any>
237 >()
238 })
239 })
240β
241 describe('Action', () => {
242 it('should discriminate the failed action by its type literal', () => {
243 expectTypeOf<
244 Extract<Action<{ value: string }, CustomError>, { type: 'failed' }>
245 >().toEqualTypeOf<{
246 type: 'failed'
247 failureCount: number
248 error: CustomError
249 }>()
250 })
251β
252 it('should discriminate the fetch action by its type literal', () => {
253 expectTypeOf<
254 Extract<Action<{ value: string }, CustomError>, { type: 'fetch' }>
255 >().toEqualTypeOf<{ type: 'fetch'; meta?: FetchMeta }>()
256 })
257β
258 it('should discriminate the success action by its type literal', () => {
259 expectTypeOf<
260 Extract<Action<{ value: string }, CustomError>, { type: 'success' }>
261 >().toEqualTypeOf<{
262 data: { value: string } | undefined
263 type: 'success'
264 dataUpdatedAt?: number
265 manual?: boolean
266 }>()
267 })
268β
269 it('should discriminate the error action by its type literal', () => {
270 expectTypeOf<
271 Extract<Action<{ value: string }, CustomError>, { type: 'error' }>
272 >().toEqualTypeOf<{ type: 'error'; error: CustomError }>()
273 })
274β
275 it('should discriminate the invalidate action by its type literal', () => {
276 expectTypeOf<
277 Extract<Action<{ value: string }, CustomError>, { type: 'invalidate' }>
278 >().toEqualTypeOf<{ type: 'invalidate' }>()
279 })
280β
281 it('should discriminate the pause action by its type literal', () => {
282 expectTypeOf<
283 Extract<Action<{ value: string }, CustomError>, { type: 'pause' }>
284 >().toEqualTypeOf<{ type: 'pause' }>()
285 })
286β
287 it('should discriminate the continue action by its type literal', () => {
288 expectTypeOf<
289 Extract<Action<{ value: string }, CustomError>, { type: 'continue' }>
290 >().toEqualTypeOf<{ type: 'continue' }>()
291 })
292β
293 it('should discriminate the setState action by its type literal', () => {
294 expectTypeOf<
295 Extract<Action<{ value: string }, CustomError>, { type: 'setState' }>
296 >().toEqualTypeOf<{
297 type: 'setState'
298 state: Partial<QueryState<{ value: string }, CustomError>>
299 }>()
300 })
301β
302 it('should union exactly the eight action type literals', () => {
303 expectTypeOf<
304 Action<{ value: string }, CustomError>['type']
305 >().toEqualTypeOf<
306 | 'continue'
307 | 'error'
308 | 'failed'
309 | 'fetch'
310 | 'invalidate'
311 | 'pause'
312 | 'setState'
313 | 'success'
314 >()
315 })
316 })
317β
318 describe('fetchState', () => {
319 it('should return the fetching fields it resets', () => {
320 expectTypeOf<ReturnType<typeof fetchState>>().toEqualTypeOf<{
321 readonly fetchFailureCount: 0
322 readonly fetchFailureReason: null
323 readonly fetchStatus: 'fetching' | 'paused'
324 readonly error?: null
325 readonly status?: 'pending'
326 }>()
327 })
328 })
329β
330 describe('queryKey', () => {
331 // A literal queryKey is preserved by `queryCache.build`, which is where
332 // that inference happens and where it is asserted
333 // (queryCache.test-d.tsx > build > should preserve a literal queryKey).
334 it('should default to a readonly unknown array', () => {
335 expectTypeOf<Query['queryKey']>().toEqualTypeOf<ReadonlyArray<unknown>>()
336 })
337β
338 it('should carry the query key type parameter of its instance', () => {
339 const query = queryClient
340 .getQueryCache()
341 .build<
342 { value: string },
343 CustomError,
344 { value: string },
345 ['k', number]
346 >(queryClient, {
347 queryKey: ['k', 1],
348 queryFn: () => Promise.resolve({ value: 'data' }),
349 })
350β
351 expectTypeOf(query.queryKey).toEqualTypeOf<['k', number]>()
352 })
353 })
354β
355 describe('queryHash', () => {
356 it('should be typed as a string', () => {
357 const query = queryClient
358 .getQueryCache()
359 .build<{ value: string }, CustomError>(queryClient, {
360 queryKey: createKey(),
361 queryFn: () => Promise.resolve({ value: 'data' }),
362 })
363β
364 expectTypeOf(query.queryHash).toEqualTypeOf<string>()
365 })
366 })
367β
368 describe('state', () => {
369 it('should carry the data and error types of the query', () => {
370 const query = queryClient
371 .getQueryCache()
372 .build<{ value: string }, CustomError>(queryClient, {
373 queryKey: createKey(),
374 queryFn: () => Promise.resolve({ value: 'data' }),
375 })
376β
377 expectTypeOf(query.state).toEqualTypeOf<
378 QueryState<{ value: string }, CustomError>
379 >()
380 })
381 })
382β
383 describe('options', () => {
384 it('should carry the type parameters of the query', () => {
385 const query = queryClient
386 .getQueryCache()
387 .build<{ value: string }, CustomError>(queryClient, {
388 queryKey: createKey(),
389 queryFn: () => Promise.resolve({ value: 'data' }),
390 })
391β
392 expectTypeOf(query.options).toEqualTypeOf<
393 QueryOptions<
394 { value: string },
395 CustomError,
396 { value: string },
397 ReadonlyArray<unknown>
398 >
399 >()
400 expectTypeOf(query.options.queryKey).toEqualTypeOf<
401 ReadonlyArray<unknown> | undefined
402 >()
403 expectTypeOf<
404 Extract<typeof query.options.retry, (...args: Array<any>) => any>
405 >()
406 .parameter(1)
407 .toEqualTypeOf<CustomError>()
408 expectTypeOf(query.options.initialData).toEqualTypeOf<
409 { value: string } | (() => { value: string } | undefined) | undefined
410 >()
411 })
412β
413 it('should always be declared', () => {
414 type Query_ = Query<{ value: string }, CustomError>
415 type OptionalKeys = {
416 [K in keyof Query_]-?: {} extends Pick<Query_, K> ? K : never
417 }[keyof Query_]
418β
419 expectTypeOf<
420 'options' extends OptionalKeys ? true : false
421 >().toEqualTypeOf<false>()
422 })
423 })
424β
425 describe('meta', () => {
426 it('should be typed as the query meta or undefined', () => {
427 const query = queryClient
428 .getQueryCache()
429 .build<{ value: string }, CustomError>(queryClient, {
430 queryKey: createKey(),
431 queryFn: () => Promise.resolve({ value: 'data' }),
432 })
433β
434 expectTypeOf(query.meta).toEqualTypeOf<
435 Record<string, unknown> | undefined
436 >()
437 })
438 })
439β
440 describe('resetState', () => {
441 it('should carry the data and error types of the query', () => {
442 const query = queryClient
443 .getQueryCache()
444 .build<{ value: string }, CustomError>(queryClient, {
445 queryKey: createKey(),
446 queryFn: () => Promise.resolve({ value: 'data' }),
447 })
448β
449 expectTypeOf(query.resetState).toEqualTypeOf<
450 QueryState<{ value: string }, CustomError>
451 >()
452 })
453 })
454β
455 describe('queryType', () => {
456 it('should be the infinite literal or undefined', () => {
457 const query = queryClient
458 .getQueryCache()
459 .build<{ value: string }, CustomError>(queryClient, {
460 queryKey: createKey(),
461 queryFn: () => Promise.resolve({ value: 'data' }),
462 })
463β
464 expectTypeOf(query.queryType).toEqualTypeOf<'infinite' | undefined>()
465 })
466 })
467β
468 describe('promise', () => {
469 it('should resolve with the data type or be undefined', () => {
470 const query = queryClient
471 .getQueryCache()
472 .build<{ value: string }, CustomError>(queryClient, {
473 queryKey: createKey(),
474 queryFn: () => Promise.resolve({ value: 'data' }),
475 })
476β
477 expectTypeOf(query.promise).toEqualTypeOf<
478 Promise<{ value: string }> | undefined
479 >()
480 })
481 })
482β
483 describe('observers', () => {
484 it('should be an array', () => {
485 const query = queryClient
486 .getQueryCache()
487 .build<{ value: string }, CustomError>(queryClient, {
488 queryKey: createKey(),
489 queryFn: () => Promise.resolve({ value: 'data' }),
490 })
491β
492 expectTypeOf(query.observers).toEqualTypeOf<
493 Array<QueryObserver<any, any, any, any, any>>
494 >()
495 })
496 })
497β
498 describe('setData', () => {
499 it('should return the data type it is given', () => {
500 const query = queryClient
501 .getQueryCache()
502 .build<{ value: string }, CustomError>(queryClient, {
503 queryKey: createKey(),
504 queryFn: () => Promise.resolve({ value: 'data' }),
505 })
506β
507 expectTypeOf(query.setData).returns.toEqualTypeOf<{
508 value: string
509 }>()
510 expectTypeOf(query.setData)
511 .parameter(0)
512 .toEqualTypeOf<{ value: string }>()
513 })
514β
515 it('should require manual on the options it is given', () => {
516 const query = queryClient
517 .getQueryCache()
518 .build<{ value: string }, CustomError>(queryClient, {
519 queryKey: createKey(),
520 queryFn: () => Promise.resolve({ value: 'data' }),
521 })
522β
523 query.setData({ value: 'data' })
524 query.setData({ value: 'data' }, { manual: true, updatedAt: 1 })
525 // @ts-expect-error manual is required when options are given
526 query.setData({ value: 'data' }, { updatedAt: 1 })
527β
528 expectTypeOf(query.setData).parameters.toEqualTypeOf<
529 [
530 newData: { value: string },
531 options?: { updatedAt?: number } & { manual: boolean },
532 ]
533 >()
534 })
535β
536 it('should reject a data type other than its own', () => {
537 const query = queryClient
538 .getQueryCache()
539 .build<{ value: string }, CustomError>(queryClient, {
540 queryKey: createKey(),
541 queryFn: () => Promise.resolve({ value: 'data' }),
542 })
543β
544 // @ts-expect-error the data must be the query data type
545 query.setData('data')
546β
547 expectTypeOf(query.setData).parameter(0).toEqualTypeOf<{
548 value: string
549 }>()
550 })
551 })
552β
553 describe('setState', () => {
554 it('should accept a partial state and return void', () => {
555 const query = queryClient
556 .getQueryCache()
557 .build<{ value: string }, CustomError>(queryClient, {
558 queryKey: createKey(),
559 queryFn: () => Promise.resolve({ value: 'data' }),
560 })
561β
562 query.setState({ status: 'success' })
563β
564 expectTypeOf(query.setState)
565 .parameter(0)
566 .toEqualTypeOf<Partial<QueryState<{ value: string }, CustomError>>>()
567 expectTypeOf(query.setState).returns.toEqualTypeOf<void>()
568 })
569β
570 it('should reject a state field that is not its own type', () => {
571 const query = queryClient
572 .getQueryCache()
573 .build<{ value: string }, CustomError>(queryClient, {
574 queryKey: createKey(),
575 queryFn: () => Promise.resolve({ value: 'data' }),
576 })
577β
578 // @ts-expect-error the data must be the query data type
579 query.setState({ data: 'data' })
580β
581 expectTypeOf(query.setState)
582 .parameter(0)
583 .toHaveProperty('data')
584 .toEqualTypeOf<{ value: string } | undefined>()
585 })
586 })
587β
588 describe('cancel', () => {
589 it('should resolve with void', () => {
590 const query = queryClient
591 .getQueryCache()
592 .build<{ value: string }, CustomError>(queryClient, {
593 queryKey: createKey(),
594 queryFn: () => Promise.resolve({ value: 'data' }),
595 })
596β
597 expectTypeOf(query.cancel).returns.toEqualTypeOf<Promise<void>>()
598 })
599β
600 it('should only accept CancelOptions', () => {
601 const query = queryClient
602 .getQueryCache()
603 .build<{ value: string }, CustomError>(queryClient, {
604 queryKey: createKey(),
605 queryFn: () => Promise.resolve({ value: 'data' }),
606 })
607β
608 expectTypeOf(query.cancel).parameters.toEqualTypeOf<
609 [options?: CancelOptions]
610 >()
611 expectTypeOf<CancelOptions>().toEqualTypeOf<{
612 revert?: boolean
613 silent?: boolean
614 }>()
615 })
616 })
617β
618 describe('destroy', () => {
619 it('should take no arguments and return void', () => {
620 const query = queryClient
621 .getQueryCache()
622 .build<{ value: string }, CustomError>(queryClient, {
623 queryKey: createKey(),
624 queryFn: () => Promise.resolve({ value: 'data' }),
625 })
626β
627 expectTypeOf(query.destroy).parameters.toEqualTypeOf<[]>()
628 expectTypeOf(query.destroy).returns.toEqualTypeOf<void>()
629 })
630 })
631β
632 describe('reset', () => {
633 it('should take no arguments and return void', () => {
634 const query = queryClient
635 .getQueryCache()
636 .build<{ value: string }, CustomError>(queryClient, {
637 queryKey: createKey(),
638 queryFn: () => Promise.resolve({ value: 'data' }),
639 })
640β
641 expectTypeOf(query.reset).parameters.toEqualTypeOf<[]>()
642 expectTypeOf(query.reset).returns.toEqualTypeOf<void>()
643 })
644 })
645β
646 describe('isActive', () => {
647 it('should return a boolean', () => {
648 const query = queryClient
649 .getQueryCache()
650 .build<{ value: string }, CustomError>(queryClient, {
651 queryKey: createKey(),
652 queryFn: () => Promise.resolve({ value: 'data' }),
653 })
654β
655 expectTypeOf(query.isActive).returns.toEqualTypeOf<boolean>()
656 })
657 })
658β
659 describe('isDisabled', () => {
660 it('should return a boolean', () => {
661 const query = queryClient
662 .getQueryCache()
663 .build<{ value: string }, CustomError>(queryClient, {
664 queryKey: createKey(),
665 queryFn: () => Promise.resolve({ value: 'data' }),
666 })
667β
668 expectTypeOf(query.isDisabled).returns.toEqualTypeOf<boolean>()
669 })
670 })
671β
672 describe('isFetched', () => {
673 it('should return a boolean', () => {
674 const query = queryClient
675 .getQueryCache()
676 .build<{ value: string }, CustomError>(queryClient, {
677 queryKey: createKey(),
678 queryFn: () => Promise.resolve({ value: 'data' }),
679 })
680β
681 expectTypeOf(query.isFetched).returns.toEqualTypeOf<boolean>()
682 })
683 })
684β
685 describe('isStatic', () => {
686 it('should return a boolean', () => {
687 const query = queryClient
688 .getQueryCache()
689 .build<{ value: string }, CustomError>(queryClient, {
690 queryKey: createKey(),
691 queryFn: () => Promise.resolve({ value: 'data' }),
692 })
693β
694 expectTypeOf(query.isStatic).returns.toEqualTypeOf<boolean>()
695 })
696 })
697β
698 describe('isStale', () => {
699 it('should return a boolean', () => {
700 const query = queryClient
701 .getQueryCache()
702 .build<{ value: string }, CustomError>(queryClient, {
703 queryKey: createKey(),
704 queryFn: () => Promise.resolve({ value: 'data' }),
705 })
706β
707 expectTypeOf(query.isStale).returns.toEqualTypeOf<boolean>()
708 })
709 })
710β
711 describe('isStaleByTime', () => {
712 it('should return a boolean', () => {
713 const query = queryClient
714 .getQueryCache()
715 .build<{ value: string }, CustomError>(queryClient, {
716 queryKey: createKey(),
717 queryFn: () => Promise.resolve({ value: 'data' }),
718 })
719β
720 expectTypeOf(query.isStaleByTime).returns.toEqualTypeOf<boolean>()
721 })
722β
723 it('should only accept an optional stale time', () => {
724 const query = queryClient
725 .getQueryCache()
726 .build<{ value: string }, CustomError>(queryClient, {
727 queryKey: createKey(),
728 queryFn: () => Promise.resolve({ value: 'data' }),
729 })
730β
731 query.isStaleByTime(1000)
732 query.isStaleByTime('static')
733 query.isStaleByTime()
734 // @ts-expect-error a stale time must be a number or the static literal
735 query.isStaleByTime('soon')
736β
737 expectTypeOf(query.isStaleByTime).parameters.toEqualTypeOf<
738 [staleTime?: number | 'static']
739 >()
740 })
741 })
742β
743 describe('getObserversCount', () => {
744 it('should return a number', () => {
745 const query = queryClient
746 .getQueryCache()
747 .build<{ value: string }, CustomError>(queryClient, {
748 queryKey: createKey(),
749 queryFn: () => Promise.resolve({ value: 'data' }),
750 })
751β
752 expectTypeOf(query.getObserversCount).returns.toEqualTypeOf<number>()
753 })
754 })
755β
756 describe('invalidate', () => {
757 it('should take no arguments and return void', () => {
758 const query = queryClient
759 .getQueryCache()
760 .build<{ value: string }, CustomError>(queryClient, {
761 queryKey: createKey(),
762 queryFn: () => Promise.resolve({ value: 'data' }),
763 })
764β
765 expectTypeOf(query.invalidate).parameters.toEqualTypeOf<[]>()
766 expectTypeOf(query.invalidate).returns.toEqualTypeOf<void>()
767 })
768 })
769β
770 describe('onFocus', () => {
771 it('should take no arguments and return void', () => {
772 const query = queryClient
773 .getQueryCache()
774 .build<{ value: string }, CustomError>(queryClient, {
775 queryKey: createKey(),
776 queryFn: () => Promise.resolve({ value: 'data' }),
777 })
778β
779 expectTypeOf(query.onFocus).parameters.toEqualTypeOf<[]>()
780 expectTypeOf(query.onFocus).returns.toEqualTypeOf<void>()
781 })
782 })
783β
784 describe('onOnline', () => {
785 it('should take no arguments and return void', () => {
786 const query = queryClient
787 .getQueryCache()
788 .build<{ value: string }, CustomError>(queryClient, {
789 queryKey: createKey(),
790 queryFn: () => Promise.resolve({ value: 'data' }),
791 })
792β
793 expectTypeOf(query.onOnline).parameters.toEqualTypeOf<[]>()
794 expectTypeOf(query.onOnline).returns.toEqualTypeOf<void>()
795 })
796 })
797β
798 describe('addObserver', () => {
799 it('should take a single observer and return void', () => {
800 const query = queryClient
801 .getQueryCache()
802 .build<{ value: string }, CustomError>(queryClient, {
803 queryKey: createKey(),
804 queryFn: () => Promise.resolve({ value: 'data' }),
805 })
806β
807 expectTypeOf(query.addObserver).parameters.toEqualTypeOf<
808 [observer: QueryObserver<any, any, any, any, any>]
809 >()
810 expectTypeOf(query.addObserver).returns.toEqualTypeOf<void>()
811 })
812 })
813β
814 describe('removeObserver', () => {
815 it('should take a single observer and return void', () => {
816 const query = queryClient
817 .getQueryCache()
818 .build<{ value: string }, CustomError>(queryClient, {
819 queryKey: createKey(),
820 queryFn: () => Promise.resolve({ value: 'data' }),
821 })
822β
823 expectTypeOf(query.removeObserver).parameters.toEqualTypeOf<
824 [observer: QueryObserver<any, any, any, any, any>]
825 >()
826 expectTypeOf(query.removeObserver).returns.toEqualTypeOf<void>()
827 })
828 })
829β
830 describe('setOptions', () => {
831 it('should accept the query options or nothing', () => {
832 const query = queryClient
833 .getQueryCache()
834 .build<{ value: string }, CustomError>(queryClient, {
835 queryKey: createKey(),
836 queryFn: () => Promise.resolve({ value: 'data' }),
837 })
838β
839 query.setOptions()
840 query.setOptions({
841 queryKey: createKey(),
842 queryFn: () => Promise.resolve({ value: 'data' }),
843 })
844 query.setOptions({
845 queryKey: createKey(),
846 // @ts-expect-error the queryFn must return the query data type
847 queryFn: () => 42,
848 })
849β
850 expectTypeOf(query.setOptions).returns.toEqualTypeOf<void>()
851 })
852 })
853β
854 describe('fetch', () => {
855 it('should resolve with the query data type', () => {
856 const query = queryClient
857 .getQueryCache()
858 .build<{ value: string }, CustomError>(queryClient, {
859 queryKey: createKey(),
860 queryFn: () => Promise.resolve({ value: 'data' }),
861 })
862β
863 expectTypeOf(query.fetch).returns.toEqualTypeOf<
864 Promise<{ value: string }>
865 >()
866 })
867β
868 it('should resolve with its data type while typing its fetch options from its query fn data type', () => {
869 const query = queryClient
870 .getQueryCache()
871 .build<{ value: string }, CustomError, number>(queryClient, {
872 queryKey: createKey(),
873 queryFn: () => Promise.resolve({ value: 'data' }),
874 })
875β
876 expectTypeOf(query.fetch).returns.toEqualTypeOf<Promise<number>>()
877 expectTypeOf(query.fetch).parameters.toEqualTypeOf<
878 [
879 options?: QueryOptions<
880 { value: string },
881 CustomError,
882 number,
883 ReadonlyArray<unknown>
884 >,
885 fetchOptions?: FetchOptions<{ value: string }>,
886 ]
887 >()
888 })
889β
890 it('should accept the query options or nothing', () => {
891 const query = queryClient
892 .getQueryCache()
893 .build<{ value: string }, CustomError>(queryClient, {
894 queryKey: createKey(),
895 queryFn: () => Promise.resolve({ value: 'data' }),
896 })
897β
898 query.fetch()
899 query.fetch({
900 queryKey: createKey(),
901 queryFn: () => Promise.resolve({ value: 'data' }),
902 })
903 query.fetch({
904 queryKey: createKey(),
905 // @ts-expect-error the queryFn must return the query data type
906 queryFn: () => 42,
907 })
908β
909 expectTypeOf(query.fetch)
910 .parameter(0)
911 .toEqualTypeOf<
912 | QueryOptions<
913 { value: string },
914 CustomError,
915 { value: string },
916 ReadonlyArray<unknown>
917 >
918 | undefined
919 >()
920 })
921 })
922β
923 describe('Query', () => {
924 it('should default its type parameters', () => {
925 expectTypeOf<Query>().toEqualTypeOf<
926 Query<unknown, DefaultError, unknown, QueryKey>
927 >()
928 })
929β
930 it('should derive its data type from its query fn data type', () => {
931 expectTypeOf<Query<{ value: string }>>().toEqualTypeOf<
932 Query<{ value: string }, DefaultError, { value: string }, QueryKey>
933 >()
934 })
935β
936 it('should keep its own fields writable', () => {
937 type Fields = Pick<Query, 'queryKey' | 'queryHash' | 'options' | 'state'>
938β
939 expectTypeOf<Fields>().toEqualTypeOf<{
940 -readonly [K in keyof Fields]: Fields[K]
941 }>()
942 })
943β
944 it('should declare exactly its public members', () => {
945 expectTypeOf<keyof Query>().toEqualTypeOf<
946 | 'queryKey'
947 | 'queryHash'
948 | 'options'
949 | 'state'
950 | 'observers'
951 | 'meta'
952 | 'promise'
953 | 'queryType'
954 | 'resetState'
955 | 'gcTime'
956 | 'setOptions'
957 | 'setData'
958 | 'setState'
959 | 'cancel'
960 | 'destroy'
961 | 'reset'
962 | 'isActive'
963 | 'isDisabled'
964 | 'isFetched'
965 | 'isStatic'
966 | 'isStale'
967 | 'isStaleByTime'
968 | 'onFocus'
969 | 'onOnline'
970 | 'addObserver'
971 | 'removeObserver'
972 | 'getObserversCount'
973 | 'invalidate'
974 | 'fetch'
975 >()
976 })
977 })
978})