|
| 1 | +import { act, renderHook } from '@testing-library/react'; |
| 2 | + |
| 3 | +import { renderHookServer } from '@/tests'; |
| 4 | + |
| 5 | +import { useMediaQuery } from './useMediaQuery'; |
| 6 | + |
| 7 | +beforeEach(() => { |
| 8 | + Object.defineProperty(window, 'matchMedia', { |
| 9 | + writable: true, |
| 10 | + value: vi.fn().mockImplementation((query) => ({ |
| 11 | + matches: false, |
| 12 | + media: query, |
| 13 | + onchange: null, |
| 14 | + addListener: vi.fn(), // deprecated |
| 15 | + removeListener: vi.fn(), // deprecated |
| 16 | + addEventListener: vi.fn(), |
| 17 | + removeEventListener: vi.fn(), |
| 18 | + dispatchEvent: vi.fn() |
| 19 | + })) |
| 20 | + }); |
| 21 | +}); |
| 22 | + |
| 23 | +afterEach(() => { |
| 24 | + vi.restoreAllMocks(); |
| 25 | +}); |
| 26 | + |
| 27 | +const mockMatchMedia = (matches: boolean) => ({ |
| 28 | + matches, |
| 29 | + media: '(max-width: 768px)', |
| 30 | + onchange: null, |
| 31 | + addListener: vi.fn(), |
| 32 | + removeListener: vi.fn(), |
| 33 | + addEventListener: vi.fn(), |
| 34 | + removeEventListener: vi.fn(), |
| 35 | + dispatchEvent: vi.fn() |
| 36 | +}); |
| 37 | + |
| 38 | +it('Should use media query"', () => { |
| 39 | + const { result } = renderHook(() => useMediaQuery('(max-width: 768px)')); |
| 40 | + |
| 41 | + expect(typeof result.current).toBe('boolean'); |
| 42 | +}); |
| 43 | + |
| 44 | +it('Should use media query on server', () => { |
| 45 | + const { result } = renderHookServer(() => useMediaQuery('(max-width: 768px)')); |
| 46 | + |
| 47 | + expect(typeof result.current).toBe('boolean'); |
| 48 | +}); |
| 49 | + |
| 50 | +it('should return true if media query matches', () => { |
| 51 | + const mockMediaQueryList = mockMatchMedia(true); |
| 52 | + |
| 53 | + const matchMediaSpy = vi.spyOn(window, 'matchMedia'); |
| 54 | + matchMediaSpy.mockReturnValue(mockMediaQueryList); |
| 55 | + |
| 56 | + const { result } = renderHook(() => useMediaQuery('(max-width: 768px)')); |
| 57 | + |
| 58 | + expect(result.current).toEqual(true); |
| 59 | +}); |
| 60 | + |
| 61 | +it('should return false if media query does not matches', () => { |
| 62 | + const mockMediaQueryList = mockMatchMedia(false); |
| 63 | + |
| 64 | + const matchMediaSpy = vi.spyOn(window, 'matchMedia'); |
| 65 | + matchMediaSpy.mockReturnValue(mockMediaQueryList); |
| 66 | + |
| 67 | + const { result } = renderHook(() => useMediaQuery('(max-width: 768px)')); |
| 68 | + |
| 69 | + expect(result.current).toEqual(false); |
| 70 | +}); |
| 71 | + |
| 72 | +it('returns false if media query does not match after change', () => { |
| 73 | + const mockMediaQueryList = mockMatchMedia(true); |
| 74 | + |
| 75 | + const matchMediaSpy = vi.spyOn(window, 'matchMedia'); |
| 76 | + matchMediaSpy.mockReturnValue(mockMediaQueryList); |
| 77 | + |
| 78 | + const { result, rerender } = renderHook(() => useMediaQuery('(max-width: 768px)')); |
| 79 | + |
| 80 | + expect(result.current).toEqual(true); |
| 81 | + |
| 82 | + act(() => { |
| 83 | + mockMediaQueryList.matches = false; |
| 84 | + mockMediaQueryList.dispatchEvent(new Event('ResizeEvent')); |
| 85 | + }); |
| 86 | + |
| 87 | + rerender(); |
| 88 | + |
| 89 | + expect(result.current).toEqual(false); |
| 90 | +}); |
0 commit comments