Skip to content

Commit 1edfd61

Browse files
committed
Add useKeyboard tests
1 parent 61bf3b0 commit 1edfd61

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

babel.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['module:metro-react-native-babel-preset'],
3+
}

jest.config.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
module.exports = {
2-
preset: 'ts-jest',
2+
testEnvironment: 'node',
3+
preset: 'react-native',
4+
globals: {
5+
'ts-jest': {
6+
tsconfig: 'tsconfig.json',
7+
},
8+
},
9+
transform: {
10+
'^.+\\.jsx$': 'babel-jest',
11+
'^.+\\.tsx?$': 'ts-jest',
12+
},
313
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
414
testPathIgnorePatterns: ['node_modules', 'lib'],
515
}

src/useKeyboard.test.ts

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)