forked from Shopify/flash-list
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGridLayoutProviderWithProps.test.ts
More file actions
179 lines (161 loc) · 6.21 KB
/
GridLayoutProviderWithProps.test.ts
File metadata and controls
179 lines (161 loc) · 6.21 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { ScrollView } from "react-native";
import { ProgressiveListView } from "recyclerlistview";
import type FlashList from "../FlashList";
import { mountFlashList } from "./helpers/mountFlashList";
describe("GridLayoutProviderWithProps", () => {
it("updates average window on layout manager change", () => {
const flashList = mountFlashList();
const oldAverageWindow = (flashList.instance as FlashList<any>).state
.layoutProvider["averageWindow"];
// width change from default 400 to 600 will force layout manager to change
flashList.find(ScrollView)?.trigger("onLayout", {
nativeEvent: { layout: { height: 900, width: 600 } },
});
const newAverageWindow =
flashList.instance.state.layoutProvider["averageWindow"];
expect(newAverageWindow).not.toBe(oldAverageWindow);
flashList.unmount();
});
it("average window's size is two times the number of items that will fill the screen", () => {
const flashList = mountFlashList({ numColumns: 2 });
expect(
flashList.instance.state.layoutProvider["averageWindow"]["inputValues"]
.length
).toBe(20);
flashList.find(ScrollView)?.trigger("onLayout", {
nativeEvent: { layout: { height: 2000, width: 600 } },
});
expect(
flashList.instance.state.layoutProvider["averageWindow"]["inputValues"]
.length
).toBe(40);
flashList.setProps({ numColumns: 1 });
expect(
flashList.instance.state.layoutProvider["averageWindow"]["inputValues"]
.length
).toBe(20);
flashList.unmount();
});
it("average window should not be less than 6 in size", () => {
const flashList = mountFlashList();
flashList.find(ScrollView)?.trigger("onLayout", {
nativeEvent: { layout: { height: 100, width: 100 } },
});
expect(
flashList.instance.state.layoutProvider["averageWindow"]["inputValues"]
.length
).toBe(6);
flashList.unmount();
});
it("vertical list: average should not update when widths change", () => {
const flashList = mountFlashList();
const layoutProvider = flashList.instance.state.layoutProvider;
const oldAverage = layoutProvider.averageItemSize;
layoutProvider.getLayoutManager()!.getLayouts()[0].width = 500;
flashList.find(ProgressiveListView)?.instance.onItemLayout(0);
expect(oldAverage).toBe(layoutProvider.averageItemSize);
flashList.unmount();
});
it("horizontal list: average should not update when heights change", () => {
const flashList = mountFlashList({ horizontal: true });
const layoutProvider = flashList.instance.state.layoutProvider;
const oldAverage = layoutProvider.averageItemSize;
layoutProvider.getLayoutManager()!.getLayouts()[0].height = 600;
// Can throw a no op set state warning. Should be handled in PLV.
flashList.find(ProgressiveListView)?.instance.onItemLayout(0);
expect(oldAverage).toBe(layoutProvider.averageItemSize);
flashList.unmount();
});
it("computes correct average", () => {
const flashList = mountFlashList();
const layoutProvider = flashList.instance.state.layoutProvider;
expect(layoutProvider.averageItemSize).toBe(200);
const layouts = layoutProvider.getLayoutManager()!.getLayouts();
const progressiveListView = flashList.find(ProgressiveListView);
layouts[0].height = 100;
layouts[1].height = 200;
layouts[2].height = 300;
layouts[3].height = 400;
progressiveListView?.instance.onItemLayout(0);
progressiveListView?.instance.onItemLayout(1);
progressiveListView?.instance.onItemLayout(2);
progressiveListView?.instance.onItemLayout(3);
// estimatedItemSize is treated as one of the values. That's why 240.
expect(layoutProvider.averageItemSize).toBe(240);
flashList.unmount();
});
it("updates all cached widths for vertical list and heights for horizontal list when orientation changes", () => {
const runCacheUpdateTest = (horizontal: boolean) => {
const flashList = mountFlashList({
data: new Array(20).fill("1"),
horizontal,
});
const progressiveListView = flashList.find(ProgressiveListView);
const layoutProvider = flashList.instance.state.layoutProvider;
layoutProvider
.getLayoutManager()!
.getLayouts()
.forEach((layout, index) => {
// marking layouts as if they're actual rendered sizes
progressiveListView?.instance.onItemLayout(index);
// checking size
if (horizontal) {
expect(layout.height).toBe(900);
} else {
expect(layout.width).toBe(400);
}
});
// simulates orientation change
flashList.find(ScrollView)?.trigger("onLayout", {
nativeEvent: { layout: { height: 400, width: 900 } },
});
layoutProvider
.getLayoutManager()!
.getLayouts()
.forEach((layout) => {
// making sure all widths or heights are updated
if (horizontal) {
expect(layout.height).toBe(400);
} else {
expect(layout.width).toBe(900);
}
// making sure extra keys don't make their way to layout unnecessarily
expect(Object.keys(layout).length).toBe(6);
});
flashList.unmount();
};
// vertical list
runCacheUpdateTest(false);
// horizontal list
runCacheUpdateTest(true);
});
it("expires if column count or padding changes", () => {
const flashList = mountFlashList();
const baseProps = flashList.instance.props;
expect(
flashList.instance.state.layoutProvider.updateProps({
...baseProps,
contentContainerStyle: { paddingTop: 10 },
}).hasExpired
).toBe(false);
expect(
flashList.instance.state.layoutProvider.updateProps({
...baseProps,
contentContainerStyle: { paddingBottom: 10 },
}).hasExpired
).toBe(false);
expect(
flashList.instance.state.layoutProvider.updateProps({
...baseProps,
contentContainerStyle: { paddingLeft: 10 },
}).hasExpired
).toBe(true);
flashList.instance.state.layoutProvider["_hasExpired"] = false;
expect(
flashList.instance.state.layoutProvider.updateProps({
...baseProps,
numColumns: 2,
}).hasExpired
).toBe(true);
});
});