|
1 | 1 | import { assertType, describe, expectTypeOf, it } from 'vitest' |
| 2 | +import { skipToken } from '@tanstack/query-core' |
2 | 3 | import { queryKey } from '@tanstack/query-test-utils' |
3 | 4 | import { QueryClient } from '../queryClient' |
4 | 5 | import type { DataTag, InfiniteData } from '@tanstack/query-core' |
@@ -151,3 +152,108 @@ describe('fetchInfiniteQuery', () => { |
151 | 152 | ]) |
152 | 153 | }) |
153 | 154 | }) |
| 155 | + |
| 156 | +describe('query', () => { |
| 157 | + it('should return the selected type', () => { |
| 158 | + const result = new QueryClient().query({ |
| 159 | + queryKey: ['key'], |
| 160 | + queryFn: () => Promise.resolve('string'), |
| 161 | + select: (data) => data.length, |
| 162 | + }) |
| 163 | + |
| 164 | + expectTypeOf(result).toEqualTypeOf<Promise<number>>() |
| 165 | + }) |
| 166 | + |
| 167 | + it('should infer select type with skipToken', () => { |
| 168 | + const result = new QueryClient().query({ |
| 169 | + queryKey: ['key'], |
| 170 | + queryFn: skipToken, |
| 171 | + select: (data: string) => data.length, |
| 172 | + }) |
| 173 | + |
| 174 | + expectTypeOf(result).toEqualTypeOf<Promise<number>>() |
| 175 | + }) |
| 176 | + |
| 177 | + it('should infer select type with skipToken and enabled false', () => { |
| 178 | + const result = new QueryClient().query({ |
| 179 | + queryKey: ['key'], |
| 180 | + queryFn: skipToken, |
| 181 | + enabled: false, |
| 182 | + select: (data: string) => data.length, |
| 183 | + }) |
| 184 | + |
| 185 | + expectTypeOf(result).toEqualTypeOf<Promise<number>>() |
| 186 | + }) |
| 187 | + |
| 188 | + it('should infer select type with skipToken and enabled true', () => { |
| 189 | + const result = new QueryClient().query({ |
| 190 | + queryKey: ['key'], |
| 191 | + queryFn: skipToken, |
| 192 | + enabled: true, |
| 193 | + select: (data: string) => data.length, |
| 194 | + }) |
| 195 | + |
| 196 | + expectTypeOf(result).toEqualTypeOf<Promise<number>>() |
| 197 | + }) |
| 198 | +}) |
| 199 | + |
| 200 | +describe('infiniteQuery', () => { |
| 201 | + it('should return infinite data', async () => { |
| 202 | + const data = await new QueryClient().infiniteQuery({ |
| 203 | + queryKey: ['key'], |
| 204 | + queryFn: () => Promise.resolve('string'), |
| 205 | + getNextPageParam: () => 1, |
| 206 | + initialPageParam: 1, |
| 207 | + }) |
| 208 | + |
| 209 | + expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>() |
| 210 | + }) |
| 211 | + |
| 212 | + it('should return the selected type', () => { |
| 213 | + const result = new QueryClient().infiniteQuery({ |
| 214 | + queryKey: ['key'], |
| 215 | + queryFn: () => Promise.resolve({ count: 1 }), |
| 216 | + getNextPageParam: () => 2, |
| 217 | + initialPageParam: 1, |
| 218 | + select: (data) => data.pages.map((page) => page.count), |
| 219 | + }) |
| 220 | + |
| 221 | + expectTypeOf(result).toEqualTypeOf<Promise<Array<number>>>() |
| 222 | + }) |
| 223 | + |
| 224 | + it('should allow passing pages with getNextPageParam', () => { |
| 225 | + assertType<Parameters<QueryClient['infiniteQuery']>>([ |
| 226 | + { |
| 227 | + queryKey: ['key'], |
| 228 | + queryFn: () => Promise.resolve('string'), |
| 229 | + initialPageParam: 1, |
| 230 | + getNextPageParam: () => 1, |
| 231 | + pages: 5, |
| 232 | + }, |
| 233 | + ]) |
| 234 | + }) |
| 235 | + |
| 236 | + it('should not allow passing pages without getNextPageParam', () => { |
| 237 | + assertType<Parameters<QueryClient['infiniteQuery']>>([ |
| 238 | + // @ts-expect-error Property 'getNextPageParam' is missing |
| 239 | + { |
| 240 | + queryKey: ['key'], |
| 241 | + queryFn: () => Promise.resolve('string'), |
| 242 | + initialPageParam: 1, |
| 243 | + pages: 5, |
| 244 | + }, |
| 245 | + ]) |
| 246 | + }) |
| 247 | + |
| 248 | + it('should preserve page param inference', () => { |
| 249 | + new QueryClient().infiniteQuery({ |
| 250 | + queryKey: ['key'], |
| 251 | + queryFn: ({ pageParam }) => { |
| 252 | + expectTypeOf(pageParam).toEqualTypeOf<number>() |
| 253 | + return Promise.resolve(pageParam.toString()) |
| 254 | + }, |
| 255 | + initialPageParam: 1, |
| 256 | + getNextPageParam: () => undefined, |
| 257 | + }) |
| 258 | + }) |
| 259 | +}) |
0 commit comments