docs: add fireEvent.scroll example for FlatList (#223)

This commit is contained in:
ajubin
2019-09-27 10:24:54 +02:00
committed by Michał Pierzchała
parent fa7ad1bd32
commit 69d148023f
+40 -1
View File
@@ -211,8 +211,10 @@ fireEvent.changeText(getByTestId('text-input'), CHANGE_TEXT);
Invokes `scroll` event handler on the element or parent element in the tree.
#### On a `ScrollView`
```jsx
import { ScrollView, TextInput } from 'react-native';
import { ScrollView, Text } from 'react-native';
import { render, fireEvent } from 'react-native-testing-library';
const onScrollMock = jest.fn();
@@ -233,6 +235,43 @@ const { getByTestId } = render(
fireEvent.scroll(getByTestId('scroll-view'), eventData);
```
#### On a `FlatList`
```jsx
import { FlatList, View } from 'react-native';
import { render, fireEvent } from 'react-native-testing-library';
const onEndReached = jest.fn();
const { getByType } = render(
<FlatList
data={Array.from({ length: 10 }, (_, key) => ({ key: `${key}` }))}
renderItem={() => <View style={{ height: 500, width: 100 }} />}
onEndReached={onEndReached}
onEndReachedThreshold={0.2}
/>
);
const eventData = {
nativeEvent: {
contentOffset: {
y: 500,
},
contentSize: {
// Dimensions of the scrollable content
height: 500,
width: 100,
},
layoutMeasurement: {
// Dimensions of the device
height: 100,
width: 100,
},
},
};
fireEvent.scroll(getByType(ScrollView), eventData);
expect(onEndReached).toHaveBeenCalled();
```
## `waitForElement`
- [`Example code`](https://github.com/callstack/react-native-testing-library/blob/master/src/__tests__/waitForElement.test.js)