Skip to content

Commit 08a4d2b

Browse files
authored
test(query-core/queryObserver): add test for skipping refetch when 'retryOnMount' is false and query is in error state (#10518)
1 parent 430e988 commit 08a4d2b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

packages/query-core/src/__tests__/queryObserver.test.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,42 @@ describe('queryObserver', () => {
14021402
unsubscribe()
14031403
})
14041404

1405+
test('should not refetch on mount when retryOnMount is false and query is in error state', async () => {
1406+
const key = queryKey()
1407+
const queryFn = vi.fn(() => Promise.reject('error'))
1408+
1409+
// First observer causes query to fail
1410+
const firstObserver = new QueryObserver(queryClient, {
1411+
queryKey: key,
1412+
queryFn,
1413+
retry: false,
1414+
})
1415+
const unsubscribeFirst = firstObserver.subscribe(vi.fn())
1416+
1417+
await vi.advanceTimersByTimeAsync(0)
1418+
1419+
expect(queryFn).toHaveBeenCalledTimes(1)
1420+
expect(queryClient.getQueryState(key)?.status).toBe('error')
1421+
1422+
unsubscribeFirst()
1423+
1424+
// New observer with retryOnMount: false should not refetch
1425+
const secondObserver = new QueryObserver(queryClient, {
1426+
queryKey: key,
1427+
queryFn,
1428+
retry: false,
1429+
retryOnMount: false,
1430+
})
1431+
const unsubscribeSecond = secondObserver.subscribe(vi.fn())
1432+
1433+
await vi.advanceTimersByTimeAsync(0)
1434+
1435+
// queryFn should still have been called only once (no refetch)
1436+
expect(queryFn).toHaveBeenCalledTimes(1)
1437+
1438+
unsubscribeSecond()
1439+
})
1440+
14051441
test('should reject promise when experimental_prefetchInRender is disabled and thenable is pending', async () => {
14061442
const key = queryKey()
14071443
const queryClient2 = new QueryClient({

0 commit comments

Comments
 (0)