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

infiniteQueryOptions.test-d.ts

137 lines4.2 KB

1import { QueryClient, skipToken } from '@tanstack/query-core'
2import { createKey } from '@tanstack/query-test-utils'
3import { assertType, describe, expectTypeOf, it } from 'vitest'
4import { createInfiniteQuery, infiniteQueryOptions } from '../src/index.js'
5import type { InfiniteData } from '@tanstack/query-core'
6​
7describe('infiniteQueryOptions', () => {
8 it('should not allow excess properties', () => {
9 const key = createKey()
10 assertType(
11 infiniteQueryOptions({
12 queryKey: key,
13 queryFn: () => Promise.resolve('data'),
14 getNextPageParam: () => 1,
15 initialPageParam: 1,
16 // @ts-expect-error this is a good error, because stallTime does not exist!
17 stallTime: 1000,
18 }),
19 )
20 })
21​
22 it('should infer types for callbacks', () => {
23 const key = createKey()
24 infiniteQueryOptions({
25 queryKey: key,
26 queryFn: () => Promise.resolve('data'),
27 staleTime: 1000,
28 getNextPageParam: () => 1,
29 initialPageParam: 1,
30 select: (data) => {
31 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
32 },
33 })
34 })
35​
36 it('should work when passed to createInfiniteQuery', () => {
37 const key = createKey()
38 const options = infiniteQueryOptions({
39 queryKey: key,
40 queryFn: () => Promise.resolve('string'),
41 getNextPageParam: () => 1,
42 initialPageParam: 1,
43 })
44​
45 const query = createInfiniteQuery(() => options)
46​
47 // known issue: type of pageParams is unknown when returned from useInfiniteQuery
48 expectTypeOf(query.data).toEqualTypeOf<
49 InfiniteData<string, unknown> | undefined
50 >()
51 })
52​
53 it('should work when passed to createInfiniteQuery with initialData', () => {
54 const key = createKey()
55 const options = infiniteQueryOptions({
56 queryKey: key,
57 queryFn: () => Promise.resolve('string'),
58 getNextPageParam: () => 1,
59 initialPageParam: 1,
60 initialData: { pages: ['string'], pageParams: [1] },
61 })
62​
63 const query = createInfiniteQuery(() => options)
64​
65 // known issue: type of pageParams is unknown when returned from createInfiniteQuery
66 expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
67 })
68​
69 it('should work when passed to infiniteQuery', async () => {
70 const options = infiniteQueryOptions({
71 queryKey: ['key'],
72 queryFn: () => Promise.resolve('string'),
73 getNextPageParam: () => 1,
74 initialPageParam: 1,
75 })
76​
77 const data = await new QueryClient().infiniteQuery(options)
78​
79 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
80 })
81​
82 it('should work when passed to infiniteQuery with select', async () => {
83 const options = infiniteQueryOptions({
84 queryKey: ['key'],
85 queryFn: () => Promise.resolve('string'),
86 getNextPageParam: () => 1,
87 initialPageParam: 1,
88 select: (data) => data.pages,
89 })
90​
91 const data = await new QueryClient().infiniteQuery(options)
92​
93 expectTypeOf(data).toEqualTypeOf<Array<string>>()
94 })
95​
96 it('should work when passed to infiniteQuery with enabled: false', async () => {
97 const options = infiniteQueryOptions({
98 queryKey: ['key'],
99 queryFn: () => Promise.resolve('string'),
100 getNextPageParam: () => 1,
101 initialPageParam: 1,
102 enabled: false,
103 })
104​
105 const data = await new QueryClient().infiniteQuery(options)
106​
107 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
108 })
109​
110 it('should work when passed to infiniteQuery with skipToken', async () => {
111 const options = infiniteQueryOptions({
112 queryKey: ['key'],
113 queryFn: skipToken,
114 getNextPageParam: () => 1,
115 initialPageParam: 1,
116 })
117​
118 const data = await new QueryClient().infiniteQuery(options)
119​
120 expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
121 })
122​
123 it('should work when passed to fetchInfiniteQuery', async () => {
124 const key = createKey()
125 const options = infiniteQueryOptions({
126 queryKey: key,
127 queryFn: () => Promise.resolve('string'),
128 getNextPageParam: () => 1,
129 initialPageParam: 1,
130 })
131​
132 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
133 const data = await new QueryClient().fetchInfiniteQuery(options)
134​
135 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
136 })
137})