|
| 1 | +import useBreakpoint, { |
| 2 | + DefaultBreakpointMap, |
| 3 | + createBreakpointHook, |
| 4 | +} from '../src/useBreakpoint' |
| 5 | + |
| 6 | +import React from 'react' |
| 7 | +import { mount } from 'enzyme' |
| 8 | + |
| 9 | +interface Props { |
| 10 | + breakpoint: DefaultBreakpointMap |
| 11 | +} |
| 12 | + |
| 13 | +describe('useBreakpoint', () => { |
| 14 | + let matchMediaSpy: jest.SpyInstance<MediaQueryList, [string]> |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + matchMediaSpy = jest.spyOn(window, 'matchMedia') |
| 18 | + window.resizeTo(1024, window.innerHeight) |
| 19 | + }) |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + matchMediaSpy.mockRestore() |
| 23 | + }) |
| 24 | + |
| 25 | + it.each` |
| 26 | + width | expected | config |
| 27 | + ${1024} | ${false} | ${{ md: 'down', sm: 'up' }} |
| 28 | + ${600} | ${true} | ${{ md: 'down', sm: 'up' }} |
| 29 | + ${991} | ${true} | ${{ md: 'down' }} |
| 30 | + ${992} | ${false} | ${{ md: 'down' }} |
| 31 | + ${768} | ${true} | ${{ md: 'up' }} |
| 32 | + ${576} | ${false} | ${{ xs: 'down' }} |
| 33 | + ${576} | ${false} | ${{ md: true }} |
| 34 | + ${800} | ${true} | ${{ md: true }} |
| 35 | + ${1000} | ${false} | ${{ md: true }} |
| 36 | + ${576} | ${false} | ${'md'} |
| 37 | + ${800} | ${true} | ${'md'} |
| 38 | + ${1000} | ${false} | ${'md'} |
| 39 | + ${500} | ${true} | ${{ xs: 'down' }} |
| 40 | + ${0} | ${true} | ${{ xs: 'up' }} |
| 41 | + `( |
| 42 | + 'should match: $expected with config: $config at window width: $width', |
| 43 | + ({ width, expected, config }) => { |
| 44 | + let actual: boolean |
| 45 | + |
| 46 | + window.resizeTo(width, window.innerHeight) |
| 47 | + |
| 48 | + const Wrapper = () => { |
| 49 | + actual = useBreakpoint(config) |
| 50 | + return null |
| 51 | + } |
| 52 | + mount(<Wrapper />).unmount() |
| 53 | + expect(actual).toEqual(expected) |
| 54 | + }, |
| 55 | + ) |
| 56 | + |
| 57 | + it('should assume pixels for number values', () => { |
| 58 | + const useCustomBreakpoint = createBreakpointHook({ |
| 59 | + xs: 0, |
| 60 | + sm: 400, |
| 61 | + md: 700, |
| 62 | + }) |
| 63 | + |
| 64 | + const Wrapper = () => { |
| 65 | + useCustomBreakpoint('sm') |
| 66 | + return null |
| 67 | + } |
| 68 | + mount(<Wrapper />).unmount() |
| 69 | + |
| 70 | + expect(matchMediaSpy).toBeCalled() |
| 71 | + expect(matchMediaSpy.mock.calls[0][0]).toEqual( |
| 72 | + '(min-width: 400px) and (max-width: 699.8px)', |
| 73 | + ) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should use calc for string values', () => { |
| 77 | + const useCustomBreakpoint = createBreakpointHook({ |
| 78 | + xs: 0, |
| 79 | + sm: '40rem', |
| 80 | + md: '70rem', |
| 81 | + }) |
| 82 | + |
| 83 | + const Wrapper = () => { |
| 84 | + useCustomBreakpoint('sm') |
| 85 | + return null |
| 86 | + } |
| 87 | + mount(<Wrapper />).unmount() |
| 88 | + |
| 89 | + expect(matchMediaSpy).toBeCalled() |
| 90 | + expect(matchMediaSpy.mock.calls[0][0]).toEqual( |
| 91 | + '(min-width: 40rem) and (max-width: calc(70rem - 0.2px))', |
| 92 | + ) |
| 93 | + }) |
| 94 | + |
| 95 | + it('should flatten media', () => { |
| 96 | + const useCustomBreakpoint = createBreakpointHook({ |
| 97 | + sm: 400, |
| 98 | + md: 400, |
| 99 | + }) |
| 100 | + |
| 101 | + const Wrapper = () => { |
| 102 | + useCustomBreakpoint({ sm: 'up', md: 'up' }) |
| 103 | + return null |
| 104 | + } |
| 105 | + mount(<Wrapper />).unmount() |
| 106 | + |
| 107 | + expect(matchMediaSpy.mock.calls[0][0]).toEqual('(min-width: 400px)') |
| 108 | + }) |
| 109 | +}) |
0 commit comments