Skip to content

Commit 878ac34

Browse files
committed
test(react-query): add tests should retry on mount when throwOnError returns false
1 parent 7d370b9 commit 878ac34

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
@@ -5920,6 +5920,7 @@ describe('useQuery', () => {
59205920
it('should be able to toggle subscribed', async () => {
59215921
const key = queryKey()
59225922
const queryFn = vi.fn(() => Promise.resolve('data'))
5923+
59235924
function Page() {
59245925
const [subscribed, setSubscribed] = React.useState(true)
59255926
const { data } = useQuery({
@@ -5964,6 +5965,7 @@ describe('useQuery', () => {
59645965
it('should not be attached to the query when subscribed is false', async () => {
59655966
const key = queryKey()
59665967
const queryFn = vi.fn(() => Promise.resolve('data'))
5968+
59675969
function Page() {
59685970
const { data } = useQuery({
59695971
queryKey: key,
@@ -5992,6 +5994,7 @@ describe('useQuery', () => {
59925994
it('should not re-render when data is added to the cache when subscribed is false', async () => {
59935995
const key = queryKey()
59945996
let renders = 0
5997+
59955998
function Page() {
59965999
const { data } = useQuery({
59976000
queryKey: key,
@@ -6191,6 +6194,7 @@ describe('useQuery', () => {
61916194
await sleep(5)
61926195
return { numbers: { current: { id } } }
61936196
}
6197+
61946198
function Test() {
61956199
const [id, setId] = React.useState(1)
61966200

@@ -6256,6 +6260,7 @@ describe('useQuery', () => {
62566260
await sleep(5)
62576261
return { numbers: { current: { id } } }
62586262
}
6263+
62596264
function Test() {
62606265
const [id, setId] = React.useState(1)
62616266

@@ -6761,10 +6766,12 @@ describe('useQuery', () => {
67616766
it('should console.error when there is no queryFn', () => {
67626767
const consoleErrorMock = vi.spyOn(console, 'error')
67636768
const key = queryKey()
6769+
67646770
function Example() {
67656771
useQuery({ queryKey: key })
67666772
return <></>
67676773
}
6774+
67686775
renderWithClient(queryClient, <Example />)
67696776

67706777
expect(consoleErrorMock).toHaveBeenCalledTimes(1)
@@ -6774,4 +6781,56 @@ describe('useQuery', () => {
67746781

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

0 commit comments

Comments
 (0)