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

infiniteQueryOptions.test-d.tsx

80 lines2.2 KB

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