|
| 1 | +import {useKeyboard} from './useKeyboard' |
| 2 | +import {act, renderHook} from '@testing-library/react-hooks' |
| 3 | +import {Keyboard} from 'react-native' |
| 4 | + |
| 5 | +describe('useKeyboard', () => { |
| 6 | + const mockCoords = {screenX: 0, screenY: 0, width: 0, height: 0} |
| 7 | + const emitKeyboardEvent = ({ |
| 8 | + show = true, |
| 9 | + startCoordinates = mockCoords, |
| 10 | + endCoordinates = mockCoords, |
| 11 | + }) => { |
| 12 | + const mockEvent = {startCoordinates, endCoordinates} |
| 13 | + |
| 14 | + Keyboard.emit( |
| 15 | + show ? 'keyboardDidShow' : 'keyboardDidHide', |
| 16 | + show ? mockEvent : null, |
| 17 | + ) |
| 18 | + } |
| 19 | + |
| 20 | + describe('setKeyboardHeight: number', () => { |
| 21 | + it('keyboard height is zero by default', () => { |
| 22 | + const {result} = renderHook(() => useKeyboard()) |
| 23 | + |
| 24 | + expect(result.current.keyboardHeight).toBe(0) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should update keyboard height when keyboard will open', () => { |
| 28 | + const height = 123 |
| 29 | + const {result} = renderHook(() => useKeyboard()) |
| 30 | + |
| 31 | + act(() => { |
| 32 | + emitKeyboardEvent({show: true, endCoordinates: {...mockCoords, height}}) |
| 33 | + }) |
| 34 | + |
| 35 | + expect(result.current.keyboardHeight).toBe(height) |
| 36 | + }) |
| 37 | + |
| 38 | + it('should reset keyboard height when keyboard will close', () => { |
| 39 | + const height = 123 |
| 40 | + const {result} = renderHook(() => useKeyboard()) |
| 41 | + |
| 42 | + act(() => { |
| 43 | + emitKeyboardEvent({show: true, endCoordinates: {...mockCoords, height}}) |
| 44 | + }) |
| 45 | + |
| 46 | + expect(result.current.keyboardHeight).toBe(height) |
| 47 | + |
| 48 | + act(() => { |
| 49 | + emitKeyboardEvent({show: false}) |
| 50 | + }) |
| 51 | + |
| 52 | + expect(result.current.keyboardHeight).toBe(0) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + describe('keyboardShown: boolean', () => { |
| 57 | + it('keyboard closed by default', () => { |
| 58 | + const {result} = renderHook(() => useKeyboard()) |
| 59 | + |
| 60 | + expect(result.current.keyboardShown).toBe(false) |
| 61 | + }) |
| 62 | + |
| 63 | + it('should set keyboardShown when keyboard will open', () => { |
| 64 | + const {result} = renderHook(() => useKeyboard()) |
| 65 | + const {keyboardShown: initial} = result.current |
| 66 | + |
| 67 | + act(() => { |
| 68 | + emitKeyboardEvent({show: true}) |
| 69 | + }) |
| 70 | + |
| 71 | + const {keyboardShown: afterOpen} = result.current |
| 72 | + |
| 73 | + expect({initial, afterOpen}).toEqual({initial: false, afterOpen: true}) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should reset keyboardShown when keyboard will close', () => { |
| 77 | + const {result} = renderHook(() => useKeyboard()) |
| 78 | + const {keyboardShown: initial} = result.current |
| 79 | + |
| 80 | + act(() => { |
| 81 | + emitKeyboardEvent({show: true}) |
| 82 | + }) |
| 83 | + |
| 84 | + const {keyboardShown: afterOpen} = result.current |
| 85 | + |
| 86 | + act(() => { |
| 87 | + emitKeyboardEvent({show: false}) |
| 88 | + }) |
| 89 | + |
| 90 | + const {keyboardShown: afterClose} = result.current |
| 91 | + |
| 92 | + expect({initial, afterOpen, afterClose}).toEqual({ |
| 93 | + initial: false, |
| 94 | + afterOpen: true, |
| 95 | + afterClose: false, |
| 96 | + }) |
| 97 | + }) |
| 98 | + }) |
| 99 | +}) |
0 commit comments