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

useInfiniteQuery.test-d.tsx

174 lines5.7 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { QueryClient } from '@tanstack/query-core'
3import { queryKey } from '@tanstack/query-test-utils'
4import { useInfiniteQuery } from '../useInfiniteQuery'
5import type { InfiniteData } from '@tanstack/query-core'
6import type { UseInfiniteQueryOptions } from '../types'
7​
8describe('pageParam', () => {
9 it('should define type of param passed to queryFunctionContext with initialPageParam', () => {
10 useInfiniteQuery({
11 queryKey: queryKey(),
12 queryFn: ({ pageParam }) => {
13 expectTypeOf(pageParam).toEqualTypeOf<number>()
14 },
15 initialPageParam: 1,
16 getNextPageParam: () => undefined,
17 })
18 })
19​
20 it('should pass direction to queryFn of useInfiniteQuery', () => {
21 useInfiniteQuery({
22 queryKey: queryKey(),
23 queryFn: ({ direction }) => {
24 expectTypeOf(direction).toEqualTypeOf<'forward' | 'backward'>()
25 },
26 initialPageParam: 1,
27 getNextPageParam: () => undefined,
28 })
29 })
30​
31 it('should define type of param passed to queryFunctionContext with initialPageParam for fetchInfiniteQuery', () => {
32 const queryClient = new QueryClient()
33 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
34 queryClient.fetchInfiniteQuery({
35 queryKey: queryKey(),
36 queryFn: ({ pageParam }) => {
37 expectTypeOf(pageParam).toEqualTypeOf<number>()
38 },
39 initialPageParam: 1,
40 })
41 })
42​
43 it('should define type of param passed to queryFunctionContext with initialPageParam for infiniteQuery', () => {
44 const queryClient = new QueryClient()
45 queryClient.infiniteQuery({
46 queryKey: ['key'],
47 queryFn: ({ pageParam }) => {
48 expectTypeOf(pageParam).toEqualTypeOf<number>()
49 return Promise.resolve(pageParam)
50 },
51 initialPageParam: 1,
52 })
53 })
54​
55 it('should define type of param passed to queryFunctionContext with initialPageParam for prefetchInfiniteQuery', () => {
56 const queryClient = new QueryClient()
57 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
58 queryClient.prefetchInfiniteQuery({
59 queryKey: queryKey(),
60 queryFn: ({ pageParam }) => {
61 expectTypeOf(pageParam).toEqualTypeOf<number>()
62 },
63 initialPageParam: 1,
64 })
65 })
66})
67describe('select', () => {
68 it('should still return paginated data if no select result', () => {
69 const infiniteQuery = useInfiniteQuery({
70 queryKey: queryKey(),
71 queryFn: ({ pageParam }) => {
72 return pageParam * 5
73 },
74 initialPageParam: 1,
75 getNextPageParam: () => undefined,
76 })
77​
78 // TODO: Order of generics prevents pageParams to be typed correctly. Using `unknown` for now
79 expectTypeOf(infiniteQuery.data).toEqualTypeOf<
80 InfiniteData<number, unknown> | undefined
81 >()
82 })
83​
84 it('should be able to transform data to arbitrary result', () => {
85 const infiniteQuery = useInfiniteQuery({
86 queryKey: queryKey(),
87 queryFn: ({ pageParam }) => {
88 return pageParam * 5
89 },
90 initialPageParam: 1,
91 getNextPageParam: () => undefined,
92 select: (data) => {
93 expectTypeOf(data).toEqualTypeOf<InfiniteData<number, number>>()
94 return 'selected' as const
95 },
96 })
97​
98 expectTypeOf(infiniteQuery.data).toEqualTypeOf<'selected' | undefined>()
99 })
100})
101describe('getNextPageParam / getPreviousPageParam', () => {
102 it('should get typed params', () => {
103 const infiniteQuery = useInfiniteQuery({
104 queryKey: queryKey(),
105 queryFn: ({ pageParam }) => {
106 return String(pageParam)
107 },
108 initialPageParam: 1,
109 getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) => {
110 expectTypeOf(lastPage).toEqualTypeOf<string>()
111 expectTypeOf(allPages).toEqualTypeOf<Array<string>>()
112 expectTypeOf(lastPageParam).toEqualTypeOf<number>()
113 expectTypeOf(allPageParams).toEqualTypeOf<Array<number>>()
114 return undefined
115 },
116 getPreviousPageParam: (
117 firstPage,
118 allPages,
119 firstPageParam,
120 allPageParams,
121 ) => {
122 expectTypeOf(firstPage).toEqualTypeOf<string>()
123 expectTypeOf(allPages).toEqualTypeOf<Array<string>>()
124 expectTypeOf(firstPageParam).toEqualTypeOf<number>()
125 expectTypeOf(allPageParams).toEqualTypeOf<Array<number>>()
126 return undefined
127 },
128 })
129​
130 // TODO: Order of generics prevents pageParams to be typed correctly. Using `unknown` for now
131 expectTypeOf(infiniteQuery.data).toEqualTypeOf<
132 InfiniteData<string, unknown> | undefined
133 >()
134 })
135})
136​
137describe('error booleans', () => {
138 it('should not be permanently `false`', () => {
139 const {
140 isFetchNextPageError,
141 isFetchPreviousPageError,
142 isLoadingError,
143 isRefetchError,
144 } = useInfiniteQuery({
145 queryKey: queryKey(),
146 queryFn: ({ pageParam }) => {
147 return pageParam * 5
148 },
149 initialPageParam: 1,
150 getNextPageParam: () => undefined,
151 })
152​
153 expectTypeOf(isFetchNextPageError).toEqualTypeOf<boolean>()
154 expectTypeOf(isFetchPreviousPageError).toEqualTypeOf<boolean>()
155 expectTypeOf(isLoadingError).toEqualTypeOf<boolean>()
156 expectTypeOf(isRefetchError).toEqualTypeOf<boolean>()
157 })
158})
159​
160describe('UseInfiniteQueryOptions', () => {
161 it('should default TData to InfiniteData<TQueryFnData>', () => {
162 const options: UseInfiniteQueryOptions<number, Error> = {
163 queryKey: queryKey(),
164 queryFn: () => 5,
165 initialPageParam: 1,
166 getNextPageParam: () => undefined,
167 }
168 const infiniteQuery = useInfiniteQuery(options)
169​
170 expectTypeOf(infiniteQuery.data).toEqualTypeOf<
171 InfiniteData<number, unknown> | undefined
172 >()
173 })
174})