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.
3.0 kB · 108 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { renderHook } from 'vitest-browser-react';import { act } from 'react';
import useMatchMedia from './useMatchMedia.js';
import type { Mock } from 'vitest';
const itIfWindowDefined = it.runIf(typeof window !== 'undefined');const itIfWindowUndefined = it.runIf(typeof window === 'undefined');
describe('useMatchMedia()', () => { let matches: boolean; let addEventListener: Mock; let removeEventListener: Mock;
beforeEach(() => { if (typeof window !== 'undefined') { matches = true; addEventListener = vi.fn(); removeEventListener = vi.fn();
const mql = { get matches() { return matches; }, addEventListener, removeEventListener, };
window.matchMedia = vi.fn().mockReturnValue(mql); } });
afterEach(() => { vi.clearAllMocks(); });
itIfWindowDefined('should return the flag initially', async () => { const { result } = await renderHook(() => useMatchMedia('screen and (min-width: 1024px'));
expect(result.current).toBe(true); });
itIfWindowUndefined('should return null', async () => { const { result } = await renderHook(() => useMatchMedia('screen and (min-width: 1024px'));
expect(result.current).toBe(null); });
itIfWindowDefined('should add listener to MediaQueryList', async () => { await renderHook(() => useMatchMedia('screen and (min-width: 1024px'));
expect(addEventListener).toHaveBeenCalledTimes(1); });
itIfWindowDefined('should update the flag when the listener is called', async () => { let listener: EventListener; addEventListener.mockImplementationOnce((_type, currentListener) => { listener = currentListener;
return () => null; });
const { result } = await renderHook(() => useMatchMedia('screen and (min-width: 1024px'));
act(() => { matches = false; const mediaQueryListEvent = new MediaQueryListEvent('change', { matches }); listener(mediaQueryListEvent); });
expect(result.current).toBe(false); });
itIfWindowDefined('should update the flag when the listener is called (legacy)', async () => { let matches = true; const addListener = vi.fn(); const removeListener = vi.fn();
const mql = { get matches() { return matches; }, addListener, removeListener, };
window.matchMedia = vi.fn().mockReturnValue(mql);
let listener: EventListener; addListener.mockImplementationOnce((currentListener) => { listener = currentListener;
return () => null; });
const { result } = await renderHook(() => useMatchMedia('screen and (min-width: 1024px'));
act(() => { matches = false; const mediaQueryListEvent = new MediaQueryListEvent('change', { matches }); listener(mediaQueryListEvent); });
expect(result.current).toBe(false); });});