@@ -25,11 +25,11 @@ This library is a replacement for [Enzyme](http://airbnb.io/enzyme/).
2525## Example
2626
2727``` jsx
28- import { render } from ' react-native-testing-library' ;
28+ import { render , fireEvent } from ' react-native-testing-library' ;
2929import { QuestionsBoard } from ' ../QuestionsBoard' ;
3030
3131function setAnswer (question , answer ) {
32- question . props . onChangeText ( answer);
32+ fireEvent . changeText (question, answer);
3333}
3434
3535test (' should verify two questions' , () => {
@@ -39,7 +39,7 @@ test('should verify two questions', () => {
3939 setAnswer (allQuestions[0 ], ' a1' );
4040 setAnswer (allQuestions[1 ], ' a2' );
4141
42- getByText (' submit' ). props . onPress ( );
42+ fireEvent . press ( getByText (' submit' ));
4343
4444 expect (props .verifyQuestions ).toBeCalledWith ({
4545 ' 1' : { q: ' q1' , a: ' a1' },
@@ -148,6 +148,111 @@ test('Component has a structure', () => {
148148});
149149```
150150
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+
151256## ` debug `
152257
153258Log prettified shallowly rendered component or test instance (just like snapshot) to stdout.
@@ -178,6 +283,7 @@ test('fetch data', async () => {
178283```
179284
180285<!-- badges -->
286+
181287[ build-badge ] : https://img.shields.io/circleci/project/github/callstack/react-native-testing-library/master.svg?style=flat-square
182288[ build ] : https://circleci.com/gh/callstack/react-native-testing-library
183289[ version-badge ] : https://img.shields.io/npm/v/react-native-testing-library.svg?style=flat-square
0 commit comments