Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-hooks. A collection of React Hooks.
hooks react react-hooks
Something went wrong. Try again.
1.9 kB · 56 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import { afterEach, describe, expect, it, vi } from 'vitest';import { renderHook } from 'vitest-browser-react';import { act } from 'react';
import useDocumentVisibility from './useDocumentVisibility.js';
const itIfDocumentDefined = it.runIf(typeof document !== 'undefined');const itIfDocumentUndefined = it.runIf(typeof document === 'undefined');
describe('useDocumentVisibility()', () => { afterEach(() => { vi.restoreAllMocks(); });
itIfDocumentDefined('should return document.visibilityState initially', async () => { const { result } = await renderHook(() => useDocumentVisibility());
expect(result.current).toBe(document.visibilityState); });
itIfDocumentDefined('should update when document visibility changes', async () => { let visibilityState: DocumentVisibilityState = 'visible';
vi.spyOn(document, 'visibilityState', 'get').mockImplementation(() => visibilityState);
const { result } = await renderHook(() => useDocumentVisibility());
expect(result.current).toBe('visible');
act(() => { visibilityState = 'hidden'; document.dispatchEvent(new Event('visibilitychange')); });
expect(result.current).toBe('hidden'); });
itIfDocumentDefined('should remove the visibility listener on unmount', async () => { const removeEventListener = vi.spyOn(document, 'removeEventListener');
const { unmount } = await renderHook(() => useDocumentVisibility());
expect(removeEventListener).not.toHaveBeenCalled();
await unmount();
expect(removeEventListener).toHaveBeenCalledTimes(1); expect(removeEventListener).toHaveBeenCalledWith('visibilitychange', expect.any(Function)); });
itIfDocumentUndefined('should return null', async () => { const { result } = await renderHook(() => useDocumentVisibility());
expect(result.current).toBe(null); });});