|
| 1 | +import {useDeviceOrientation} from './useDeviceOrientation' |
| 2 | +import {act, renderHook} from '@testing-library/react-hooks' |
| 3 | + |
| 4 | +// @ts-expect-error - untyped implementation |
| 5 | +import RCTDeviceEventEmitter from 'react-native/Libraries/EventEmitter/RCTDeviceEventEmitter' |
| 6 | +import {Dimensions} from 'react-native' |
| 7 | + |
| 8 | +describe('useDeviceOrientation', () => { |
| 9 | + const emitChangeDimensions = (screen = {width: 0, height: 0}) => |
| 10 | + RCTDeviceEventEmitter.emit('didUpdateDimensions', {screen}) |
| 11 | + |
| 12 | + const rotatePortrait = () => emitChangeDimensions({height: 200, width: 100}) |
| 13 | + const rotateLandscape = () => emitChangeDimensions({height: 100, width: 200}) |
| 14 | + |
| 15 | + it('should return portrait orientation', () => { |
| 16 | + const {result} = renderHook(() => useDeviceOrientation()) |
| 17 | + |
| 18 | + act(() => rotatePortrait()) |
| 19 | + |
| 20 | + expect(result.current).toEqual({ |
| 21 | + landscape: false, |
| 22 | + portrait: true, |
| 23 | + }) |
| 24 | + }) |
| 25 | + |
| 26 | + it('should return landscape orientation', () => { |
| 27 | + const {result} = renderHook(() => useDeviceOrientation()) |
| 28 | + |
| 29 | + act(() => rotateLandscape()) |
| 30 | + |
| 31 | + expect(result.current).toEqual({ |
| 32 | + landscape: true, |
| 33 | + portrait: false, |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + it('should return landscape & portrait orientations when window is square', () => { |
| 38 | + const {result} = renderHook(() => useDeviceOrientation()) |
| 39 | + |
| 40 | + act(() => emitChangeDimensions({width: 100, height: 100})) |
| 41 | + |
| 42 | + expect(result.current).toEqual({ |
| 43 | + landscape: true, |
| 44 | + portrait: true, |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + it('should use latest screen size for orientation state initialization', () => { |
| 49 | + Dimensions.get('screen') |
| 50 | + |
| 51 | + rotateLandscape() |
| 52 | + |
| 53 | + const {result} = renderHook(() => useDeviceOrientation()) |
| 54 | + |
| 55 | + expect(result.current).toEqual({ |
| 56 | + landscape: true, |
| 57 | + portrait: false, |
| 58 | + }) |
| 59 | + }) |
| 60 | +}) |
0 commit comments