Skip to content

Commit bbc9723

Browse files
committed
test: Add tests for throwOnError behavior in useQuery
This commit introduces tests to verify the behavior of the `throwOnError` callback in scenarios where `retryOnMount` is enabled. It ensures proper handling of retries based on the error state and the `throwOnError` function's return value. These tests improve the reliability and coverage of error handling logic in `useQuery`.
1 parent b79395d commit bbc9723

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

packages/react-query/src/__tests__/useQuery.test.tsx

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6926,4 +6926,130 @@ describe('useQuery', () => {
69266926
expect(fetchCount).toBe(initialFetchCount + 1)
69276927
expect(queryFn).toHaveBeenCalledTimes(2)
69286928
})
6929+
6930+
it('should not retry on mount when throwOnError function returns true', async () => {
6931+
const key = queryKey()
6932+
let fetchCount = 0
6933+
const queryFn = vi.fn().mockImplementation(() => {
6934+
fetchCount++
6935+
console.log(`Fetching... (attempt ${fetchCount})`)
6936+
return Promise.reject(new Error('Simulated 500 error'))
6937+
})
6938+
6939+
function Component() {
6940+
const { status, error } = useQuery({
6941+
queryKey: key,
6942+
queryFn,
6943+
throwOnError: () => true,
6944+
retryOnMount: true,
6945+
staleTime: Infinity,
6946+
retry: false,
6947+
})
6948+
6949+
return (
6950+
<div>
6951+
<div data-testid="status">{status}</div>
6952+
{error && <div data-testid="error">{error.message}</div>}
6953+
</div>
6954+
)
6955+
}
6956+
6957+
const { unmount, getByTestId } = renderWithClient(
6958+
queryClient,
6959+
<ErrorBoundary
6960+
fallbackRender={({ error }) => (
6961+
<div>
6962+
<div data-testid="status">error</div>
6963+
<div data-testid="error">{error?.message}</div>
6964+
</div>
6965+
)}
6966+
>
6967+
<Component />
6968+
</ErrorBoundary>,
6969+
)
6970+
6971+
await vi.waitFor(() =>
6972+
expect(getByTestId('status')).toHaveTextContent('error'),
6973+
)
6974+
expect(getByTestId('error')).toHaveTextContent('Simulated 500 error')
6975+
expect(fetchCount).toBe(1)
6976+
6977+
unmount()
6978+
6979+
const initialFetchCount = fetchCount
6980+
6981+
renderWithClient(queryClient,
6982+
<ErrorBoundary
6983+
fallbackRender={({ error }) => (
6984+
<div>
6985+
<div data-testid="status">error</div>
6986+
<div data-testid="error">{error?.message}</div>
6987+
</div>
6988+
)}
6989+
>
6990+
<Component />
6991+
</ErrorBoundary>
6992+
)
6993+
6994+
await vi.waitFor(() =>
6995+
expect(getByTestId('status')).toHaveTextContent('error'),
6996+
)
6997+
6998+
// Should not retry because throwOnError returns true
6999+
expect(fetchCount).toBe(initialFetchCount)
7000+
expect(queryFn).toHaveBeenCalledTimes(1)
7001+
})
7002+
7003+
it('should handle throwOnError function based on actual error state', async () => {
7004+
const key = queryKey()
7005+
let fetchCount = 0
7006+
const queryFn = vi.fn().mockImplementation(() => {
7007+
fetchCount++
7008+
console.log(`Fetching... (attempt ${fetchCount})`)
7009+
return Promise.reject(new Error('Simulated 500 error'))
7010+
})
7011+
7012+
function Component() {
7013+
const { status, error } = useQuery({
7014+
queryKey: key,
7015+
queryFn,
7016+
throwOnError: (error) => error.message.includes('404'),
7017+
retryOnMount: true,
7018+
staleTime: Infinity,
7019+
retry: false,
7020+
})
7021+
7022+
return (
7023+
<div>
7024+
<div data-testid="status">{status}</div>
7025+
{error && <div data-testid="error">{error.message}</div>}
7026+
</div>
7027+
)
7028+
}
7029+
7030+
const { unmount, getByTestId } = renderWithClient(
7031+
queryClient,
7032+
<Component />,
7033+
)
7034+
7035+
await vi.waitFor(() =>
7036+
expect(getByTestId('status')).toHaveTextContent('error'),
7037+
)
7038+
expect(getByTestId('error')).toHaveTextContent('Simulated 500 error')
7039+
expect(fetchCount).toBe(1)
7040+
7041+
unmount()
7042+
7043+
const initialFetchCount = fetchCount
7044+
7045+
renderWithClient(queryClient, <Component />)
7046+
7047+
await vi.waitFor(() =>
7048+
expect(getByTestId('status')).toHaveTextContent('error'),
7049+
)
7050+
7051+
// Should retry because throwOnError returns false (500 error doesn't include '404')
7052+
expect(fetchCount).toBe(initialFetchCount + 1)
7053+
expect(queryFn).toHaveBeenCalledTimes(2)
7054+
})
69297055
})

0 commit comments

Comments
 (0)