-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathscroll-to-flat-list.test.tsx
More file actions
119 lines (101 loc) · 3.46 KB
/
scroll-to-flat-list.test.tsx
File metadata and controls
119 lines (101 loc) · 3.46 KB
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
import * as React from 'react';
import type { ScrollViewProps } from 'react-native';
import { FlatList, Text, View } from 'react-native';
import { render, screen } from '../../..';
import type { EventEntry } from '../../../test-utils/events';
import { createEventLogger } from '../../../test-utils/events';
import { userEvent } from '../..';
const data = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
function mapEventsToShortForm(events: EventEntry[]) {
return events.map((e) => [
e.name,
e.payload.nativeEvent.contentOffset.y,
e.payload.nativeEvent.contentOffset.x,
]);
}
function renderFlatListWithToolkit(props: ScrollViewProps = {}) {
const { events, logEvent } = createEventLogger();
const renderItem = (title: string) => <Text>{title}</Text>;
render(
<FlatList
testID="flatList"
onScroll={logEvent('scroll')}
onScrollBeginDrag={logEvent('scrollBeginDrag')}
onScrollEndDrag={logEvent('scrollEndDrag')}
onMomentumScrollBegin={logEvent('momentumScrollBegin')}
onMomentumScrollEnd={logEvent('momentumScrollEnd')}
onScrollToTop={logEvent('scrollToTop')}
onEndReached={logEvent('endReached')}
onViewableItemsChanged={logEvent('viewableItemsChanged')}
data={data}
renderItem={({ item }) => renderItem(item)}
{...props}
/>,
);
return { events };
}
describe('scrollTo() with FlatList', () => {
it('supports vertical drag scroll', async () => {
const { events } = renderFlatListWithToolkit();
const user = userEvent.setup();
await user.scrollTo(screen.getByTestId('flatList'), { y: 100 });
expect(mapEventsToShortForm(events)).toEqual([
['scrollBeginDrag', 0, 0],
['scroll', 25, 0],
['scroll', 50, 0],
['scroll', 75, 0],
['scrollEndDrag', 100, 0],
]);
expect(events).toMatchSnapshot('scrollTo({ y: 100 })');
});
it('supports horizontal drag scroll', async () => {
const { events } = renderFlatListWithToolkit({ horizontal: true });
const user = userEvent.setup();
await user.scrollTo(screen.getByTestId('flatList'), { x: 100 });
expect(mapEventsToShortForm(events)).toEqual([
['scrollBeginDrag', 0, 0],
['scroll', 0, 25],
['scroll', 0, 50],
['scroll', 0, 75],
['scrollEndDrag', 0, 100],
]);
});
});
const DATA = new Array(100).fill(0).map((_, i) => `Item ${i}`);
function Scrollable() {
return (
<View style={{ flex: 1 }}>
<FlatList
testID="flat-list"
data={DATA}
renderItem={(x) => <Item title={x.item} />}
initialNumToRender={10}
updateCellsBatchingPeriod={0}
/>
</View>
);
}
function Item({ title }: { title: string }) {
return (
<View>
<Text>{title}</Text>
</View>
);
}
test('scrollTo with contentSize and layoutMeasurement update FlatList content', async () => {
render(<Scrollable />);
const user = userEvent.setup();
expect(screen.getByText('Item 0')).toBeOnTheScreen();
expect(screen.getByText('Item 7')).toBeOnTheScreen();
expect(screen.queryByText('Item 15')).not.toBeOnTheScreen();
await user.scrollTo(screen.getByTestId('flat-list'), {
y: 300,
contentSize: { width: 240, height: 480 },
layoutMeasurement: { width: 240, height: 480 },
});
expect(screen.getByText('Item 0')).toBeOnTheScreen();
expect(screen.getByText('Item 7')).toBeOnTheScreen();
expect(screen.getByText('Item 15')).toBeOnTheScreen();
// Prevent act warning by unmounting the component
await screen.unmountAsync();
});