/
TextInput-test.js
182 lines (158 loc) · 5.51 KB
/
TextInput-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+react_native
* @format
* @flow-strict
*/
const React = require('react');
const ReactTestRenderer = require('react-test-renderer');
const TextInput = require('../TextInput');
const ReactNative = require('../../../Renderer/shims/ReactNative');
const {
enter,
expectRendersMatchingSnapshot,
} = require('../../../Utilities/ReactNativeTestTools');
jest.unmock('../TextInput');
describe('TextInput tests', () => {
let input;
let inputRef;
let onChangeListener;
let onChangeTextListener;
const initialValue = 'initialValue';
beforeEach(() => {
inputRef = React.createRef(null);
onChangeListener = jest.fn();
onChangeTextListener = jest.fn();
function TextInputWrapper() {
const [state, setState] = React.useState({text: initialValue});
return (
<TextInput
ref={inputRef}
value={state.text}
onChangeText={text => {
onChangeTextListener(text);
setState({text});
}}
onChange={event => {
onChangeListener(event);
}}
/>
);
}
const renderTree = ReactTestRenderer.create(<TextInputWrapper />);
input = renderTree.root.findByType(TextInput);
});
it('has expected instance functions', () => {
expect(inputRef.current.isFocused).toBeInstanceOf(Function); // Would have prevented S168585
expect(inputRef.current.clear).toBeInstanceOf(Function);
expect(inputRef.current.focus).toBeInstanceOf(jest.fn().constructor);
expect(inputRef.current.blur).toBeInstanceOf(jest.fn().constructor);
expect(inputRef.current.setNativeProps).toBeInstanceOf(
jest.fn().constructor,
);
expect(inputRef.current.measure).toBeInstanceOf(jest.fn().constructor);
expect(inputRef.current.measureInWindow).toBeInstanceOf(
jest.fn().constructor,
);
expect(inputRef.current.measureLayout).toBeInstanceOf(
jest.fn().constructor,
);
});
it('calls onChange callbacks', () => {
expect(input.props.value).toBe(initialValue);
const message = 'This is a test message';
ReactTestRenderer.act(() => {
enter(input, message);
});
expect(input.props.value).toBe(message);
expect(onChangeTextListener).toHaveBeenCalledWith(message);
expect(onChangeListener).toHaveBeenCalledWith({
nativeEvent: {text: message},
});
});
function createTextInput(extraProps) {
const textInputRef = React.createRef(null);
ReactTestRenderer.create(
<TextInput ref={textInputRef} value="value1" {...extraProps} />,
);
return textInputRef;
}
it('focus() should not do anything if the TextInput is not editable', () => {
const textInputRef = createTextInput({editable: false});
// currentProps is the property actually containing props at runtime
textInputRef.current.currentProps = textInputRef.current.props;
expect(textInputRef.current.isFocused()).toBe(false);
TextInput.State.focusTextInput(textInputRef.current);
expect(textInputRef.current.isFocused()).toBe(false);
});
it('should have support for being focused and blurred', () => {
const textInputRef = createTextInput();
expect(textInputRef.current.isFocused()).toBe(false);
ReactNative.findNodeHandle = jest.fn().mockImplementation(ref => {
if (ref == null) {
return null;
}
if (
ref === textInputRef.current ||
ref === textInputRef.current.getNativeRef()
) {
return 1;
}
return 2;
});
TextInput.State.focusTextInput(textInputRef.current);
expect(textInputRef.current.isFocused()).toBe(true);
expect(TextInput.State.currentlyFocusedInput()).toBe(textInputRef.current);
TextInput.State.blurTextInput(textInputRef.current);
expect(textInputRef.current.isFocused()).toBe(false);
expect(TextInput.State.currentlyFocusedInput()).toBe(null);
});
it('should unfocus when other TextInput is focused', () => {
const textInputRe1 = React.createRef(null);
const textInputRe2 = React.createRef(null);
ReactTestRenderer.create(
<>
<TextInput ref={textInputRe1} value="value1" />
<TextInput ref={textInputRe2} value="value2" />
</>,
);
ReactNative.findNodeHandle = jest.fn().mockImplementation(ref => {
if (
ref === textInputRe1.current ||
ref === textInputRe1.current.getNativeRef()
) {
return 1;
}
if (
ref === textInputRe2.current ||
ref === textInputRe2.current.getNativeRef()
) {
return 2;
}
return 3;
});
expect(textInputRe1.current.isFocused()).toBe(false);
expect(textInputRe2.current.isFocused()).toBe(false);
TextInput.State.focusTextInput(textInputRe1.current);
expect(textInputRe1.current.isFocused()).toBe(true);
expect(textInputRe2.current.isFocused()).toBe(false);
expect(TextInput.State.currentlyFocusedInput()).toBe(textInputRe1.current);
TextInput.State.focusTextInput(textInputRe2.current);
expect(textInputRe1.current.isFocused()).toBe(false);
expect(textInputRe2.current.isFocused()).toBe(true);
expect(TextInput.State.currentlyFocusedInput()).toBe(textInputRe2.current);
});
it('should render as expected', () => {
expectRendersMatchingSnapshot(
'TextInput',
() => <TextInput />,
() => {
jest.dontMock('../TextInput');
},
);
});
});