Skip to content

Commit 07bc820

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 52c2092 commit 07bc820

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
@@ -6832,4 +6832,130 @@ describe('useQuery', () => {
68326832
expect(fetchCount).toBe(initialFetchCount + 1)
68336833
expect(queryFn).toHaveBeenCalledTimes(2)
68346834
})
6835+
6836+
it('should not retry on mount when throwOnError function returns true', async () => {
6837+
const key = queryKey()
6838+
let fetchCount = 0
6839+
const queryFn = vi.fn().mockImplementation(() => {
6840+
fetchCount++
6841+
console.log(`Fetching... (attempt ${fetchCount})`)
6842+
return Promise.reject(new Error('Simulated 500 error'))
6843+
})
6844+
6845+
function Component() {
6846+
const { status, error } = useQuery({
6847+
queryKey: key,
6848+
queryFn,
6849+
throwOnError: () => true,
6850+
retryOnMount: true,
6851+
staleTime: Infinity,
6852+
retry: false,
6853+
})
6854+
6855+
return (
6856+
<div>
6857+
<div data-testid="status">{status}</div>
6858+
{error && <div data-testid="error">{error.message}</div>}
6859+
</div>
6860+
)
6861+
}
6862+
6863+
const { unmount, getByTestId } = renderWithClient(
6864+
queryClient,
6865+
<ErrorBoundary
6866+
fallbackRender={({ error }) => (
6867+
<div>
6868+
<div data-testid="status">error</div>
6869+
<div data-testid="error">{error?.message}</div>
6870+
</div>
6871+
)}
6872+
>
6873+
<Component />
6874+
</ErrorBoundary>,
6875+
)
6876+
6877+
await vi.waitFor(() =>
6878+
expect(getByTestId('status')).toHaveTextContent('error'),
6879+
)
6880+
expect(getByTestId('error')).toHaveTextContent('Simulated 500 error')
6881+
expect(fetchCount).toBe(1)
6882+
6883+
unmount()
6884+
6885+
const initialFetchCount = fetchCount
6886+
6887+
renderWithClient(queryClient,
6888+
<ErrorBoundary
6889+
fallbackRender={({ error }) => (
6890+
<div>
6891+
<div data-testid="status">error</div>
6892+
<div data-testid="error">{error?.message}</div>
6893+
</div>
6894+
)}
6895+
>
6896+
<Component />
6897+
</ErrorBoundary>
6898+
)
6899+
6900+
await vi.waitFor(() =>
6901+
expect(getByTestId('status')).toHaveTextContent('error'),
6902+
)
6903+
6904+
// Should not retry because throwOnError returns true
6905+
expect(fetchCount).toBe(initialFetchCount)
6906+
expect(queryFn).toHaveBeenCalledTimes(1)
6907+
})
6908+
6909+
it('should handle throwOnError function based on actual error state', async () => {
6910+
const key = queryKey()
6911+
let fetchCount = 0
6912+
const queryFn = vi.fn().mockImplementation(() => {
6913+
fetchCount++
6914+
console.log(`Fetching... (attempt ${fetchCount})`)
6915+
return Promise.reject(new Error('Simulated 500 error'))
6916+
})
6917+
6918+
function Component() {
6919+
const { status, error } = useQuery({
6920+
queryKey: key,
6921+
queryFn,
6922+
throwOnError: (error) => error.message.includes('404'),
6923+
retryOnMount: true,
6924+
staleTime: Infinity,
6925+
retry: false,
6926+
})
6927+
6928+
return (
6929+
<div>
6930+
<div data-testid="status">{status}</div>
6931+
{error && <div data-testid="error">{error.message}</div>}
6932+
</div>
6933+
)
6934+
}
6935+
6936+
const { unmount, getByTestId } = renderWithClient(
6937+
queryClient,
6938+
<Component />,
6939+
)
6940+
6941+
await vi.waitFor(() =>
6942+
expect(getByTestId('status')).toHaveTextContent('error'),
6943+
)
6944+
expect(getByTestId('error')).toHaveTextContent('Simulated 500 error')
6945+
expect(fetchCount).toBe(1)
6946+
6947+
unmount()
6948+
6949+
const initialFetchCount = fetchCount
6950+
6951+
renderWithClient(queryClient, <Component />)
6952+
6953+
await vi.waitFor(() =>
6954+
expect(getByTestId('status')).toHaveTextContent('error'),
6955+
)
6956+
6957+
// Should retry because throwOnError returns false (500 error doesn't include '404')
6958+
expect(fetchCount).toBe(initialFetchCount + 1)
6959+
expect(queryFn).toHaveBeenCalledTimes(2)
6960+
})
68356961
})

0 commit comments

Comments
 (0)