Open History to see the last commit
1// useInfiniteQuery under the 2.0 read layer: same entry, same async data
2// node β first load suspends, page fetches hold the committed pages (no
3// fallback), and the pager surface (fetchNextPage / hasNextPage /
4// isFetchingNextPage) derives from cache state plus the adapter's mirrored
5// page-boundary checks.
6import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
7import { Loading } from 'solid-js'
8import { createKey, sleep } from '@tanstack/query-test-utils'
9import { QueryCache, QueryClient, useInfiniteQuery } from '..'
10import { renderWithClient } from './utils'
11β
12describe('useInfiniteQuery 2.0 read semantics', () => {
13 let queryCache: QueryCache
14 let queryClient: QueryClient
15β
16 beforeEach(() => {
17 vi.useFakeTimers()
18 queryCache = new QueryCache()
19 queryClient = new QueryClient({ queryCache })
20 })
21β
22 afterEach(() => {
23 queryClient.clear()
24 vi.useRealTimers()
25 })
26β
27 function pagedQuery(key: ReadonlyArray<unknown>, lastPage = 3) {
28 return {
29 queryKey: key,
30 queryFn: ({ pageParam }: { pageParam: number }) =>
31 sleep(10).then(() => `page-${pageParam}`),
32 initialPageParam: 0,
33 getNextPageParam: (_last: string, pages: Array<string>) =>
34 pages.length < lastPage ? pages.length : undefined,
35 getPreviousPageParam: (first: string) => {
36 const current = Number(first.split('-')[1])
37 return current > 0 ? current - 1 : undefined
38 },
39 }
40 }
41β
42 it('suspends on first load, then renders the first page', async () => {
43 const key = createKey()
44 function Page() {
45 const query = useInfiniteQuery(() => pagedQuery(key))
46 return (
47 <div>
48 <span>pages: {query.data.pages.join(',')}</span>
49 <span>hasNext: {String(query.hasNextPage)}</span>
50 </div>
51 )
52 }
53β
54 const rendered = renderWithClient(queryClient, () => (
55 <Loading fallback={<span>loading</span>}>
56 <Page />
57 </Loading>
58 ))
59 expect(rendered.getByText('loading')).toBeInTheDocument()
60β
61 await vi.advanceTimersByTimeAsync(10)
62 expect(rendered.getByText('pages: page-0')).toBeInTheDocument()
63 expect(rendered.getByText('hasNext: true')).toBeInTheDocument()
64 })
65β
66 it('fetchNextPage appends while committed pages stay visible, with observable direction', async () => {
67 const key = createKey()
68 function Page() {
69 const query = useInfiniteQuery(() => pagedQuery(key))
70 return (
71 <div>
72 <span>pages: {query.data.pages.join(',')}</span>
73 <span>
74 fetchingNext: {String(query.isFetchingNextPage)}, refetching:{' '}
75 {String(query.isRefetching)}
76 </span>
77 <button onClick={() => void query.fetchNextPage()}>more</button>
78 </div>
79 )
80 }
81β
82 const rendered = renderWithClient(queryClient, () => (
83 <Loading fallback={<span>loading</span>}>
84 <Page />
85 </Loading>
86 ))
87 await vi.advanceTimersByTimeAsync(10)
88 expect(rendered.getByText('pages: page-0')).toBeInTheDocument()
89β
90 rendered.getByRole('button').click()
91 await vi.advanceTimersByTimeAsync(0)
92 // Page fetch in flight: committed page still on screen (no fallback),
93 // direction observable, and NOT reported as a plain refetch.
94 expect(rendered.getByText('pages: page-0')).toBeInTheDocument()
95 expect(
96 rendered.getByText('fetchingNext: true, refetching: false'),
97 ).toBeInTheDocument()
98β
99 await vi.advanceTimersByTimeAsync(10)
100 expect(rendered.getByText('pages: page-0,page-1')).toBeInTheDocument()
101 expect(
102 rendered.getByText('fetchingNext: false, refetching: false'),
103 ).toBeInTheDocument()
104 })
105β
106 it('hasNextPage turns false at the boundary', async () => {
107 const key = createKey()
108 function Page() {
109 const query = useInfiniteQuery(() => pagedQuery(key, 2))
110 return (
111 <div>
112 <span>pages: {query.data.pages.join(',')}</span>
113 <span>hasNext: {String(query.hasNextPage)}</span>
114 <button onClick={() => void query.fetchNextPage()}>more</button>
115 </div>
116 )
117 }
118β
119 const rendered = renderWithClient(queryClient, () => (
120 <Loading fallback={<span>loading</span>}>
121 <Page />
122 </Loading>
123 ))
124 await vi.advanceTimersByTimeAsync(10)
125 expect(rendered.getByText('hasNext: true')).toBeInTheDocument()
126β
127 rendered.getByRole('button').click()
128 await vi.advanceTimersByTimeAsync(10)
129 expect(rendered.getByText('pages: page-0,page-1')).toBeInTheDocument()
130 expect(rendered.getByText('hasNext: false')).toBeInTheDocument()
131 })
132β
133 it('fetchPreviousPage prepends', async () => {
134 const key = createKey()
135 function Page() {
136 const query = useInfiniteQuery(() => ({
137 ...pagedQuery(key),
138 initialPageParam: 2,
139 }))
140 return (
141 <div>
142 <span>pages: {query.data.pages.join(',')}</span>
143 <span>hasPrev: {String(query.hasPreviousPage)}</span>
144 <button onClick={() => void query.fetchPreviousPage()}>prev</button>
145 </div>
146 )
147 }
148β
149 const rendered = renderWithClient(queryClient, () => (
150 <Loading fallback={<span>loading</span>}>
151 <Page />
152 </Loading>
153 ))
154 await vi.advanceTimersByTimeAsync(10)
155 expect(rendered.getByText('pages: page-2')).toBeInTheDocument()
156 expect(rendered.getByText('hasPrev: true')).toBeInTheDocument()
157β
158 rendered.getByRole('button').click()
159 await vi.advanceTimersByTimeAsync(10)
160 expect(rendered.getByText('pages: page-1,page-2')).toBeInTheDocument()
161 })
162β
163 it('full refetch reports isRefetching, not a page fetch', async () => {
164 const key = createKey()
165 function Page() {
166 const query = useInfiniteQuery(() => pagedQuery(key))
167 return (
168 <div>
169 <span>pages: {query.data.pages.join(',')}</span>
170 <span>
171 fetchingNext: {String(query.isFetchingNextPage)}, refetching:{' '}
172 {String(query.isRefetching)}
173 </span>
174 <button onClick={() => void query.refetch()}>refetch</button>
175 </div>
176 )
177 }
178β
179 const rendered = renderWithClient(queryClient, () => (
180 <Loading fallback={<span>loading</span>}>
181 <Page />
182 </Loading>
183 ))
184 await vi.advanceTimersByTimeAsync(10)
185β
186 rendered.getByRole('button').click()
187 await vi.advanceTimersByTimeAsync(0)
188 expect(
189 rendered.getByText('fetchingNext: false, refetching: true'),
190 ).toBeInTheDocument()
191β
192 await vi.advanceTimersByTimeAsync(10)
193 expect(
194 rendered.getByText('fetchingNext: false, refetching: false'),
195 ).toBeInTheDocument()
196 expect(rendered.getByText('pages: page-0')).toBeInTheDocument()
197 })
198})