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