Open History to see the last commit
1import { createKey, sleep } from '@tanstack/query-test-utils'
2import { fireEvent } from '@testing-library/preact'
3import { Suspense } from 'preact/compat'
4import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
5import {
6 QueryCache,
7 QueryClient,
8 noop,
9 usePrefetchInfiniteQuery,
10 useSuspenseInfiniteQuery,
11} from '..'
12import { renderWithClient } from './utils'
13import type { JSX, VNode } from 'preact'
14β
15describe('usePrefetchInfiniteQuery', () => {
16 let queryCache: QueryCache
17 let queryClient: QueryClient
18β
19 beforeEach(() => {
20 vi.useFakeTimers()
21 queryCache = new QueryCache()
22 queryClient = new QueryClient({ queryCache })
23 })
24β
25 afterEach(() => {
26 queryClient.clear()
27 Fallback.mockClear()
28 vi.useRealTimers()
29 })
30β
31 const Fallback = vi
32 .fn<() => JSX.Element>()
33 .mockReturnValue(<div>Loading...</div>)
34β
35 it('should prefetch an infinite query if query state does not exist', async () => {
36 const data = [
37 'Do you fetch on render?',
38 'Or do you render as you fetch?',
39 'Either way, Tanstack Query helps you!',
40 ]
41β
42 const queryOpts = {
43 queryKey: createKey(),
44 queryFn: vi
45 .fn<(context: { pageParam: number }) => Promise<string>>()
46 .mockImplementation(({ pageParam }) =>
47 sleep(10).then(() => data[pageParam]!),
48 ),
49 initialPageParam: 0,
50 getNextPageParam: (_lastPage: string, allPages: Array<string>) =>
51 allPages.length < data.length ? allPages.length : undefined,
52 }
53β
54 function Page() {
55 const state = useSuspenseInfiniteQuery(queryOpts)
56β
57 return (
58 <div>
59 {state.data.pages.map((page) => (
60 <div key={page}>data: {page}</div>
61 ))}
62 <button onClick={() => state.fetchNextPage()}>Next Page</button>
63 </div>
64 )
65 }
66β
67 function App() {
68 usePrefetchInfiniteQuery({ ...queryOpts, pages: data.length })
69β
70 return (
71 <Suspense fallback={<Fallback />}>
72 <Page />
73 </Suspense>
74 )
75 }
76β
77 const rendered = renderWithClient(queryClient, <App />)
78β
79 await vi.advanceTimersByTimeAsync(30)
80 rendered.getByText('data: Do you fetch on render?')
81β
82 fireEvent.click(rendered.getByText('Next Page'))
83 expect(
84 rendered.getByText('data: Or do you render as you fetch?'),
85 ).toBeInTheDocument()
86β
87 fireEvent.click(rendered.getByText('Next Page'))
88 expect(
89 rendered.getByText('data: Either way, Tanstack Query helps you!'),
90 ).toBeInTheDocument()
91 expect(Fallback).toHaveBeenCalledTimes(1)
92 expect(queryOpts.queryFn).toHaveBeenCalledTimes(3)
93 })
94β
95 it('should not display fallback if the query cache is already populated', async () => {
96 const data = [
97 'Prefetch rocks!',
98 'No waterfalls, boy!',
99 'Tanstack Query #ftw',
100 ]
101β
102 const queryOpts = {
103 queryKey: createKey(),
104 queryFn: vi
105 .fn<(context: { pageParam: number }) => Promise<string>>()
106 .mockImplementation(({ pageParam }) =>
107 sleep(10).then(() => data[pageParam]!),
108 ),
109 initialPageParam: 0,
110 getNextPageParam: (_lastPage: string, allPages: Array<string>) =>
111 allPages.length < data.length ? allPages.length : undefined,
112 }
113β
114 void queryClient.infiniteQuery({ ...queryOpts, pages: 3 }).catch(noop)
115 await vi.advanceTimersByTimeAsync(30)
116 queryOpts.queryFn.mockClear()
117β
118 function Page() {
119 const state = useSuspenseInfiniteQuery(queryOpts)
120β
121 return (
122 <div>
123 {state.data.pages.map((page) => (
124 <div key={page}>data: {page}</div>
125 ))}
126 <button onClick={() => state.fetchNextPage()}>Next Page</button>
127 </div>
128 )
129 }
130β
131 function App() {
132 usePrefetchInfiniteQuery(queryOpts)
133β
134 return (
135 <Suspense fallback={<Fallback />}>
136 <Page />
137 </Suspense>
138 )
139 }
140β
141 const rendered = renderWithClient(queryClient, <App />)
142β
143 expect(rendered.getByText('data: Prefetch rocks!')).toBeInTheDocument()
144β
145 fireEvent.click(rendered.getByText('Next Page'))
146 expect(rendered.getByText('data: No waterfalls, boy!')).toBeInTheDocument()
147β
148 fireEvent.click(rendered.getByText('Next Page'))
149 expect(rendered.getByText('data: Tanstack Query #ftw')).toBeInTheDocument()
150 expect(queryOpts.queryFn).not.toHaveBeenCalled()
151 expect(Fallback).not.toHaveBeenCalled()
152 })
153β
154 it('should not create an endless loop when using inside a suspense boundary', async () => {
155 const data = ['Infinite Page 1', 'Infinite Page 2', 'Infinite Page 3']
156β
157 const queryOpts = {
158 queryKey: createKey(),
159 queryFn: vi
160 .fn<(context: { pageParam: number }) => Promise<string>>()
161 .mockImplementation(({ pageParam }) =>
162 sleep(10).then(() => data[pageParam]!),
163 ),
164 initialPageParam: 0,
165 // always reports another page available, to guard against an endless
166 // auto-advance loop rather than a bounded pagination sequence
167 getNextPageParam: (_lastPage: string, allPages: Array<string>) =>
168 allPages.length,
169 }
170β
171 function Prefetch({ children }: { children: VNode }) {
172 usePrefetchInfiniteQuery(queryOpts)
173 return <>{children}</>
174 }
175β
176 function Page() {
177 const state = useSuspenseInfiniteQuery(queryOpts)
178β
179 return (
180 <div>
181 {state.data.pages.map((page) => (
182 <div key={page}>data: {page}</div>
183 ))}
184 <button onClick={() => state.fetchNextPage()}>Next Page</button>
185 </div>
186 )
187 }
188β
189 function App() {
190 return (
191 <Suspense fallback={<></>}>
192 <Prefetch>
193 <Page />
194 </Prefetch>
195 </Suspense>
196 )
197 }
198β
199 const rendered = renderWithClient(queryClient, <App />)
200β
201 await vi.advanceTimersByTimeAsync(10)
202 rendered.getByText('data: Infinite Page 1')
203β
204 fireEvent.click(rendered.getByText('Next Page'))
205 await vi.advanceTimersByTimeAsync(11)
206 expect(rendered.getByText('data: Infinite Page 2')).toBeInTheDocument()
207β
208 fireEvent.click(rendered.getByText('Next Page'))
209 await vi.advanceTimersByTimeAsync(11)
210 expect(rendered.getByText('data: Infinite Page 3')).toBeInTheDocument()
211 expect(queryOpts.queryFn).toHaveBeenCalledTimes(3)
212 })
213})