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

infiniteQueryOptions.test-d.ts

205 lines6.1 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 keep data possibly undefined when initialData is a ternary that can be undefined', () => {
77 const options = infiniteQueryOptions({
78 queryKey: createKey(),
79 queryFn: () => Promise.resolve({ example: true }),
80 initialData:
81 Math.random() > 0.5
82 ? { pages: [{ example: true }], pageParams: [1] }
83 : undefined,
84 getNextPageParam: () => 1,
85 initialPageParam: 1,
86 })
87​
88 const { data } = reactive(useInfiniteQuery(options))
89​
90 expectTypeOf(data).toEqualTypeOf<
91 InfiniteData<{ example: boolean }, unknown> | undefined
92 >()
93 })
94​
95 it('should work when passed to infiniteQuery', async () => {
96 const options = infiniteQueryOptions({
97 queryKey: ['key'],
98 queryFn: () => Promise.resolve('string'),
99 getNextPageParam: () => 1,
100 initialPageParam: 1,
101 })
102​
103 const data = await new QueryClient().infiniteQuery({
104 ...options,
105 staleTime: 0,
106 pages: 1,
107 })
108​
109 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
110 })
111​
112 it('should work when passed to infiniteQuery with select', async () => {
113 const options = infiniteQueryOptions({
114 queryKey: ['key'],
115 queryFn: () => Promise.resolve('string'),
116 getNextPageParam: () => 1,
117 initialPageParam: 1,
118 select: (data) => data.pages,
119 })
120​
121 const data = await new QueryClient().infiniteQuery({
122 ...options,
123 staleTime: 0,
124 pages: 1,
125 })
126​
127 expectTypeOf(data).toEqualTypeOf<Array<string>>()
128 })
129​
130 it('should tag the queryKey with the result type of the QueryFn', () => {
131 const key = createKey()
132 const { queryKey: tagged } = infiniteQueryOptions({
133 queryKey: key,
134 queryFn: () => Promise.resolve('string'),
135 getNextPageParam: () => 1,
136 initialPageParam: 1,
137 })
138​
139 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
140 })
141​
142 it('should tag the queryKey even if no promise is returned', () => {
143 const key = createKey()
144 const { queryKey: tagged } = infiniteQueryOptions({
145 queryKey: key,
146 queryFn: () => 'string',
147 getNextPageParam: () => 1,
148 initialPageParam: 1,
149 })
150​
151 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
152 })
153​
154 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
155 const key = createKey()
156 const { queryKey: tagged } = infiniteQueryOptions({
157 queryKey: key,
158 queryFn: () => Promise.resolve('string'),
159 select: (data) => data.pages,
160 getNextPageParam: () => 1,
161 initialPageParam: 1,
162 })
163​
164 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
165 })
166​
167 it('should return the proper type when passed to getQueryData', () => {
168 const key = createKey()
169 const { queryKey: tagged } = infiniteQueryOptions({
170 queryKey: key,
171 queryFn: () => Promise.resolve('string'),
172 getNextPageParam: () => 1,
173 initialPageParam: 1,
174 })
175​
176 const queryClient = new QueryClient()
177 const data = queryClient.getQueryData(tagged)
178​
179 expectTypeOf(data).toEqualTypeOf<
180 InfiniteData<string, unknown> | undefined
181 >()
182 })
183​
184 it('should properly type when passed to setQueryData', () => {
185 const key = createKey()
186 const { queryKey: tagged } = infiniteQueryOptions({
187 queryKey: key,
188 queryFn: () => Promise.resolve('string'),
189 getNextPageParam: () => 1,
190 initialPageParam: 1,
191 })
192​
193 const queryClient = new QueryClient()
194 const data = queryClient.setQueryData(tagged, (prev) => {
195 expectTypeOf(prev).toEqualTypeOf<
196 InfiniteData<string, unknown> | undefined
197 >()
198 return prev
199 })
200​
201 expectTypeOf(data).toEqualTypeOf<
202 InfiniteData<string, unknown> | undefined
203 >()
204 })
205})