Skip to content

Commit bf3034c

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

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
@@ -6027,6 +6027,7 @@ describe('useQuery', () => {
60276027
it('should be able to toggle subscribed', async () => {
60286028
const key = queryKey()
60296029
const queryFn = vi.fn(() => Promise.resolve('data'))
6030+
60306031
function Page() {
60316032
const [subscribed, setSubscribed] = React.useState(true)
60326033
const { data } = useQuery({
@@ -6069,6 +6070,7 @@ describe('useQuery', () => {
60696070
it('should not be attached to the query when subscribed is false', async () => {
60706071
const key = queryKey()
60716072
const queryFn = vi.fn(() => Promise.resolve('data'))
6073+
60726074
function Page() {
60736075
const { data } = useQuery({
60746076
queryKey: key,
@@ -6095,6 +6097,7 @@ describe('useQuery', () => {
60956097
it('should not re-render when data is added to the cache when subscribed is false', async () => {
60966098
const key = queryKey()
60976099
let renders = 0
6100+
60986101
function Page() {
60996102
const { data } = useQuery({
61006103
queryKey: key,
@@ -6297,6 +6300,7 @@ describe('useQuery', () => {
62976300
await sleep(5)
62986301
return { numbers: { current: { id } } }
62996302
}
6303+
63006304
function Test() {
63016305
const [id, setId] = React.useState(1)
63026306

@@ -6357,6 +6361,7 @@ describe('useQuery', () => {
63576361
await sleep(5)
63586362
return { numbers: { current: { id } } }
63596363
}
6364+
63606365
function Test() {
63616366
const [id, setId] = React.useState(1)
63626367

@@ -6854,10 +6859,12 @@ describe('useQuery', () => {
68546859
it('should console.error when there is no queryFn', () => {
68556860
const consoleErrorMock = vi.spyOn(console, 'error')
68566861
const key = queryKey()
6862+
68576863
function Example() {
68586864
useQuery({ queryKey: key })
68596865
return <></>
68606866
}
6867+
68616868
renderWithClient(queryClient, <Example />)
68626869

68636870
expect(consoleErrorMock).toHaveBeenCalledTimes(1)
@@ -6867,4 +6874,56 @@ describe('useQuery', () => {
68676874

68686875
consoleErrorMock.mockRestore()
68696876
})
6877+
6878+
it('should retry on mount when throwOnError returns false', async () => {
6879+
const key = queryKey()
6880+
let fetchCount = 0
6881+
const queryFn = vi.fn().mockImplementation(() => {
6882+
fetchCount++
6883+
console.log(`Fetching... (attempt ${fetchCount})`)
6884+
return Promise.reject(new Error('Simulated 500 error'))
6885+
})
6886+
6887+
function Component() {
6888+
const { status, error } = useQuery({
6889+
queryKey: key,
6890+
queryFn,
6891+
throwOnError: () => false,
6892+
retryOnMount: true,
6893+
staleTime: Infinity,
6894+
retry: false,
6895+
})
6896+
6897+
return (
6898+
<div>
6899+
<div data-testid="status">{status}</div>
6900+
{error && <div data-testid="error">{error.message}</div>}
6901+
</div>
6902+
)
6903+
}
6904+
6905+
const { unmount, getByTestId } = renderWithClient(
6906+
queryClient,
6907+
<Component />,
6908+
)
6909+
6910+
await vi.waitFor(() =>
6911+
expect(getByTestId('status')).toHaveTextContent('error'),
6912+
)
6913+
expect(getByTestId('error')).toHaveTextContent('Simulated 500 error')
6914+
expect(fetchCount).toBe(1)
6915+
6916+
unmount()
6917+
6918+
const initialFetchCount = fetchCount
6919+
6920+
renderWithClient(queryClient, <Component />)
6921+
6922+
await vi.waitFor(() =>
6923+
expect(getByTestId('status')).toHaveTextContent('error'),
6924+
)
6925+
6926+
expect(fetchCount).toBe(initialFetchCount + 1)
6927+
expect(queryFn).toHaveBeenCalledTimes(2)
6928+
})
68706929
})

0 commit comments

Comments
 (0)