Files
hapi/web/src/hooks/useReconnectingState.test.ts
T
Haoqing WangandGitHub 079556b43a fix(web): stop flashing the reconnecting banner on self-healing blips (#1230)
EventSource reports an error the moment a connection drops, including
while it is already retrying by itself - readyState is still CONNECTING
and the stream is typically back within a few seconds. useSSE forwards
that straight to onDisconnect, and App turned it into a full-width
"reconnecting" banner synchronously, so a blip the browser recovered from
on its own still read as a broken network.

Route the disconnect through a small hook that waits out a grace period
first. Genuine outages still surface the banner, just a moment later;
recoveries that beat the timer stay silent. The sibling syncing banner
already debounces this way in useSyncingState.

The grace period is anchored to the first drop, so repeated failed
retries cannot push the banner back indefinitely.
2026-08-01 23:25:40 +08:00

92 lines
3.2 KiB
TypeScript

import { act, renderHook } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { RECONNECTING_BANNER_DELAY_MS, useReconnectingState } from './useReconnectingState'
describe('useReconnectingState', () => {
beforeEach(() => {
vi.useFakeTimers()
})
afterEach(() => {
vi.useRealTimers()
})
it('starts out connected', () => {
const { result } = renderHook(() => useReconnectingState())
expect(result.current.isReconnecting).toBe(false)
expect(result.current.reason).toBeNull()
})
it('stays quiet while the browser silently reconnects', () => {
// EventSource fires onerror while it retries on its own, typically
// recovering within a few seconds. Flashing a full-width banner for
// that reads as "the network is broken" when nothing was lost.
const { result } = renderHook(() => useReconnectingState())
act(() => result.current.reportDisconnect('error'))
expect(result.current.isReconnecting).toBe(false)
act(() => {
vi.advanceTimersByTime(RECONNECTING_BANNER_DELAY_MS - 1)
})
expect(result.current.isReconnecting).toBe(false)
act(() => result.current.reportConnect())
act(() => {
vi.advanceTimersByTime(RECONNECTING_BANNER_DELAY_MS)
})
expect(result.current.isReconnecting).toBe(false)
})
it('surfaces the banner once the outage outlasts the grace period', () => {
const { result } = renderHook(() => useReconnectingState())
act(() => result.current.reportDisconnect('heartbeat-timeout'))
act(() => {
vi.advanceTimersByTime(RECONNECTING_BANNER_DELAY_MS)
})
expect(result.current.isReconnecting).toBe(true)
expect(result.current.reason).toBe('heartbeat-timeout')
})
it('keeps the first reason when further disconnects arrive', () => {
const { result } = renderHook(() => useReconnectingState())
act(() => result.current.reportDisconnect('error'))
act(() => result.current.reportDisconnect('closed'))
act(() => {
vi.advanceTimersByTime(RECONNECTING_BANNER_DELAY_MS)
})
expect(result.current.isReconnecting).toBe(true)
expect(result.current.reason).toBe('error')
})
it('clears the banner as soon as the stream comes back', () => {
const { result } = renderHook(() => useReconnectingState())
act(() => result.current.reportDisconnect('error'))
act(() => {
vi.advanceTimersByTime(RECONNECTING_BANNER_DELAY_MS)
})
expect(result.current.isReconnecting).toBe(true)
act(() => result.current.reportConnect())
expect(result.current.isReconnecting).toBe(false)
expect(result.current.reason).toBeNull()
})
it('drops a pending banner when the hook unmounts', () => {
const { result, unmount } = renderHook(() => useReconnectingState())
act(() => result.current.reportDisconnect('error'))
unmount()
expect(() => {
vi.advanceTimersByTime(RECONNECTING_BANNER_DELAY_MS)
}).not.toThrow()
})
})