Skip to content

Commit 8fa5915

Browse files
authored
fix(react-query-devtools): inform onlineManager when offline mode is simulated (#5210)
1 parent 79b6a5d commit 8fa5915

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react'
22
import { fireEvent, screen, waitFor, act } from '@testing-library/react'
33
import '@testing-library/jest-dom'
4-
import { useQuery } from '@tanstack/react-query'
4+
import { useQuery, onlineManager } from '@tanstack/react-query'
55
import { defaultPanelSize, sortFns } from '../utils'
66
import {
77
getByTextContent,
@@ -478,6 +478,31 @@ describe('ReactQueryDevtools', () => {
478478
expect(count).toBe(2)
479479
})
480480

481+
it('should inform onlineManager when offline mode is simulated', () => {
482+
const { queryClient } = createQueryClient()
483+
const online: Array<boolean> = []
484+
485+
const unsubscribe = onlineManager.subscribe(() => {
486+
online.push(onlineManager.isOnline())
487+
})
488+
489+
renderWithClient(queryClient, <div />, {
490+
initialIsOpen: true,
491+
})
492+
493+
fireEvent.click(
494+
screen.getByRole('button', { name: /mock offline behavior/i }),
495+
)
496+
497+
fireEvent.click(
498+
screen.getByRole('button', { name: /restore offline mock/i }),
499+
)
500+
501+
expect(online).toStrictEqual([false, true])
502+
503+
unsubscribe()
504+
})
505+
481506
it('should sort the queries according to the sorting filter', async () => {
482507
const { queryClient, queryCache } = createQueryClient()
483508

packages/react-query-devtools/src/devtools.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ export const ReactQueryDevtoolsPanel = React.forwardRef<
689689
} else {
690690
onlineManager.setOnline(false)
691691
setMockOffline(true)
692+
window.dispatchEvent(new Event('offline'))
692693
}
693694
}}
694695
aria-label={

0 commit comments

Comments
 (0)