Skip to content

Commit 182b239

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 4c40790 commit 182b239

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

0 commit comments

Comments
 (0)