|
1 | 1 | import { render, waitFor, fireEvent } from '@testing-library/react' |
2 | 2 | import * as React from 'react' |
3 | 3 |
|
4 | | -import { sleep, queryKey, waitForMs } from './utils' |
| 4 | +import { sleep, queryKey, waitForMs, mockConsoleError } from './utils' |
5 | 5 | import { useInfiniteQuery, useQueryCache } from '..' |
6 | 6 | import { InfiniteQueryResult } from '../../core' |
7 | 7 |
|
@@ -112,6 +112,48 @@ describe('useInfiniteQuery', () => { |
112 | 112 | }) |
113 | 113 | }) |
114 | 114 |
|
| 115 | + it('should not throw when fetchMore returns an error', async () => { |
| 116 | + const consoleMock = mockConsoleError() |
| 117 | + const key = queryKey() |
| 118 | + let noThrow: boolean |
| 119 | + |
| 120 | + function Page() { |
| 121 | + const start = 1 |
| 122 | + const state = useInfiniteQuery( |
| 123 | + key, |
| 124 | + async (_key, page: number = start) => { |
| 125 | + if (page === 2) { |
| 126 | + throw new Error('error') |
| 127 | + } |
| 128 | + return page |
| 129 | + }, |
| 130 | + { |
| 131 | + retry: 1, |
| 132 | + retryDelay: 10, |
| 133 | + getFetchMore: (lastPage, _pages) => lastPage + 1, |
| 134 | + } |
| 135 | + ) |
| 136 | + |
| 137 | + const { fetchMore } = state |
| 138 | + |
| 139 | + React.useEffect(() => { |
| 140 | + setTimeout(async () => { |
| 141 | + try { |
| 142 | + await fetchMore() |
| 143 | + noThrow = true |
| 144 | + } catch (error) {} |
| 145 | + }, 20) |
| 146 | + }, [fetchMore]) |
| 147 | + |
| 148 | + return null |
| 149 | + } |
| 150 | + |
| 151 | + render(<Page />) |
| 152 | + |
| 153 | + await waitFor(() => expect(noThrow).toBe(true)) |
| 154 | + consoleMock.mockRestore() |
| 155 | + }) |
| 156 | + |
115 | 157 | it('should keep the previous data when keepPreviousData is set', async () => { |
116 | 158 | const key = queryKey() |
117 | 159 | const states: InfiniteQueryResult<string>[] = [] |
|
0 commit comments