|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import View from '../../Components/View/View'; |
| 12 | +import Dimensions from '../Dimensions'; |
| 13 | +import { |
| 14 | + type DisplayMetrics, |
| 15 | + type DisplayMetricsAndroid, |
| 16 | +} from '../NativeDeviceInfo'; |
| 17 | +import useWindowDimensions from '../useWindowDimensions'; |
| 18 | +import {useEffect} from 'react'; |
| 19 | +import {act, create} from 'react-test-renderer'; |
| 20 | + |
| 21 | +type State = DisplayMetrics | DisplayMetricsAndroid; |
| 22 | +type TestProps = { |
| 23 | + selector?: (state: State) => number, |
| 24 | + onResult?: (result: number | State) => void, |
| 25 | + testID?: string, |
| 26 | +}; |
| 27 | +function TestView({selector, onResult}: TestProps) { |
| 28 | + const result = useWindowDimensions(selector); |
| 29 | + useEffect(() => { |
| 30 | + onResult?.(result); |
| 31 | + }, [onResult, result]); |
| 32 | + return <View />; |
| 33 | +} |
| 34 | + |
| 35 | +const defaultWindow = {fontScale: 2, height: 1334, scale: 2, width: 750}; |
| 36 | + |
| 37 | +describe('useWindowDimensions', () => { |
| 38 | + const expectedDimensions = Dimensions.get('window'); |
| 39 | + let cleanupFns = []; |
| 40 | + |
| 41 | + // Auto cleanup |
| 42 | + afterEach(() => { |
| 43 | + cleanupFns.forEach(fn => fn()); |
| 44 | + cleanupFns = []; |
| 45 | + }); |
| 46 | + |
| 47 | + const renderHook = (props?: TestProps) => { |
| 48 | + let root; |
| 49 | + const defaultProps: TestProps = {onResult: jest.fn(), selector: undefined}; |
| 50 | + // Mount |
| 51 | + act(() => { |
| 52 | + root = create(<TestView {...defaultProps} {...props} />); |
| 53 | + }); |
| 54 | + |
| 55 | + const rerender = (newProps: TestProps) => { |
| 56 | + act(() => { |
| 57 | + root.update(<TestView {...defaultProps} {...props} {...newProps} />); |
| 58 | + }); |
| 59 | + }; |
| 60 | + const unmount = () => { |
| 61 | + act(() => { |
| 62 | + root.unmount(); |
| 63 | + }); |
| 64 | + }; |
| 65 | + cleanupFns.push(unmount); // auto-cleanup |
| 66 | + return {unmount, rerender}; |
| 67 | + }; |
| 68 | + |
| 69 | + const mockGetWindow = () => { |
| 70 | + const spy = jest.spyOn(Dimensions, 'get'); |
| 71 | + cleanupFns.push(() => spy.mockRestore()); // auto-cleanup |
| 72 | + return { |
| 73 | + getWindow: spy, |
| 74 | + }; |
| 75 | + }; |
| 76 | + const mockAddEventListener = () => { |
| 77 | + const sub = {remove: jest.fn()}; |
| 78 | + const spy = jest |
| 79 | + .spyOn(Dimensions, 'addEventListener') |
| 80 | + .mockImplementation(() => sub); |
| 81 | + cleanupFns.push(() => spy.mockRestore()); // auto-cleanup |
| 82 | + return { |
| 83 | + addListener: spy, |
| 84 | + removeListener: sub.remove, |
| 85 | + // $FlowFixMe[unclear-type] |
| 86 | + getListener: (): Function => spy.mock.calls.at(-1)?.at(1), // `-1` - last call, `1` - second argument |
| 87 | + }; |
| 88 | + }; |
| 89 | + |
| 90 | + it('should cleanup a listener on a component unmount', () => { |
| 91 | + // Arrange |
| 92 | + const {addListener, removeListener} = mockAddEventListener(); |
| 93 | + |
| 94 | + const {unmount} = renderHook(); |
| 95 | + |
| 96 | + expect(addListener).toHaveBeenCalledTimes(1); |
| 97 | + expect(addListener).toHaveBeenCalledWith('change', expect.any(Function)); |
| 98 | + |
| 99 | + // Act |
| 100 | + unmount(); |
| 101 | + |
| 102 | + // Assert |
| 103 | + expect(removeListener).toHaveBeenCalledTimes(1); |
| 104 | + expect(removeListener).toHaveBeenCalledWith(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should return the current window dimensions on mount', () => { |
| 108 | + // Arrange |
| 109 | + const onResult = jest.fn(); |
| 110 | + |
| 111 | + // Act |
| 112 | + renderHook({onResult}); |
| 113 | + |
| 114 | + // Assert |
| 115 | + expect(onResult).toHaveBeenCalledTimes(1); |
| 116 | + expect(onResult).toHaveBeenCalledWith(expectedDimensions); |
| 117 | + expect(expectedDimensions).toStrictEqual(defaultWindow); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should return the same object on re-render', () => { |
| 121 | + // Arrange |
| 122 | + const onResult = jest.fn(); |
| 123 | + |
| 124 | + const {rerender} = renderHook({onResult}); |
| 125 | + |
| 126 | + expect(onResult).toHaveBeenCalledTimes(1); |
| 127 | + expect(onResult).toHaveBeenCalledWith(expectedDimensions); |
| 128 | + expect(expectedDimensions).toStrictEqual(defaultWindow); |
| 129 | + |
| 130 | + // Act |
| 131 | + rerender({testID: 'test-123'}); |
| 132 | + |
| 133 | + // Assert |
| 134 | + expect(onResult).toHaveBeenCalledTimes(1); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should not re-render when screen dimension has changed but window is the same', () => { |
| 138 | + // Arrange |
| 139 | + const {getListener} = mockAddEventListener(); |
| 140 | + const onResult = jest.fn(); |
| 141 | + renderHook({onResult}); |
| 142 | + |
| 143 | + expect(onResult).toHaveBeenCalledTimes(1); |
| 144 | + expect(onResult).toHaveBeenCalledWith(expectedDimensions); |
| 145 | + expect(expectedDimensions).toStrictEqual(defaultWindow); |
| 146 | + |
| 147 | + // Act |
| 148 | + const listener = getListener(); |
| 149 | + act(() => { |
| 150 | + listener({ |
| 151 | + window: {...expectedDimensions}, |
| 152 | + screen: {...expectedDimensions, height: 1000}, |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + // Assert |
| 157 | + expect(onResult).toHaveBeenCalledTimes(1); |
| 158 | + }); |
| 159 | + |
| 160 | + describe('selector argument', () => { |
| 161 | + it('should return partial of state', () => { |
| 162 | + const onResult = jest.fn(); |
| 163 | + |
| 164 | + renderHook({onResult, selector: state => state.height}); |
| 165 | + |
| 166 | + // Assert |
| 167 | + expect(onResult).toHaveBeenCalledTimes(1); |
| 168 | + expect(onResult).toHaveBeenCalledWith(expectedDimensions.height); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should re-render if selected value has changed', () => { |
| 172 | + // Arrange |
| 173 | + const newHeight = 666; |
| 174 | + const onResult = jest.fn(); |
| 175 | + const {getListener} = mockAddEventListener(); |
| 176 | + const {getWindow} = mockGetWindow(); |
| 177 | + |
| 178 | + renderHook({onResult, selector: state => state.height}); |
| 179 | + |
| 180 | + expect(onResult).toHaveBeenCalledTimes(1); |
| 181 | + expect(onResult).toHaveBeenNthCalledWith(1, expectedDimensions.height); |
| 182 | + |
| 183 | + // Act |
| 184 | + act(() => { |
| 185 | + const listener = getListener(); |
| 186 | + const newWindow = {...expectedDimensions, height: newHeight}; |
| 187 | + getWindow.mockReturnValue(newWindow); |
| 188 | + listener({window: newWindow}); |
| 189 | + }); |
| 190 | + |
| 191 | + // Assert |
| 192 | + expect(onResult).toHaveBeenCalledTimes(2); |
| 193 | + expect(onResult).toHaveBeenNthCalledWith(2, newHeight); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should return derived value based on state', () => { |
| 197 | + // Arrange |
| 198 | + const onResult = jest.fn(); |
| 199 | + |
| 200 | + // Act |
| 201 | + renderHook({onResult, selector: state => state.width / state.height}); |
| 202 | + |
| 203 | + // Assert |
| 204 | + expect(onResult).toHaveBeenCalledTimes(1); |
| 205 | + expect(onResult).toHaveBeenCalledWith( |
| 206 | + expectedDimensions.width / expectedDimensions.height, |
| 207 | + ); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should not re-render if selected value has not changed', () => { |
| 211 | + // Arrange |
| 212 | + const onResult = jest.fn(); |
| 213 | + const {getListener} = mockAddEventListener(); |
| 214 | + const {getWindow} = mockGetWindow(); |
| 215 | + |
| 216 | + renderHook({onResult, selector: state => state.fontScale}); |
| 217 | + expect(onResult).toHaveBeenCalledTimes(1); |
| 218 | + expect(onResult).toHaveBeenCalledWith(expectedDimensions.fontScale); |
| 219 | + |
| 220 | + // Act |
| 221 | + act(() => { |
| 222 | + const listener = getListener(); |
| 223 | + const newWindow = {...expectedDimensions, width: 400, height: 400}; |
| 224 | + getWindow.mockReturnValue(newWindow); |
| 225 | + listener({window: newWindow}); |
| 226 | + }); |
| 227 | + |
| 228 | + // Assert |
| 229 | + expect(onResult).toHaveBeenCalledTimes(1); |
| 230 | + }); |
| 231 | + }); |
| 232 | +}); |
0 commit comments