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.7 kB · 59 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import { afterEach, describe, expect, it, vi } from 'vitest';import { renderHook } from 'vitest-browser-react';import { act } from 'react';
import useDeviceOrientation from './useDeviceOrientation.js';
const itIfWindowDefined = it.runIf(typeof window !== 'undefined');const itIfWindowUndefined = it.runIf(typeof window === 'undefined');
describe('useDeviceOrientation()', () => { afterEach(() => { vi.restoreAllMocks(); });
itIfWindowDefined('should return null initially', async () => { const { result } = await renderHook(() => useDeviceOrientation());
expect(result.current).toBe(null); });
itIfWindowDefined('should update when device orientation changes', async () => { const { result } = await renderHook(() => useDeviceOrientation());
act(() => { window.dispatchEvent( Object.assign(new Event('deviceorientation'), { absolute: true, alpha: 45, beta: -30, gamma: 15, }), ); });
expect(result.current).toEqual({ absolute: true, alpha: 45, beta: -30, gamma: 15, }); });
itIfWindowDefined('should remove the orientation listener on unmount', async () => { const removeEventListener = vi.spyOn(window, 'removeEventListener');
const { unmount } = await renderHook(() => useDeviceOrientation());
await unmount();
expect(removeEventListener).toHaveBeenCalledTimes(1); expect(removeEventListener).toHaveBeenCalledWith('deviceorientation', expect.any(Function)); });
itIfWindowUndefined('should return null', async () => { const { result } = await renderHook(() => useDeviceOrientation());
expect(result.current).toBe(null); });});