-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathFlatList.test.tsx
74 lines (65 loc) · 2.23 KB
/
FlatList.test.tsx
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
import React from 'react';
import {
act,
cleanup,
render,
screen,
userEvent,
waitForElementToBeRemoved,
} from '@testing-library/react-native';
import SectionList from '../src/components/FlatList';
const SCREEN_SIZE = {width: 240, height: 480};
const scrollDownEventData = {
y: 300,
contentSize: SCREEN_SIZE,
layoutMeasurement: SCREEN_SIZE,
};
afterEach(cleanup);
jest.useFakeTimers();
it('scrolls to bottom and loads more items', async () => {
// Render the SectionList component
render(<SectionList />);
// First dish is visible
expect(screen.getByText(/pizza/i)).toBeOnTheScreen();
// First dish from 2nd page is not visible yet
expect(() => screen.getByText(/the impossible burger/i)).toThrow(
'Unable to find an element with text: /the impossible burger/i',
);
// We haven't started loading yet
expect(() => screen.getByText(/loading more dishes/i)).toThrow(
'Unable to find an element with text: /loading more dishes/i',
);
// Simulate scrolling to the bottom of the list
const user = userEvent.setup();
await user.scrollTo(
screen.getByLabelText('dishes-list'),
scrollDownEventData,
);
await waitForElementToBeRemoved(
() => screen.getByText(/loading more dishes/i),
{
timeout: 1500,
},
);
expect(await screen.findByText(/the impossible burger/i)).toBeOnTheScreen();
});
it('refreshes when scrolling to the top', async () => {
// Render the SectionList component
render(<SectionList />);
// First dish is visible
expect(screen.getByText(/pizza/i)).toBeOnTheScreen();
// Simulate pull to refresh via refreshControl props, this can not be simulated by fireEvent.scroll
// See discussion https://github.com/callstack/react-native-testing-library/issues/809#issuecomment-984823700
const flatListTestInstance = screen.getByLabelText('dishes-list');
const {refreshControl} = flatListTestInstance.props;
await act(async () => {
refreshControl.props.onRefresh();
});
// First dish is not visible due to refresh and refreshing indicator is visible
expect(() => screen.getByText(/pizza/i)).toThrow(
'Unable to find an element with text: /pizza/i',
);
await waitForElementToBeRemoved(() => screen.getByText(/refreshing/i), {
timeout: 1500,
});
});