35 lines
924 B
TypeScript
35 lines
924 B
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { renderHook, act } from '@testing-library/react'
|
|
import { useOnlineStatus } from '../useOnlineStatus'
|
|
|
|
describe('useOnlineStatus', () => {
|
|
it('returns true initially', () => {
|
|
const { result } = renderHook(() => useOnlineStatus())
|
|
expect(result.current).toBe(true)
|
|
})
|
|
|
|
it('returns false when offline event fires', () => {
|
|
const { result } = renderHook(() => useOnlineStatus())
|
|
|
|
act(() => {
|
|
window.dispatchEvent(new Event('offline'))
|
|
})
|
|
|
|
expect(result.current).toBe(false)
|
|
})
|
|
|
|
it('returns true when online event fires after offline', () => {
|
|
const { result } = renderHook(() => useOnlineStatus())
|
|
|
|
act(() => {
|
|
window.dispatchEvent(new Event('offline'))
|
|
})
|
|
expect(result.current).toBe(false)
|
|
|
|
act(() => {
|
|
window.dispatchEvent(new Event('online'))
|
|
})
|
|
expect(result.current).toBe(true)
|
|
})
|
|
})
|