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

infiniteQueryOptions.test-d.ts

221 lines6.6 KB

1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { dataTagSymbol } from '@tanstack/query-core'
3import { reactive } from 'vue-demi'
4import { createKey } from '@tanstack/query-test-utils'
5import { infiniteQueryOptions } from '../infiniteQueryOptions'
6import { QueryClient } from '../queryClient'
7import { useInfiniteQuery } from '../useInfiniteQuery'
8import type { InfiniteData } from '@tanstack/query-core'
9​
10// Regression test for exported infiniteQueryOptions inference under declaration emit.
11// TypeScript should be able to name the return type without expanding the
12// internal data tag symbols into the consumer's .d.ts output.
13export const exportedInfiniteQueryOptions = infiniteQueryOptions({
14 queryKey: ['invalid'],
15 getNextPageParam: () => 1,
16 initialPageParam: 1,
17})
18​
19describe('infiniteQueryOptions', () => {
20 it('should not allow excess properties', () => {
21 const key = createKey()
22 assertType(
23 infiniteQueryOptions({
24 queryKey: key,
25 queryFn: () => Promise.resolve('data'),
26 getNextPageParam: () => 1,
27 initialPageParam: 1,
28 // @ts-expect-error this is a good error, because stallTime does not exist!
29 stallTime: 1000,
30 }),
31 )
32 })
33​
34 it('should infer types for callbacks', () => {
35 const key = createKey()
36 infiniteQueryOptions({
37 queryKey: key,
38 queryFn: () => Promise.resolve('data'),
39 staleTime: 1000,
40 getNextPageParam: () => 1,
41 initialPageParam: 1,
42 select: (data) => {
43 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
44 },
45 })
46 })
47​
48 it('should work when passed to useInfiniteQuery', () => {
49 const key = createKey()
50 const options = infiniteQueryOptions({
51 queryKey: key,
52 queryFn: () => Promise.resolve('string'),
53 getNextPageParam: () => 1,
54 initialPageParam: 1,
55 })
56​
57 const { data } = reactive(useInfiniteQuery(options))
58​
59 expectTypeOf(data).toEqualTypeOf<
60 InfiniteData<string, unknown> | undefined
61 >()
62 })
63​
64 it('should keep initialData required when it is defined', () => {
65 const options = infiniteQueryOptions({
66 queryKey: createKey(),
67 queryFn: () => Promise.resolve({ example: true }),
68 initialData: { pages: [{ example: true }], pageParams: [1] },
69 getNextPageParam: () => 1,
70 initialPageParam: 1,
71 })
72​
73 expectTypeOf<undefined>().not.toExtend<typeof options.initialData>()
74 })
75​
76 it('should have defined data when passed to useInfiniteQuery with defined initialData', () => {
77 const options = infiniteQueryOptions({
78 queryKey: createKey(),
79 queryFn: () => Promise.resolve({ example: true }),
80 initialData: { pages: [{ example: true }], pageParams: [1] },
81 getNextPageParam: () => 1,
82 initialPageParam: 1,
83 })
84​
85 const { data } = reactive(useInfiniteQuery(options))
86​
87 expectTypeOf(data).toEqualTypeOf<
88 InfiniteData<{ example: boolean }, unknown>
89 >()
90 })
91​
92 it('should keep data possibly undefined when initialData is a ternary that can be undefined', () => {
93 const options = infiniteQueryOptions({
94 queryKey: createKey(),
95 queryFn: () => Promise.resolve({ example: true }),
96 initialData:
97 Math.random() > 0.5
98 ? { pages: [{ example: true }], pageParams: [1] }
99 : undefined,
100 getNextPageParam: () => 1,
101 initialPageParam: 1,
102 })
103​
104 const { data } = reactive(useInfiniteQuery(options))
105​
106 expectTypeOf(data).toEqualTypeOf<
107 InfiniteData<{ example: boolean }, unknown> | undefined
108 >()
109 })
110​
111 it('should work when passed to infiniteQuery', async () => {
112 const options = infiniteQueryOptions({
113 queryKey: ['key'],
114 queryFn: () => Promise.resolve('string'),
115 getNextPageParam: () => 1,
116 initialPageParam: 1,
117 })
118​
119 const data = await new QueryClient().infiniteQuery({
120 ...options,
121 staleTime: 0,
122 pages: 1,
123 })
124​
125 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
126 })
127​
128 it('should work when passed to infiniteQuery with select', async () => {
129 const options = infiniteQueryOptions({
130 queryKey: ['key'],
131 queryFn: () => Promise.resolve('string'),
132 getNextPageParam: () => 1,
133 initialPageParam: 1,
134 select: (data) => data.pages,
135 })
136​
137 const data = await new QueryClient().infiniteQuery({
138 ...options,
139 staleTime: 0,
140 pages: 1,
141 })
142​
143 expectTypeOf(data).toEqualTypeOf<Array<string>>()
144 })
145​
146 it('should tag the queryKey with the result type of the QueryFn', () => {
147 const key = createKey()
148 const { queryKey: tagged } = infiniteQueryOptions({
149 queryKey: key,
150 queryFn: () => Promise.resolve('string'),
151 getNextPageParam: () => 1,
152 initialPageParam: 1,
153 })
154​
155 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
156 })
157​
158 it('should tag the queryKey even if no promise is returned', () => {
159 const key = createKey()
160 const { queryKey: tagged } = infiniteQueryOptions({
161 queryKey: key,
162 queryFn: () => 'string',
163 getNextPageParam: () => 1,
164 initialPageParam: 1,
165 })
166​
167 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
168 })
169​
170 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
171 const key = createKey()
172 const { queryKey: tagged } = infiniteQueryOptions({
173 queryKey: key,
174 queryFn: () => Promise.resolve('string'),
175 select: (data) => data.pages,
176 getNextPageParam: () => 1,
177 initialPageParam: 1,
178 })
179​
180 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
181 })
182​
183 it('should return the proper type when passed to getQueryData', () => {
184 const key = createKey()
185 const { queryKey: tagged } = infiniteQueryOptions({
186 queryKey: key,
187 queryFn: () => Promise.resolve('string'),
188 getNextPageParam: () => 1,
189 initialPageParam: 1,
190 })
191​
192 const queryClient = new QueryClient()
193 const data = queryClient.getQueryData(tagged)
194​
195 expectTypeOf(data).toEqualTypeOf<
196 InfiniteData<string, unknown> | undefined
197 >()
198 })
199​
200 it('should properly type when passed to setQueryData', () => {
201 const key = createKey()
202 const { queryKey: tagged } = infiniteQueryOptions({
203 queryKey: key,
204 queryFn: () => Promise.resolve('string'),
205 getNextPageParam: () => 1,
206 initialPageParam: 1,
207 })
208​
209 const queryClient = new QueryClient()
210 const data = queryClient.setQueryData(tagged, (prev) => {
211 expectTypeOf(prev).toEqualTypeOf<
212 InfiniteData<string, unknown> | undefined
213 >()
214 return prev
215 })
216​
217 expectTypeOf(data).toEqualTypeOf<
218 InfiniteData<string, unknown> | undefined
219 >()
220 })
221})