|
| 1 | +import React from 'react'; |
| 2 | +import { renderToString } from 'react-dom/server'; |
| 3 | + |
| 4 | +import { Row } from '..'; |
| 5 | +import { act, render } from '../../../tests/utils'; |
| 6 | +import ConfigProvider from '../../config-provider'; |
| 7 | +import useBreakpoint from '../hooks/useBreakpoint'; |
| 8 | + |
| 9 | +describe('useBreakpoint', () => { |
| 10 | + const originalMatchMedia = window.matchMedia; |
| 11 | + let mediaQueries: Map<string, MediaQueryList>; |
| 12 | + let matchedQueries: Set<string>; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + mediaQueries = new Map(); |
| 16 | + matchedQueries = new Set(['(max-width: 575px)']); |
| 17 | + window.matchMedia = jest.fn((query: string) => { |
| 18 | + if (!mediaQueries.has(query)) { |
| 19 | + const mql = Object.assign(new EventTarget(), { |
| 20 | + media: query, |
| 21 | + matches: matchedQueries.has(query), |
| 22 | + }) as MediaQueryList; |
| 23 | + jest.spyOn(mql, 'addEventListener'); |
| 24 | + jest.spyOn(mql, 'removeEventListener'); |
| 25 | + mediaQueries.set(query, mql); |
| 26 | + } |
| 27 | + return mediaQueries.get(query)!; |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + jest.restoreAllMocks(); |
| 33 | + window.matchMedia = originalMatchMedia; |
| 34 | + }); |
| 35 | + |
| 36 | + const changeMedia = (query: string, matches: boolean) => { |
| 37 | + const mql = mediaQueries.get(query)!; |
| 38 | + Object.assign(mql, { matches }); |
| 39 | + act(() => { |
| 40 | + mql.dispatchEvent(Object.assign(new Event('change'), { matches })); |
| 41 | + }); |
| 42 | + }; |
| 43 | + |
| 44 | + it('provides client breakpoints on the first commit', () => { |
| 45 | + const committed = jest.fn(); |
| 46 | + const Demo: React.FC = () => { |
| 47 | + const screens = useBreakpoint(); |
| 48 | + React.useLayoutEffect(() => { |
| 49 | + committed(screens); |
| 50 | + }, [screens]); |
| 51 | + return null; |
| 52 | + }; |
| 53 | + |
| 54 | + render(<Demo />); |
| 55 | + expect(committed.mock.calls[0][0]).toMatchObject({ xs: true, md: false }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('updates breakpoints without resubscribing or rendering unchanged snapshots', () => { |
| 59 | + const rendered = jest.fn(); |
| 60 | + const Demo: React.FC = () => { |
| 61 | + const screens = useBreakpoint(); |
| 62 | + rendered(screens); |
| 63 | + return <span>{JSON.stringify(screens)}</span>; |
| 64 | + }; |
| 65 | + const { container, rerender, unmount } = render(<Demo />); |
| 66 | + expect(JSON.parse(container.textContent!)).toMatchObject({ xs: true, md: false }); |
| 67 | + const matchMediaCalls = jest.mocked(window.matchMedia).mock.calls.length; |
| 68 | + |
| 69 | + changeMedia('(min-width: 768px)', true); |
| 70 | + expect(JSON.parse(container.textContent!)).toMatchObject({ md: true }); |
| 71 | + |
| 72 | + const renderCount = rendered.mock.calls.length; |
| 73 | + changeMedia('(min-width: 768px)', true); |
| 74 | + expect(rendered).toHaveBeenCalledTimes(renderCount); |
| 75 | + |
| 76 | + rerender(<Demo />); |
| 77 | + expect(window.matchMedia).toHaveBeenCalledTimes(matchMediaCalls); |
| 78 | + |
| 79 | + unmount(); |
| 80 | + mediaQueries.forEach((mql) => { |
| 81 | + expect(mql.removeEventListener).toHaveBeenCalledTimes( |
| 82 | + jest.mocked(mql.addEventListener).mock.calls.length, |
| 83 | + ); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('reads the latest breakpoints on parent renders when refresh is disabled', () => { |
| 88 | + const rendered = jest.fn(); |
| 89 | + const Demo: React.FC<{ refresh: boolean }> = (props) => { |
| 90 | + const { refresh } = props; |
| 91 | + const screens = useBreakpoint(refresh); |
| 92 | + rendered(screens); |
| 93 | + return <span>{JSON.stringify(screens)}</span>; |
| 94 | + }; |
| 95 | + const { container, rerender } = render(<Demo refresh={false} />); |
| 96 | + const renderCount = rendered.mock.calls.length; |
| 97 | + const matchMediaCalls = jest.mocked(window.matchMedia).mock.calls.length; |
| 98 | + expect(container.textContent).toBe('{}'); |
| 99 | + |
| 100 | + changeMedia('(min-width: 768px)', true); |
| 101 | + expect(rendered).toHaveBeenCalledTimes(renderCount); |
| 102 | + expect(container.textContent).toBe('{}'); |
| 103 | + |
| 104 | + rerender(<Demo refresh={false} />); |
| 105 | + expect(JSON.parse(container.textContent!)).toMatchObject({ md: true }); |
| 106 | + |
| 107 | + rerender(<Demo refresh />); |
| 108 | + changeMedia('(min-width: 768px)', false); |
| 109 | + expect(JSON.parse(container.textContent!)).toMatchObject({ md: false }); |
| 110 | + expect(window.matchMedia).toHaveBeenCalledTimes(matchMediaCalls); |
| 111 | + |
| 112 | + rerender(<Demo refresh={false} />); |
| 113 | + const disabledRenderCount = rendered.mock.calls.length; |
| 114 | + changeMedia('(min-width: 768px)', true); |
| 115 | + expect(rendered).toHaveBeenCalledTimes(disabledRenderCount); |
| 116 | + |
| 117 | + rerender(<Demo refresh={false} />); |
| 118 | + expect(JSON.parse(container.textContent!)).toMatchObject({ md: true }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('updates subscriptions when breakpoint tokens change', () => { |
| 122 | + const Demo: React.FC = () => <span>{JSON.stringify(useBreakpoint())}</span>; |
| 123 | + const { container, rerender } = render( |
| 124 | + <ConfigProvider theme={{ token: { colorPrimary: '#1677ff' } }}> |
| 125 | + <Demo /> |
| 126 | + </ConfigProvider>, |
| 127 | + ); |
| 128 | + const previousQuery = mediaQueries.get('(min-width: 768px)')!; |
| 129 | + expect(JSON.parse(container.textContent!)).toMatchObject({ md: false }); |
| 130 | + |
| 131 | + const matchMediaCalls = jest.mocked(window.matchMedia).mock.calls.length; |
| 132 | + rerender( |
| 133 | + <ConfigProvider theme={{ token: { colorPrimary: '#ff0000' } }}> |
| 134 | + <Demo /> |
| 135 | + </ConfigProvider>, |
| 136 | + ); |
| 137 | + expect(window.matchMedia).toHaveBeenCalledTimes(matchMediaCalls); |
| 138 | + |
| 139 | + matchedQueries.add('(min-width: 800px)'); |
| 140 | + rerender( |
| 141 | + <ConfigProvider theme={{ token: { screenMD: 800, screenMDMin: 800 } }}> |
| 142 | + <Demo /> |
| 143 | + </ConfigProvider>, |
| 144 | + ); |
| 145 | + expect(JSON.parse(container.textContent!)).toMatchObject({ md: true }); |
| 146 | + expect(previousQuery.removeEventListener).toHaveBeenCalledTimes( |
| 147 | + jest.mocked(previousQuery.addEventListener).mock.calls.length, |
| 148 | + ); |
| 149 | + |
| 150 | + changeMedia('(min-width: 800px)', false); |
| 151 | + expect(JSON.parse(container.textContent!)).toMatchObject({ md: false }); |
| 152 | + }); |
| 153 | + |
| 154 | + it('retains the latest subscribed breakpoints across token changes when refresh is disabled', () => { |
| 155 | + const Demo: React.FC = () => <span>{JSON.stringify(useBreakpoint(false))}</span>; |
| 156 | + const renderDemo = (screenMD: number) => ( |
| 157 | + <ConfigProvider theme={{ token: { screenMD, screenMDMin: screenMD } }}> |
| 158 | + <Demo /> |
| 159 | + </ConfigProvider> |
| 160 | + ); |
| 161 | + const { container, rerender } = render(renderDemo(768)); |
| 162 | + |
| 163 | + rerender(renderDemo(768)); |
| 164 | + expect(JSON.parse(container.textContent!)).toMatchObject({ xs: true, md: false }); |
| 165 | + |
| 166 | + changeMedia('(min-width: 768px)', true); |
| 167 | + expect(JSON.parse(container.textContent!)).toMatchObject({ md: false }); |
| 168 | + |
| 169 | + rerender(renderDemo(800)); |
| 170 | + expect(JSON.parse(container.textContent!)).toMatchObject({ xs: true, md: true }); |
| 171 | + |
| 172 | + rerender(renderDemo(800)); |
| 173 | + expect(JSON.parse(container.textContent!)).toMatchObject({ xs: true, md: false }); |
| 174 | + }); |
| 175 | + |
| 176 | + it.each([undefined, { md: true }] as const)( |
| 177 | + 'hydrates with the same default screens as the server: %p', |
| 178 | + (defaultScreens) => { |
| 179 | + const rendered = jest.fn(); |
| 180 | + const Demo: React.FC = () => { |
| 181 | + const screens = useBreakpoint(true, defaultScreens); |
| 182 | + rendered(screens); |
| 183 | + return <span>{JSON.stringify(screens)}</span>; |
| 184 | + }; |
| 185 | + const container = document.createElement('div'); |
| 186 | + container.innerHTML = renderToString(<Demo />); |
| 187 | + expect(container.textContent).toBe(JSON.stringify(defaultScreens ?? {})); |
| 188 | + expect(window.matchMedia).not.toHaveBeenCalled(); |
| 189 | + const serverRenderCount = rendered.mock.calls.length; |
| 190 | + const onRecoverableError = jest.fn(); |
| 191 | + |
| 192 | + const { unmount } = render(<Demo />, { container, hydrate: true, onRecoverableError }); |
| 193 | + expect(rendered.mock.calls[serverRenderCount][0]).toEqual(defaultScreens ?? {}); |
| 194 | + expect(onRecoverableError).not.toHaveBeenCalled(); |
| 195 | + expect(JSON.parse(container.textContent!)).toMatchObject({ xs: true, md: false }); |
| 196 | + unmount(); |
| 197 | + }, |
| 198 | + ); |
| 199 | + |
| 200 | + it('preserves the null default used by responsive gutters during hydration', () => { |
| 201 | + const Demo: React.FC = () => <Row gutter={{ xs: 8, md: 24 }} />; |
| 202 | + const container = document.createElement('div'); |
| 203 | + container.innerHTML = renderToString(<Demo />); |
| 204 | + expect(container.firstElementChild).toHaveStyle({ marginInline: '-12px' }); |
| 205 | + expect(window.matchMedia).not.toHaveBeenCalled(); |
| 206 | + const onRecoverableError = jest.fn(); |
| 207 | + |
| 208 | + const { unmount } = render(<Demo />, { container, hydrate: true, onRecoverableError }); |
| 209 | + expect(onRecoverableError).not.toHaveBeenCalled(); |
| 210 | + expect(container.firstElementChild).toHaveStyle({ marginInline: '-4px' }); |
| 211 | + unmount(); |
| 212 | + }); |
| 213 | +}); |
0 commit comments