Skip to content

Commit f7de073

Browse files
committed
test(react-query): add tests should retry on mount when throwOnError returns false
1 parent 763cee3 commit f7de073

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5919,6 +5919,7 @@ describe('useQuery', () => {
59195919
it('should be able to toggle subscribed', async () => {
59205920
const key = queryKey()
59215921
const queryFn = vi.fn(() => Promise.resolve('data'))
5922+
59225923
function Page() {
59235924
const [subscribed, setSubscribed] = React.useState(true)
59245925
const { data } = useQuery({
@@ -5963,6 +5964,7 @@ describe('useQuery', () => {
59635964
it('should not be attached to the query when subscribed is false', async () => {
59645965
const key = queryKey()
59655966
const queryFn = vi.fn(() => Promise.resolve('data'))
5967+
59665968
function Page() {
59675969
const { data } = useQuery({
59685970
queryKey: key,
@@ -5991,6 +5993,7 @@ describe('useQuery', () => {
59915993
it('should not re-render when data is added to the cache when subscribed is false', async () => {
59925994
const key = queryKey()
59935995
let renders = 0
5996+
59945997
function Page() {
59955998
const { data } = useQuery({
59965999
queryKey: key,
@@ -6190,6 +6193,7 @@ describe('useQuery', () => {
61906193
await sleep(5)
61916194
return { numbers: { current: { id } } }
61926195
}
6196+
61936197
function Test() {
61946198
const [id, setId] = React.useState(1)
61956199

@@ -6255,6 +6259,7 @@ describe('useQuery', () => {
62556259
await sleep(5)
62566260
return { numbers: { current: { id } } }
62576261
}
6262+
62586263
function Test() {
62596264
const [id, setId] = React.useState(1)
62606265

@@ -6760,10 +6765,12 @@ describe('useQuery', () => {
67606765
it('should console.error when there is no queryFn', () => {
67616766
const consoleErrorMock = vi.spyOn(console, 'error')
67626767
const key = queryKey()
6768+
67636769
function Example() {
67646770
useQuery({ queryKey: key })
67656771
return <></>
67666772
}
6773+
67676774
renderWithClient(queryClient, <Example />)
67686775

67696776
expect(consoleErrorMock).toHaveBeenCalledTimes(1)
@@ -6773,4 +6780,56 @@ describe('useQuery', () => {
67736780

67746781
consoleErrorMock.mockRestore()
67756782
})
6783+
6784+
it('should retry on mount when throwOnError returns false', async () => {
6785+
const key = queryKey()
6786+
let fetchCount = 0
6787+
const queryFn = vi.fn().mockImplementation(() => {
6788+
fetchCount++
6789+
console.log(`Fetching... (attempt ${fetchCount})`)
6790+
return Promise.reject(new Error('Simulated 500 error'))
6791+
})
6792+
6793+
function Component() {
6794+
const { status, error } = useQuery({
6795+
queryKey: key,
6796+
queryFn,
6797+
throwOnError: () => false,
6798+
retryOnMount: true,
6799+
staleTime: Infinity,
6800+
retry: false,
6801+
})
6802+
6803+
return (
6804+
<div>
6805+
<div data-testid="status">{status}</div>
6806+
{error && <div data-testid="error">{error.message}</div>}
6807+
</div>
6808+
)
6809+
}
6810+
6811+
const { unmount, getByTestId } = renderWithClient(
6812+
queryClient,
6813+
<Component />,
6814+
)
6815+
6816+
await vi.waitFor(() =>
6817+
expect(getByTestId('status')).toHaveTextContent('error'),
6818+
)
6819+
expect(getByTestId('error')).toHaveTextContent('Simulated 500 error')
6820+
expect(fetchCount).toBe(1)
6821+
6822+
unmount()
6823+
6824+
const initialFetchCount = fetchCount
6825+
6826+
renderWithClient(queryClient, <Component />)
6827+
6828+
await vi.waitFor(() =>
6829+
expect(getByTestId('status')).toHaveTextContent('error'),
6830+
)
6831+
6832+
expect(fetchCount).toBe(initialFetchCount + 1)
6833+
expect(queryFn).toHaveBeenCalledTimes(2)
6834+
})
67766835
})

0 commit comments

Comments
 (0)