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

infiniteQueryOptions.test-d.tsx

77 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 // Non-optional even without initialData: `data` is a suspending read.
55 expectTypeOf(() => useInfiniteQuery(() => options).data).toEqualTypeOf<
56 () => InfiniteData<{ wow: boolean }, unknown>
57 >()
58​
59 expectTypeOf(options).toExtend<
60 ReturnType<
61 UndefinedInitialDataInfiniteOptions<
62 { wow: boolean },
63 Error,
64 InfiniteData<{ wow: boolean }, unknown>,
65 Array<string>,
66 number
67 >
68 >
69 >()
70​
71 expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<
72 InfiniteData<{
73 wow: boolean
74 }>
75 >()
76 })
77})