Open History to see the last commit
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})
67β
68describe('select', () => {
69 it('should still return paginated data if no select result', () => {
70 const infiniteQuery = useInfiniteQuery({
71 queryKey: queryKey(),
72 queryFn: ({ pageParam }) => {
73 return pageParam * 5
74 },
75 initialPageParam: 1,
76 getNextPageParam: () => undefined,
77 })
78β
79 // TODO: Order of generics prevents pageParams to be typed correctly. Using `unknown` for now
80 expectTypeOf(infiniteQuery.data).toEqualTypeOf<
81 InfiniteData<number, unknown> | undefined
82 >()
83 })
84β
85 it('should be able to transform data to arbitrary result', () => {
86 const infiniteQuery = useInfiniteQuery({
87 queryKey: queryKey(),
88 queryFn: ({ pageParam }) => {
89 return pageParam * 5
90 },
91 initialPageParam: 1,
92 getNextPageParam: () => undefined,
93 select: (data) => {
94 expectTypeOf(data).toEqualTypeOf<InfiniteData<number, number>>()
95 return 'selected' as const
96 },
97 })
98β
99 expectTypeOf(infiniteQuery.data).toEqualTypeOf<'selected' | undefined>()
100 })
101})
102β
103describe('getNextPageParam / getPreviousPageParam', () => {
104 it('should get typed params', () => {
105 const infiniteQuery = useInfiniteQuery({
106 queryKey: queryKey(),
107 queryFn: ({ pageParam }) => {
108 return String(pageParam)
109 },
110 initialPageParam: 1,
111 getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) => {
112 expectTypeOf(lastPage).toEqualTypeOf<string>()
113 expectTypeOf(allPages).toEqualTypeOf<Array<string>>()
114 expectTypeOf(lastPageParam).toEqualTypeOf<number>()
115 expectTypeOf(allPageParams).toEqualTypeOf<Array<number>>()
116 return undefined
117 },
118 getPreviousPageParam: (
119 firstPage,
120 allPages,
121 firstPageParam,
122 allPageParams,
123 ) => {
124 expectTypeOf(firstPage).toEqualTypeOf<string>()
125 expectTypeOf(allPages).toEqualTypeOf<Array<string>>()
126 expectTypeOf(firstPageParam).toEqualTypeOf<number>()
127 expectTypeOf(allPageParams).toEqualTypeOf<Array<number>>()
128 return undefined
129 },
130 })
131β
132 // TODO: Order of generics prevents pageParams to be typed correctly. Using `unknown` for now
133 expectTypeOf(infiniteQuery.data).toEqualTypeOf<
134 InfiniteData<string, unknown> | undefined
135 >()
136 })
137})
138β
139describe('error booleans', () => {
140 it('should not be permanently `false`', () => {
141 const {
142 isFetchNextPageError,
143 isFetchPreviousPageError,
144 isLoadingError,
145 isRefetchError,
146 } = useInfiniteQuery({
147 queryKey: queryKey(),
148 queryFn: ({ pageParam }) => {
149 return pageParam * 5
150 },
151 initialPageParam: 1,
152 getNextPageParam: () => undefined,
153 })
154β
155 expectTypeOf(isFetchNextPageError).toEqualTypeOf<boolean>()
156 expectTypeOf(isFetchPreviousPageError).toEqualTypeOf<boolean>()
157 expectTypeOf(isLoadingError).toEqualTypeOf<boolean>()
158 expectTypeOf(isRefetchError).toEqualTypeOf<boolean>()
159 })
160})
161β
162describe('UseInfiniteQueryOptions', () => {
163 it('should default TData to InfiniteData<TQueryFnData>', () => {
164 const options: UseInfiniteQueryOptions<number, Error> = {
165 queryKey: queryKey(),
166 queryFn: () => 5,
167 initialPageParam: 1,
168 getNextPageParam: () => undefined,
169 }
170 const infiniteQuery = useInfiniteQuery(options)
171β
172 expectTypeOf(infiniteQuery.data).toEqualTypeOf<
173 InfiniteData<number, unknown> | undefined
174 >()
175 })
176})