@@ -25,11 +25,11 @@ This library is a replacement for [Enzyme](http://airbnb.io/enzyme/).
25
25
## Example
26
26
27
27
``` jsx
28
- import { render } from ' react-native-testing-library' ;
28
+ import { render , fireEvent } from ' react-native-testing-library' ;
29
29
import { QuestionsBoard } from ' ../QuestionsBoard' ;
30
30
31
31
function setAnswer (question , answer ) {
32
- question . props . onChangeText ( answer);
32
+ fireEvent . changeText (question, answer);
33
33
}
34
34
35
35
test (' should verify two questions' , () => {
@@ -39,7 +39,7 @@ test('should verify two questions', () => {
39
39
setAnswer (allQuestions[0 ], ' a1' );
40
40
setAnswer (allQuestions[1 ], ' a2' );
41
41
42
- getByText (' submit' ). props . onPress ( );
42
+ fireEvent . press ( getByText (' submit' ));
43
43
44
44
expect (props .verifyQuestions ).toBeCalledWith ({
45
45
' 1' : { q: ' q1' , a: ' a1' },
@@ -148,6 +148,111 @@ test('Component has a structure', () => {
148
148
});
149
149
```
150
150
151
+ ## ` FireEvent API `
152
+
153
+ ### ` fireEvent: (element: ReactTestInstance, eventName: string, data?: *) => void `
154
+
155
+ Invokes named event handler on the element or parent element in the tree.
156
+
157
+ ``` jsx
158
+ import { View } from ' react-native' ;
159
+ import { render , fireEvent } from ' react-native-testing-library' ;
160
+ import { MyComponent } from ' ./MyComponent' ;
161
+
162
+ const onEventMock = jest .fn ();
163
+ const { getByTestId } = render (
164
+ < MyComponent testID= " custom" onMyCustomEvent= {onEventMock} / >
165
+ );
166
+
167
+ fireEvent (getByTestId (' custom' ), ' myCustomEvent' );
168
+ ```
169
+
170
+ ### ` fireEvent.press: (element: ReactTestInstance) => void `
171
+
172
+ Invokes ` press ` event handler on the element or parent element in the tree.
173
+
174
+ ``` jsx
175
+ import { View , Text , TouchableOpacity } from ' react-native' ;
176
+ import { render , fireEvent } from ' react-native-testing-library' ;
177
+
178
+ const onPressMock = jest .fn ();
179
+
180
+ const { getByTestId } = render (
181
+ < View>
182
+ < TouchableOpacity onPress= {onPressMock} testID= " button" >
183
+ < Text > Press me< / Text >
184
+ < / TouchableOpacity>
185
+ < / View>
186
+ );
187
+
188
+ fireEvent .press (getByTestId (' button' ));
189
+ ```
190
+
191
+ ### ` fireEvent.doublePress: (element: ReactTestInstance) => void `
192
+
193
+ Invokes ` doublePress ` event handler on the element or parent element in the tree.
194
+
195
+ ``` jsx
196
+ import { TouchableOpacity , Text } from ' react-native' ;
197
+ import { render , fireEvent } from ' react-native-testing-library' ;
198
+
199
+ const onDoublePressMock = jest .fn ();
200
+
201
+ const { getByTestId } = render (
202
+ < TouchableOpacity onDoublePress= {onDoublePressMock}>
203
+ < Text testID= " button-text" > Click me< / Text >
204
+ < / TouchableOpacity>
205
+ );
206
+
207
+ fireEvent .doublePress (getByTestId (' button-text' ));
208
+ ```
209
+
210
+ ### ` fireEvent.changeText: (element: ReactTestInstance, data?: *) => void `
211
+
212
+ Invokes ` changeText ` event handler on the element or parent element in the tree.
213
+
214
+ ``` jsx
215
+ import { View , TextInput } from ' react-native' ;
216
+ import { render , fireEvent } from ' react-native-testing-library' ;
217
+
218
+ const onChangeTextMock = jest .fn ();
219
+ const CHANGE_TEXT = ' content' ;
220
+
221
+ const { getByTestId } = render (
222
+ < View>
223
+ < TextInput testID= " text-input" onChangeText= {onChangeTextMock} / >
224
+ < / View>
225
+ );
226
+
227
+ fireEvent .changeText (getByTestId (' text-input' ), CHANGE_TEXT );
228
+ ```
229
+
230
+ ### ` fireEvent.scroll: (element: ReactTestInstance, data?: *) => void `
231
+
232
+ Invokes ` scroll ` event handler on the element or parent element in the tree.
233
+
234
+ ``` jsx
235
+ import { ScrollView , TextInput } from ' react-native' ;
236
+ import { render , fireEvent } from ' react-native-testing-library' ;
237
+
238
+ const onScrollMock = jest .fn ();
239
+ const eventData = {
240
+ nativeEvent: {
241
+ contentOffset: {
242
+ y: 200 ,
243
+ },
244
+ },
245
+ };
246
+
247
+ const { getByTestId } = render (
248
+ < ScrollView testID= " scroll-view" onScroll= {onScrollMock}>
249
+ < Text > XD < / Text >
250
+ < / ScrollView>
251
+ );
252
+
253
+ fireEvent .scroll (getByTestId (' scroll-view' ), eventData);
254
+ ```
255
+
151
256
## ` debug `
152
257
153
258
Log prettified shallowly rendered component or test instance (just like snapshot) to stdout.
@@ -178,6 +283,7 @@ test('fetch data', async () => {
178
283
```
179
284
180
285
<!-- badges -->
286
+
181
287
[ build-badge ] : https://img.shields.io/circleci/project/github/callstack/react-native-testing-library/master.svg?style=flat-square
182
288
[ build ] : https://circleci.com/gh/callstack/react-native-testing-library
183
289
[ version-badge ] : https://img.shields.io/npm/v/react-native-testing-library.svg?style=flat-square
0 commit comments