Open History to see the last commit
1import { afterEach, beforeEach, describe, expectTypeOf, it } from 'vitest'
2import { queryKey } from '@tanstack/query-test-utils'
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: queryKey(),
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: queryKey(),
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: queryKey(),
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<Extract<typeof query.options.retry, Function>>()
404 .parameter(1)
405 .toEqualTypeOf<CustomError>()
406 expectTypeOf(query.options.initialData).toEqualTypeOf<
407 { value: string } | (() => { value: string } | undefined) | undefined
408 >()
409 })
410β
411 it('should always be declared', () => {
412 type Query_ = Query<{ value: string }, CustomError>
413 type OptionalKeys = {
414 [K in keyof Query_]-?: {} extends Pick<Query_, K> ? K : never
415 }[keyof Query_]
416β
417 expectTypeOf<
418 'options' extends OptionalKeys ? true : false
419 >().toEqualTypeOf<false>()
420 })
421 })
422β
423 describe('meta', () => {
424 it('should be typed as the query meta or undefined', () => {
425 const query = queryClient
426 .getQueryCache()
427 .build<{ value: string }, CustomError>(queryClient, {
428 queryKey: queryKey(),
429 queryFn: () => Promise.resolve({ value: 'data' }),
430 })
431β
432 expectTypeOf(query.meta).toEqualTypeOf<
433 Record<string, unknown> | undefined
434 >()
435 })
436 })
437β
438 describe('resetState', () => {
439 it('should carry the data and error types of the query', () => {
440 const query = queryClient
441 .getQueryCache()
442 .build<{ value: string }, CustomError>(queryClient, {
443 queryKey: queryKey(),
444 queryFn: () => Promise.resolve({ value: 'data' }),
445 })
446β
447 expectTypeOf(query.resetState).toEqualTypeOf<
448 QueryState<{ value: string }, CustomError>
449 >()
450 })
451 })
452β
453 describe('queryType', () => {
454 it('should be the infinite literal or undefined', () => {
455 const query = queryClient
456 .getQueryCache()
457 .build<{ value: string }, CustomError>(queryClient, {
458 queryKey: queryKey(),
459 queryFn: () => Promise.resolve({ value: 'data' }),
460 })
461β
462 expectTypeOf(query.queryType).toEqualTypeOf<'infinite' | undefined>()
463 })
464 })
465β
466 describe('promise', () => {
467 it('should resolve with the data type or be undefined', () => {
468 const query = queryClient
469 .getQueryCache()
470 .build<{ value: string }, CustomError>(queryClient, {
471 queryKey: queryKey(),
472 queryFn: () => Promise.resolve({ value: 'data' }),
473 })
474β
475 expectTypeOf(query.promise).toEqualTypeOf<
476 Promise<{ value: string }> | undefined
477 >()
478 })
479 })
480β
481 describe('observers', () => {
482 it('should be an array', () => {
483 const query = queryClient
484 .getQueryCache()
485 .build<{ value: string }, CustomError>(queryClient, {
486 queryKey: queryKey(),
487 queryFn: () => Promise.resolve({ value: 'data' }),
488 })
489β
490 expectTypeOf(query.observers).toEqualTypeOf<
491 Array<QueryObserver<any, any, any, any, any>>
492 >()
493 })
494 })
495β
496 describe('setData', () => {
497 it('should return the data type it is given', () => {
498 const query = queryClient
499 .getQueryCache()
500 .build<{ value: string }, CustomError>(queryClient, {
501 queryKey: queryKey(),
502 queryFn: () => Promise.resolve({ value: 'data' }),
503 })
504β
505 expectTypeOf(query.setData).returns.toEqualTypeOf<{
506 value: string
507 }>()
508 expectTypeOf(query.setData)
509 .parameter(0)
510 .toEqualTypeOf<{ value: string }>()
511 })
512β
513 it('should require manual on the options it is given', () => {
514 const query = queryClient
515 .getQueryCache()
516 .build<{ value: string }, CustomError>(queryClient, {
517 queryKey: queryKey(),
518 queryFn: () => Promise.resolve({ value: 'data' }),
519 })
520β
521 query.setData({ value: 'data' })
522 query.setData({ value: 'data' }, { manual: true, updatedAt: 1 })
523 // @ts-expect-error manual is required when options are given
524 query.setData({ value: 'data' }, { updatedAt: 1 })
525β
526 expectTypeOf(query.setData).parameters.toEqualTypeOf<
527 [
528 newData: { value: string },
529 options?: { updatedAt?: number } & { manual: boolean },
530 ]
531 >()
532 })
533β
534 it('should reject a data type other than its own', () => {
535 const query = queryClient
536 .getQueryCache()
537 .build<{ value: string }, CustomError>(queryClient, {
538 queryKey: queryKey(),
539 queryFn: () => Promise.resolve({ value: 'data' }),
540 })
541β
542 // @ts-expect-error the data must be the query data type
543 query.setData('data')
544β
545 expectTypeOf(query.setData).parameter(0).toEqualTypeOf<{
546 value: string
547 }>()
548 })
549 })
550β
551 describe('setState', () => {
552 it('should accept a partial state and return void', () => {
553 const query = queryClient
554 .getQueryCache()
555 .build<{ value: string }, CustomError>(queryClient, {
556 queryKey: queryKey(),
557 queryFn: () => Promise.resolve({ value: 'data' }),
558 })
559β
560 query.setState({ status: 'success' })
561β
562 expectTypeOf(query.setState)
563 .parameter(0)
564 .toEqualTypeOf<Partial<QueryState<{ value: string }, CustomError>>>()
565 expectTypeOf(query.setState).returns.toEqualTypeOf<void>()
566 })
567β
568 it('should reject a state field that is not its own type', () => {
569 const query = queryClient
570 .getQueryCache()
571 .build<{ value: string }, CustomError>(queryClient, {
572 queryKey: queryKey(),
573 queryFn: () => Promise.resolve({ value: 'data' }),
574 })
575β
576 // @ts-expect-error the data must be the query data type
577 query.setState({ data: 'data' })
578β
579 expectTypeOf(query.setState)
580 .parameter(0)
581 .toHaveProperty('data')
582 .toEqualTypeOf<{ value: string } | undefined>()
583 })
584 })
585β
586 describe('cancel', () => {
587 it('should resolve with void', () => {
588 const query = queryClient
589 .getQueryCache()
590 .build<{ value: string }, CustomError>(queryClient, {
591 queryKey: queryKey(),
592 queryFn: () => Promise.resolve({ value: 'data' }),
593 })
594β
595 expectTypeOf(query.cancel).returns.toEqualTypeOf<Promise<void>>()
596 })
597β
598 it('should only accept CancelOptions', () => {
599 const query = queryClient
600 .getQueryCache()
601 .build<{ value: string }, CustomError>(queryClient, {
602 queryKey: queryKey(),
603 queryFn: () => Promise.resolve({ value: 'data' }),
604 })
605β
606 expectTypeOf(query.cancel).parameters.toEqualTypeOf<
607 [options?: CancelOptions]
608 >()
609 expectTypeOf<CancelOptions>().toEqualTypeOf<{
610 revert?: boolean
611 silent?: boolean
612 }>()
613 })
614 })
615β
616 describe('destroy', () => {
617 it('should take no arguments and return void', () => {
618 const query = queryClient
619 .getQueryCache()
620 .build<{ value: string }, CustomError>(queryClient, {
621 queryKey: queryKey(),
622 queryFn: () => Promise.resolve({ value: 'data' }),
623 })
624β
625 expectTypeOf(query.destroy).parameters.toEqualTypeOf<[]>()
626 expectTypeOf(query.destroy).returns.toEqualTypeOf<void>()
627 })
628 })
629β
630 describe('reset', () => {
631 it('should take no arguments and return void', () => {
632 const query = queryClient
633 .getQueryCache()
634 .build<{ value: string }, CustomError>(queryClient, {
635 queryKey: queryKey(),
636 queryFn: () => Promise.resolve({ value: 'data' }),
637 })
638β
639 expectTypeOf(query.reset).parameters.toEqualTypeOf<[]>()
640 expectTypeOf(query.reset).returns.toEqualTypeOf<void>()
641 })
642 })
643β
644 describe('isActive', () => {
645 it('should return a boolean', () => {
646 const query = queryClient
647 .getQueryCache()
648 .build<{ value: string }, CustomError>(queryClient, {
649 queryKey: queryKey(),
650 queryFn: () => Promise.resolve({ value: 'data' }),
651 })
652β
653 expectTypeOf(query.isActive).returns.toEqualTypeOf<boolean>()
654 })
655 })
656β
657 describe('isDisabled', () => {
658 it('should return a boolean', () => {
659 const query = queryClient
660 .getQueryCache()
661 .build<{ value: string }, CustomError>(queryClient, {
662 queryKey: queryKey(),
663 queryFn: () => Promise.resolve({ value: 'data' }),
664 })
665β
666 expectTypeOf(query.isDisabled).returns.toEqualTypeOf<boolean>()
667 })
668 })
669β
670 describe('isFetched', () => {
671 it('should return a boolean', () => {
672 const query = queryClient
673 .getQueryCache()
674 .build<{ value: string }, CustomError>(queryClient, {
675 queryKey: queryKey(),
676 queryFn: () => Promise.resolve({ value: 'data' }),
677 })
678β
679 expectTypeOf(query.isFetched).returns.toEqualTypeOf<boolean>()
680 })
681 })
682β
683 describe('isStatic', () => {
684 it('should return a boolean', () => {
685 const query = queryClient
686 .getQueryCache()
687 .build<{ value: string }, CustomError>(queryClient, {
688 queryKey: queryKey(),
689 queryFn: () => Promise.resolve({ value: 'data' }),
690 })
691β
692 expectTypeOf(query.isStatic).returns.toEqualTypeOf<boolean>()
693 })
694 })
695β
696 describe('isStale', () => {
697 it('should return a boolean', () => {
698 const query = queryClient
699 .getQueryCache()
700 .build<{ value: string }, CustomError>(queryClient, {
701 queryKey: queryKey(),
702 queryFn: () => Promise.resolve({ value: 'data' }),
703 })
704β
705 expectTypeOf(query.isStale).returns.toEqualTypeOf<boolean>()
706 })
707 })
708β
709 describe('isStaleByTime', () => {
710 it('should return a boolean', () => {
711 const query = queryClient
712 .getQueryCache()
713 .build<{ value: string }, CustomError>(queryClient, {
714 queryKey: queryKey(),
715 queryFn: () => Promise.resolve({ value: 'data' }),
716 })
717β
718 expectTypeOf(query.isStaleByTime).returns.toEqualTypeOf<boolean>()
719 })
720β
721 it('should only accept an optional stale time', () => {
722 const query = queryClient
723 .getQueryCache()
724 .build<{ value: string }, CustomError>(queryClient, {
725 queryKey: queryKey(),
726 queryFn: () => Promise.resolve({ value: 'data' }),
727 })
728β
729 query.isStaleByTime(1000)
730 query.isStaleByTime('static')
731 query.isStaleByTime()
732 // @ts-expect-error a stale time must be a number or the static literal
733 query.isStaleByTime('soon')
734β
735 expectTypeOf(query.isStaleByTime).parameters.toEqualTypeOf<
736 [staleTime?: number | 'static']
737 >()
738 })
739 })
740β
741 describe('getObserversCount', () => {
742 it('should return a number', () => {
743 const query = queryClient
744 .getQueryCache()
745 .build<{ value: string }, CustomError>(queryClient, {
746 queryKey: queryKey(),
747 queryFn: () => Promise.resolve({ value: 'data' }),
748 })
749β
750 expectTypeOf(query.getObserversCount).returns.toEqualTypeOf<number>()
751 })
752 })
753β
754 describe('invalidate', () => {
755 it('should take no arguments and return void', () => {
756 const query = queryClient
757 .getQueryCache()
758 .build<{ value: string }, CustomError>(queryClient, {
759 queryKey: queryKey(),
760 queryFn: () => Promise.resolve({ value: 'data' }),
761 })
762β
763 expectTypeOf(query.invalidate).parameters.toEqualTypeOf<[]>()
764 expectTypeOf(query.invalidate).returns.toEqualTypeOf<void>()
765 })
766 })
767β
768 describe('onFocus', () => {
769 it('should take no arguments and return void', () => {
770 const query = queryClient
771 .getQueryCache()
772 .build<{ value: string }, CustomError>(queryClient, {
773 queryKey: queryKey(),
774 queryFn: () => Promise.resolve({ value: 'data' }),
775 })
776β
777 expectTypeOf(query.onFocus).parameters.toEqualTypeOf<[]>()
778 expectTypeOf(query.onFocus).returns.toEqualTypeOf<void>()
779 })
780 })
781β
782 describe('onOnline', () => {
783 it('should take no arguments and return void', () => {
784 const query = queryClient
785 .getQueryCache()
786 .build<{ value: string }, CustomError>(queryClient, {
787 queryKey: queryKey(),
788 queryFn: () => Promise.resolve({ value: 'data' }),
789 })
790β
791 expectTypeOf(query.onOnline).parameters.toEqualTypeOf<[]>()
792 expectTypeOf(query.onOnline).returns.toEqualTypeOf<void>()
793 })
794 })
795β
796 describe('addObserver', () => {
797 it('should take a single observer and return void', () => {
798 const query = queryClient
799 .getQueryCache()
800 .build<{ value: string }, CustomError>(queryClient, {
801 queryKey: queryKey(),
802 queryFn: () => Promise.resolve({ value: 'data' }),
803 })
804β
805 expectTypeOf(query.addObserver).parameters.toEqualTypeOf<
806 [observer: QueryObserver<any, any, any, any, any>]
807 >()
808 expectTypeOf(query.addObserver).returns.toEqualTypeOf<void>()
809 })
810 })
811β
812 describe('removeObserver', () => {
813 it('should take a single observer and return void', () => {
814 const query = queryClient
815 .getQueryCache()
816 .build<{ value: string }, CustomError>(queryClient, {
817 queryKey: queryKey(),
818 queryFn: () => Promise.resolve({ value: 'data' }),
819 })
820β
821 expectTypeOf(query.removeObserver).parameters.toEqualTypeOf<
822 [observer: QueryObserver<any, any, any, any, any>]
823 >()
824 expectTypeOf(query.removeObserver).returns.toEqualTypeOf<void>()
825 })
826 })
827β
828 describe('setOptions', () => {
829 it('should accept the query options or nothing', () => {
830 const query = queryClient
831 .getQueryCache()
832 .build<{ value: string }, CustomError>(queryClient, {
833 queryKey: queryKey(),
834 queryFn: () => Promise.resolve({ value: 'data' }),
835 })
836β
837 query.setOptions()
838 query.setOptions({
839 queryKey: queryKey(),
840 queryFn: () => Promise.resolve({ value: 'data' }),
841 })
842 query.setOptions({
843 queryKey: queryKey(),
844 // @ts-expect-error the queryFn must return the query data type
845 queryFn: () => 42,
846 })
847β
848 expectTypeOf(query.setOptions).returns.toEqualTypeOf<void>()
849 })
850 })
851β
852 describe('fetch', () => {
853 it('should resolve with the query data type', () => {
854 const query = queryClient
855 .getQueryCache()
856 .build<{ value: string }, CustomError>(queryClient, {
857 queryKey: queryKey(),
858 queryFn: () => Promise.resolve({ value: 'data' }),
859 })
860β
861 expectTypeOf(query.fetch).returns.toEqualTypeOf<
862 Promise<{ value: string }>
863 >()
864 })
865β
866 it('should resolve with its data type while typing its fetch options from its query fn data type', () => {
867 const query = queryClient
868 .getQueryCache()
869 .build<{ value: string }, CustomError, number>(queryClient, {
870 queryKey: queryKey(),
871 queryFn: () => Promise.resolve({ value: 'data' }),
872 })
873β
874 expectTypeOf(query.fetch).returns.toEqualTypeOf<Promise<number>>()
875 expectTypeOf(query.fetch).parameters.toEqualTypeOf<
876 [
877 options?: QueryOptions<
878 { value: string },
879 CustomError,
880 number,
881 ReadonlyArray<unknown>
882 >,
883 fetchOptions?: FetchOptions<{ value: string }>,
884 ]
885 >()
886 })
887β
888 it('should accept the query options or nothing', () => {
889 const query = queryClient
890 .getQueryCache()
891 .build<{ value: string }, CustomError>(queryClient, {
892 queryKey: queryKey(),
893 queryFn: () => Promise.resolve({ value: 'data' }),
894 })
895β
896 query.fetch()
897 query.fetch({
898 queryKey: queryKey(),
899 queryFn: () => Promise.resolve({ value: 'data' }),
900 })
901 query.fetch({
902 queryKey: queryKey(),
903 // @ts-expect-error the queryFn must return the query data type
904 queryFn: () => 42,
905 })
906β
907 expectTypeOf(query.fetch)
908 .parameter(0)
909 .toEqualTypeOf<
910 | QueryOptions<
911 { value: string },
912 CustomError,
913 { value: string },
914 ReadonlyArray<unknown>
915 >
916 | undefined
917 >()
918 })
919 })
920β
921 describe('Query', () => {
922 it('should default its type parameters', () => {
923 expectTypeOf<Query>().toEqualTypeOf<
924 Query<unknown, DefaultError, unknown, QueryKey>
925 >()
926 })
927β
928 it('should derive its data type from its query fn data type', () => {
929 expectTypeOf<Query<{ value: string }>>().toEqualTypeOf<
930 Query<{ value: string }, DefaultError, { value: string }, QueryKey>
931 >()
932 })
933β
934 it('should keep its own fields writable', () => {
935 type Fields = Pick<Query, 'queryKey' | 'queryHash' | 'options' | 'state'>
936β
937 expectTypeOf<Fields>().toEqualTypeOf<{
938 -readonly [K in keyof Fields]: Fields[K]
939 }>()
940 })
941β
942 it('should declare exactly its public members', () => {
943 expectTypeOf<keyof Query>().toEqualTypeOf<
944 | 'queryKey'
945 | 'queryHash'
946 | 'options'
947 | 'state'
948 | 'observers'
949 | 'meta'
950 | 'promise'
951 | 'queryType'
952 | 'resetState'
953 | 'gcTime'
954 | 'setOptions'
955 | 'setData'
956 | 'setState'
957 | 'cancel'
958 | 'destroy'
959 | 'reset'
960 | 'isActive'
961 | 'isDisabled'
962 | 'isFetched'
963 | 'isStatic'
964 | 'isStale'
965 | 'isStaleByTime'
966 | 'onFocus'
967 | 'onOnline'
968 | 'addObserver'
969 | 'removeObserver'
970 | 'getObserversCount'
971 | 'invalidate'
972 | 'fetch'
973 >()
974 })
975 })
976})