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

infiniteQueryOptions.test-d.tsx

76 lines2.1 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { dataTagSymbol } from '@tanstack/query-core'
3import { queryKey } from '@tanstack/query-test-utils'
4import { useInfiniteQuery } from '../useInfiniteQuery'
5import { infiniteQueryOptions } from '../infiniteQueryOptions'
6import type { InfiniteData } from '@tanstack/query-core'
7import type {
8 DefinedInitialDataInfiniteOptions,
9 UndefinedInitialDataInfiniteOptions,
10} from '../infiniteQueryOptions'
11​
12describe('infiniteQueryOptions', () => {
13 it('should infer defined types', () => {
14 const options = infiniteQueryOptions({
15 getNextPageParam: () => 10,
16 queryKey: queryKey(),
17 queryFn: () => ({ wow: true }),
18 initialData: {
19 pageParams: [undefined],
20 pages: [{ wow: true }],
21 },
22 initialPageParam: 0,
23 })
24​
25 expectTypeOf(useInfiniteQuery(() => options).data).toEqualTypeOf<
26 InfiniteData<{ wow: boolean }, unknown>
27 >()
28​
29 expectTypeOf(options).toExtend<
30 ReturnType<
31 DefinedInitialDataInfiniteOptions<
32 { wow: boolean },
33 Error,
34 InfiniteData<{ wow: boolean }, unknown>,
35 Array<string>,
36 number | undefined
37 >
38 >
39 >()
40​
41 expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<
42 InfiniteData<{ wow: boolean }>
43 >()
44 })
45​
46 it('should work without defined types', () => {
47 const options = infiniteQueryOptions({
48 getNextPageParam: () => undefined,
49 queryKey: queryKey(),
50 queryFn: () => ({ wow: true }),
51 initialPageParam: 0,
52 })
53​
54 expectTypeOf(() => useInfiniteQuery(() => options).data).toEqualTypeOf<
55 () => InfiniteData<{ wow: boolean }, unknown> | undefined
56 >()
57​
58 expectTypeOf(options).toExtend<
59 ReturnType<
60 UndefinedInitialDataInfiniteOptions<
61 { wow: boolean },
62 Error,
63 InfiniteData<{ wow: boolean }, unknown>,
64 Array<string>,
65 number
66 >
67 >
68 >()
69​
70 expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<
71 InfiniteData<{
72 wow: boolean
73 }>
74 >()
75 })
76})