feat: add unit tests and CI configuration
This commit is contained in:
34
lib/hooks/__tests__/useOnlineStatus.test.ts
Normal file
34
lib/hooks/__tests__/useOnlineStatus.test.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
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)
|
||||
})
|
||||
})
|
||||
99
lib/hooks/__tests__/useVisibilityPolling.test.ts
Normal file
99
lib/hooks/__tests__/useVisibilityPolling.test.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { renderHook, act } from '@testing-library/react'
|
||||
import { useVisibilityPolling } from '../useVisibilityPolling'
|
||||
|
||||
describe('useVisibilityPolling', () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('starts polling and calls callback at the visible interval', () => {
|
||||
const callback = vi.fn()
|
||||
|
||||
renderHook(() =>
|
||||
useVisibilityPolling(callback, {
|
||||
visibleInterval: 1000,
|
||||
hiddenInterval: null,
|
||||
})
|
||||
)
|
||||
|
||||
// Initial call might not happen immediately; advance to trigger interval
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(1000)
|
||||
})
|
||||
|
||||
expect(callback).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('reports isPolling as true when active', () => {
|
||||
const callback = vi.fn()
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useVisibilityPolling(callback, {
|
||||
visibleInterval: 1000,
|
||||
hiddenInterval: null,
|
||||
})
|
||||
)
|
||||
|
||||
expect(result.current.isPolling).toBe(true)
|
||||
})
|
||||
|
||||
it('calls callback multiple times over multiple intervals', () => {
|
||||
const callback = vi.fn()
|
||||
|
||||
renderHook(() =>
|
||||
useVisibilityPolling(callback, {
|
||||
visibleInterval: 500,
|
||||
hiddenInterval: null,
|
||||
})
|
||||
)
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(1500)
|
||||
})
|
||||
|
||||
expect(callback.mock.calls.length).toBeGreaterThanOrEqual(2)
|
||||
})
|
||||
|
||||
it('triggerPoll calls callback immediately', () => {
|
||||
const callback = vi.fn()
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useVisibilityPolling(callback, {
|
||||
visibleInterval: 10000,
|
||||
hiddenInterval: null,
|
||||
})
|
||||
)
|
||||
|
||||
act(() => {
|
||||
result.current.triggerPoll()
|
||||
})
|
||||
|
||||
expect(callback).toHaveBeenCalled()
|
||||
expect(result.current.lastPollTime).not.toBeNull()
|
||||
})
|
||||
|
||||
it('cleans up intervals on unmount', () => {
|
||||
const callback = vi.fn()
|
||||
|
||||
const { unmount } = renderHook(() =>
|
||||
useVisibilityPolling(callback, {
|
||||
visibleInterval: 1000,
|
||||
hiddenInterval: null,
|
||||
})
|
||||
)
|
||||
|
||||
unmount()
|
||||
callback.mockClear()
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(5000)
|
||||
})
|
||||
|
||||
expect(callback).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user