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

createInfiniteQuery.test-d.tsx

109 lines2.4 KB

1import { describe } from 'node:test'
2import { expectTypeOf, it } from 'vitest'
3import { type InfiniteData, dataTagSymbol } from '@tanstack/query-core'
4import {
5 createInfiniteQuery,
6 infiniteQueryOptions,
7} from '../createInfiniteQuery'
8import type {
9 DefinedInitialDataInfiniteOptions,
10 UndefinedInitialDataInfiniteOptions,
11} from '../createInfiniteQuery'
12​
13const doNotRun = (_callback: () => void) => {}
14​
15describe('infiniteQueryOptions', () => {
16 it('should infer defined types', () => {
17 const options = infiniteQueryOptions({
18 getNextPageParam: () => 10,
19 queryKey: ['key'],
20 queryFn: () => ({ wow: true }),
21 initialData: {
22 pageParams: [undefined],
23 pages: [{ wow: true }],
24 },
25 initialPageParam: 0,
26 })
27​
28 doNotRun(() => {
29 expectTypeOf<
30 InfiniteData<
31 {
32 wow: boolean
33 },
34 unknown
35 >
36 >(createInfiniteQuery(() => options).data)
37​
38 expectTypeOf<
39 ReturnType<
40 DefinedInitialDataInfiniteOptions<
41 {
42 wow: boolean
43 },
44 Error,
45 InfiniteData<
46 {
47 wow: boolean
48 },
49 unknown
50 >,
51 Array<string>,
52 number | undefined
53 >
54 >
55 >(options)
56​
57 expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<
58 InfiniteData<{ wow: boolean }>
59 >()
60 })
61 })
62​
63 it('should work without defined types', () => {
64 const options = infiniteQueryOptions({
65 getNextPageParam: () => undefined,
66 queryKey: ['key'],
67 queryFn: () => ({ wow: true }),
68 initialPageParam: 0,
69 })
70​
71 doNotRun(() => {
72 expectTypeOf<
73 () =>
74 | InfiniteData<
75 {
76 wow: boolean
77 },
78 unknown
79 >
80 | undefined
81 >(() => createInfiniteQuery(() => options).data)
82​
83 expectTypeOf<
84 ReturnType<
85 UndefinedInitialDataInfiniteOptions<
86 {
87 wow: boolean
88 },
89 Error,
90 InfiniteData<
91 {
92 wow: boolean
93 },
94 unknown
95 >,
96 Array<string>,
97 number
98 >
99 >
100 >(options)
101​
102 expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<
103 InfiniteData<{
104 wow: boolean
105 }>
106 >()
107 })
108 })
109})