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.3 kB · 42 lines
TypeScript
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243import { beforeEach, describe, expect, it, vi } from 'vitest';import { renderHook } from 'vitest-browser-react';
import useForcedColors from './useForcedColors.js';import useMatchMedia from './useMatchMedia.js';
vi.mock('/src/useMatchMedia.ts', () => ({ default: vi.fn(),}));
const itIfWindowDefined = it.runIf(typeof window !== 'undefined');const itIfWindowUndefined = it.runIf(typeof window === 'undefined');
describe('useForcedColors()', () => { beforeEach(() => { vi.mocked(useMatchMedia).mockReset(); });
itIfWindowDefined('calls useMatchMedia with correct query', async () => { vi.mocked(useMatchMedia).mockReturnValue(false);
await renderHook(() => useForcedColors());
expect(useMatchMedia).toHaveBeenCalledWith('(forced-colors: active)'); });
itIfWindowDefined.each([false, true])('returns useMatchMedia result properly', async (active) => { vi.mocked(useMatchMedia).mockReturnValue(active);
const { result } = await renderHook(() => useForcedColors());
expect(result.current).toBe(active); });
itIfWindowUndefined('should return null', async () => { vi.mocked(useMatchMedia).mockReturnValue(null);
const { result } = await renderHook(() => useForcedColors());
expect(result.current).toBe(null); });});