@shopify/flash-list 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/CHANGELOG.md +159 -0
  2. package/LICENSE.md +7 -0
  3. package/README.md +65 -0
  4. package/RNFlashList.podspec +26 -0
  5. package/android/build.gradle +59 -0
  6. package/android/src/main/AndroidManifest.xml +3 -0
  7. package/android/src/main/kotlin/com/shopify/reactnative/flash_list/AutoLayoutShadow.kt +94 -0
  8. package/android/src/main/kotlin/com/shopify/reactnative/flash_list/AutoLayoutView.kt +79 -0
  9. package/android/src/main/kotlin/com/shopify/reactnative/flash_list/AutoLayoutViewManager.kt +69 -0
  10. package/android/src/main/kotlin/com/shopify/reactnative/flash_list/CellContainer.java +16 -0
  11. package/android/src/main/kotlin/com/shopify/reactnative/flash_list/CellContainerImpl.kt +16 -0
  12. package/android/src/main/kotlin/com/shopify/reactnative/flash_list/CellContainerManager.kt +27 -0
  13. package/android/src/main/kotlin/com/shopify/reactnative/flash_list/FlashListPackage.kt +19 -0
  14. package/android/src/test/java/com/shopify/reactnative/flash_list/AutoLayoutShadowTest.kt +146 -0
  15. package/android/src/test/java/com/shopify/reactnative/flash_list/models/Rect.kt +59 -0
  16. package/android/src/test/java/com/shopify/reactnative/flash_list/models/TestCollection.kt +6 -0
  17. package/android/src/test/java/com/shopify/reactnative/flash_list/models/TestDataModel.kt +8 -0
  18. package/android/src/test/resources/LayoutTestData.json +708 -0
  19. package/dist/AnimatedFlashList.d.ts +6 -0
  20. package/dist/AnimatedFlashList.d.ts.map +1 -0
  21. package/dist/AnimatedFlashList.js +8 -0
  22. package/dist/AnimatedFlashList.js.map +1 -0
  23. package/dist/FlashList.d.ts +121 -0
  24. package/dist/FlashList.d.ts.map +1 -0
  25. package/dist/FlashList.js +502 -0
  26. package/dist/FlashList.js.map +1 -0
  27. package/dist/FlashListProps.d.ts +251 -0
  28. package/dist/FlashListProps.d.ts.map +1 -0
  29. package/dist/FlashListProps.js +3 -0
  30. package/dist/FlashListProps.js.map +1 -0
  31. package/dist/GridLayoutProviderWithProps.d.ts +30 -0
  32. package/dist/GridLayoutProviderWithProps.d.ts.map +1 -0
  33. package/dist/GridLayoutProviderWithProps.js +80 -0
  34. package/dist/GridLayoutProviderWithProps.js.map +1 -0
  35. package/dist/PureComponentWrapper.d.ts +22 -0
  36. package/dist/PureComponentWrapper.d.ts.map +1 -0
  37. package/dist/PureComponentWrapper.js +37 -0
  38. package/dist/PureComponentWrapper.js.map +1 -0
  39. package/dist/__tests__/AverageWindow.test.d.ts +2 -0
  40. package/dist/__tests__/AverageWindow.test.d.ts.map +1 -0
  41. package/dist/__tests__/AverageWindow.test.js +69 -0
  42. package/dist/__tests__/AverageWindow.test.js.map +1 -0
  43. package/dist/__tests__/FlashList.test.d.ts +2 -0
  44. package/dist/__tests__/FlashList.test.d.ts.map +1 -0
  45. package/dist/__tests__/FlashList.test.js +656 -0
  46. package/dist/__tests__/FlashList.test.js.map +1 -0
  47. package/dist/__tests__/GridLayoutProviderWithProps.test.d.ts +2 -0
  48. package/dist/__tests__/GridLayoutProviderWithProps.test.d.ts.map +1 -0
  49. package/dist/__tests__/GridLayoutProviderWithProps.test.js +133 -0
  50. package/dist/__tests__/GridLayoutProviderWithProps.test.js.map +1 -0
  51. package/dist/__tests__/PlatformHelper.web.test.d.ts +2 -0
  52. package/dist/__tests__/PlatformHelper.web.test.d.ts.map +1 -0
  53. package/dist/__tests__/PlatformHelper.web.test.js +25 -0
  54. package/dist/__tests__/PlatformHelper.web.test.js.map +1 -0
  55. package/dist/__tests__/ViewabilityHelper.test.d.ts +2 -0
  56. package/dist/__tests__/ViewabilityHelper.test.d.ts.map +1 -0
  57. package/dist/__tests__/ViewabilityHelper.test.js +187 -0
  58. package/dist/__tests__/ViewabilityHelper.test.js.map +1 -0
  59. package/dist/__tests__/helpers/mountFlashList.d.ts +20 -0
  60. package/dist/__tests__/helpers/mountFlashList.d.ts.map +1 -0
  61. package/dist/__tests__/helpers/mountFlashList.js +44 -0
  62. package/dist/__tests__/helpers/mountFlashList.js.map +1 -0
  63. package/dist/__tests__/useBlankAreaTracker.test.d.ts +2 -0
  64. package/dist/__tests__/useBlankAreaTracker.test.d.ts.map +1 -0
  65. package/dist/__tests__/useBlankAreaTracker.test.js +179 -0
  66. package/dist/__tests__/useBlankAreaTracker.test.js.map +1 -0
  67. package/dist/benchmark/AutoScrollHelper.d.ts +18 -0
  68. package/dist/benchmark/AutoScrollHelper.d.ts.map +1 -0
  69. package/dist/benchmark/AutoScrollHelper.js +68 -0
  70. package/dist/benchmark/AutoScrollHelper.js.map +1 -0
  71. package/dist/benchmark/JSFPSMonitor.d.ts +23 -0
  72. package/dist/benchmark/JSFPSMonitor.d.ts.map +1 -0
  73. package/dist/benchmark/JSFPSMonitor.js +65 -0
  74. package/dist/benchmark/JSFPSMonitor.js.map +1 -0
  75. package/dist/benchmark/roundToDecimalPlaces.d.ts +2 -0
  76. package/dist/benchmark/roundToDecimalPlaces.d.ts.map +1 -0
  77. package/dist/benchmark/roundToDecimalPlaces.js +9 -0
  78. package/dist/benchmark/roundToDecimalPlaces.js.map +1 -0
  79. package/dist/benchmark/useBenchmark.d.ts +35 -0
  80. package/dist/benchmark/useBenchmark.d.ts.map +1 -0
  81. package/dist/benchmark/useBenchmark.js +167 -0
  82. package/dist/benchmark/useBenchmark.js.map +1 -0
  83. package/dist/benchmark/useBlankAreaTracker.d.ts +34 -0
  84. package/dist/benchmark/useBlankAreaTracker.d.ts.map +1 -0
  85. package/dist/benchmark/useBlankAreaTracker.js +67 -0
  86. package/dist/benchmark/useBlankAreaTracker.js.map +1 -0
  87. package/dist/benchmark/useDataMultiplier.d.ts +9 -0
  88. package/dist/benchmark/useDataMultiplier.d.ts.map +1 -0
  89. package/dist/benchmark/useDataMultiplier.js +25 -0
  90. package/dist/benchmark/useDataMultiplier.js.map +1 -0
  91. package/dist/benchmark/useFlatListBenchmark.d.ts +13 -0
  92. package/dist/benchmark/useFlatListBenchmark.d.ts.map +1 -0
  93. package/dist/benchmark/useFlatListBenchmark.js +100 -0
  94. package/dist/benchmark/useFlatListBenchmark.js.map +1 -0
  95. package/dist/errors/CustomError.d.ts +8 -0
  96. package/dist/errors/CustomError.d.ts.map +1 -0
  97. package/dist/errors/CustomError.js +14 -0
  98. package/dist/errors/CustomError.js.map +1 -0
  99. package/dist/errors/ExceptionList.d.ts +20 -0
  100. package/dist/errors/ExceptionList.d.ts.map +1 -0
  101. package/dist/errors/ExceptionList.js +22 -0
  102. package/dist/errors/ExceptionList.js.map +1 -0
  103. package/dist/errors/Warnings.d.ts +10 -0
  104. package/dist/errors/Warnings.d.ts.map +1 -0
  105. package/dist/errors/Warnings.js +15 -0
  106. package/dist/errors/Warnings.js.map +1 -0
  107. package/dist/index.d.ts +13 -0
  108. package/dist/index.d.ts.map +1 -0
  109. package/dist/index.js +28 -0
  110. package/dist/index.js.map +1 -0
  111. package/dist/native/auto-layout/AutoLayoutView.d.ts +21 -0
  112. package/dist/native/auto-layout/AutoLayoutView.d.ts.map +1 -0
  113. package/dist/native/auto-layout/AutoLayoutView.js +48 -0
  114. package/dist/native/auto-layout/AutoLayoutView.js.map +1 -0
  115. package/dist/native/auto-layout/AutoLayoutViewNativeComponent.d.ts +4 -0
  116. package/dist/native/auto-layout/AutoLayoutViewNativeComponent.d.ts.map +1 -0
  117. package/dist/native/auto-layout/AutoLayoutViewNativeComponent.js +6 -0
  118. package/dist/native/auto-layout/AutoLayoutViewNativeComponent.js.map +1 -0
  119. package/dist/native/auto-layout/AutoLayoutViewNativeComponent.web.d.ts +5 -0
  120. package/dist/native/auto-layout/AutoLayoutViewNativeComponent.web.d.ts.map +1 -0
  121. package/dist/native/auto-layout/AutoLayoutViewNativeComponent.web.js +6 -0
  122. package/dist/native/auto-layout/AutoLayoutViewNativeComponent.web.js.map +1 -0
  123. package/dist/native/auto-layout/AutoLayoutViewNativeComponentProps.d.ts +14 -0
  124. package/dist/native/auto-layout/AutoLayoutViewNativeComponentProps.d.ts.map +1 -0
  125. package/dist/native/auto-layout/AutoLayoutViewNativeComponentProps.js +3 -0
  126. package/dist/native/auto-layout/AutoLayoutViewNativeComponentProps.js.map +1 -0
  127. package/dist/native/cell-container/CellContainer.d.ts +6 -0
  128. package/dist/native/cell-container/CellContainer.d.ts.map +1 -0
  129. package/dist/native/cell-container/CellContainer.js +9 -0
  130. package/dist/native/cell-container/CellContainer.js.map +1 -0
  131. package/dist/native/cell-container/CellContainer.web.d.ts +7 -0
  132. package/dist/native/cell-container/CellContainer.web.d.ts.map +1 -0
  133. package/dist/native/cell-container/CellContainer.web.js +13 -0
  134. package/dist/native/cell-container/CellContainer.web.js.map +1 -0
  135. package/dist/tsconfig.tsbuildinfo +1 -0
  136. package/dist/utils/AverageWindow.d.ts +21 -0
  137. package/dist/utils/AverageWindow.d.ts.map +1 -0
  138. package/dist/utils/AverageWindow.js +49 -0
  139. package/dist/utils/AverageWindow.js.map +1 -0
  140. package/dist/utils/PlatformHelper.d.ts +14 -0
  141. package/dist/utils/PlatformHelper.d.ts.map +1 -0
  142. package/dist/utils/PlatformHelper.js +16 -0
  143. package/dist/utils/PlatformHelper.js.map +1 -0
  144. package/dist/utils/PlatformHelper.web.d.ts +14 -0
  145. package/dist/utils/PlatformHelper.web.d.ts.map +1 -0
  146. package/dist/utils/PlatformHelper.web.js +18 -0
  147. package/dist/utils/PlatformHelper.web.js.map +1 -0
  148. package/dist/viewability/ViewToken.d.ts +8 -0
  149. package/dist/viewability/ViewToken.d.ts.map +1 -0
  150. package/dist/viewability/ViewToken.js +3 -0
  151. package/dist/viewability/ViewToken.js.map +1 -0
  152. package/dist/viewability/ViewabilityHelper.d.ts +25 -0
  153. package/dist/viewability/ViewabilityHelper.d.ts.map +1 -0
  154. package/dist/viewability/ViewabilityHelper.js +104 -0
  155. package/dist/viewability/ViewabilityHelper.js.map +1 -0
  156. package/dist/viewability/ViewabilityManager.d.ts +24 -0
  157. package/dist/viewability/ViewabilityManager.d.ts.map +1 -0
  158. package/dist/viewability/ViewabilityManager.js +94 -0
  159. package/dist/viewability/ViewabilityManager.js.map +1 -0
  160. package/ios/RNFlashList.xcodeproj/project.pbxproj +3 -0
  161. package/ios/RNFlashList.xcodeproj/project.xcworkspace/contents.xcworkspacedata +4 -0
  162. package/ios/Sources/AutoLayoutView.swift +218 -0
  163. package/ios/Sources/AutoLayoutViewManager.m +14 -0
  164. package/ios/Sources/AutoLayoutViewManager.swift +12 -0
  165. package/ios/Sources/CellContainer.swift +9 -0
  166. package/ios/Sources/CellContainerManager.m +8 -0
  167. package/ios/Sources/CellContainerManager.swift +12 -0
  168. package/ios/Sources/FlatListPro-Bridging-Header.h +8 -0
  169. package/ios/Tests/AutoLayoutViewTests.swift +113 -0
  170. package/jestSetup.js +15 -0
  171. package/package.json +75 -0
  172. package/src/AnimatedFlashList.ts +11 -0
  173. package/src/FlashList.tsx +801 -0
  174. package/src/FlashListProps.ts +312 -0
  175. package/src/GridLayoutProviderWithProps.ts +137 -0
  176. package/src/PureComponentWrapper.tsx +42 -0
  177. package/src/__tests__/AverageWindow.test.ts +80 -0
  178. package/src/__tests__/FlashList.test.tsx +738 -0
  179. package/src/__tests__/GridLayoutProviderWithProps.test.ts +150 -0
  180. package/src/__tests__/PlatformHelper.web.test.ts +29 -0
  181. package/src/__tests__/ViewabilityHelper.test.ts +283 -0
  182. package/src/__tests__/helpers/mountFlashList.tsx +62 -0
  183. package/src/__tests__/useBlankAreaTracker.test.tsx +206 -0
  184. package/src/benchmark/AutoScrollHelper.ts +70 -0
  185. package/src/benchmark/JSFPSMonitor.ts +74 -0
  186. package/src/benchmark/roundToDecimalPlaces.ts +4 -0
  187. package/src/benchmark/useBenchmark.ts +240 -0
  188. package/src/benchmark/useBlankAreaTracker.ts +117 -0
  189. package/src/benchmark/useDataMultiplier.ts +19 -0
  190. package/src/benchmark/useFlatListBenchmark.ts +107 -0
  191. package/src/errors/CustomError.ts +10 -0
  192. package/src/errors/ExceptionList.ts +23 -0
  193. package/src/errors/Warnings.ts +18 -0
  194. package/src/index.ts +32 -0
  195. package/src/native/auto-layout/AutoLayoutView.tsx +72 -0
  196. package/src/native/auto-layout/AutoLayoutViewNativeComponent.ts +7 -0
  197. package/src/native/auto-layout/AutoLayoutViewNativeComponent.web.ts +8 -0
  198. package/src/native/auto-layout/AutoLayoutViewNativeComponentProps.ts +14 -0
  199. package/src/native/cell-container/CellContainer.ts +7 -0
  200. package/src/native/cell-container/CellContainer.web.tsx +9 -0
  201. package/src/utils/AverageWindow.ts +49 -0
  202. package/src/utils/PlatformHelper.ts +16 -0
  203. package/src/utils/PlatformHelper.web.ts +20 -0
  204. package/src/viewability/ViewToken.ts +7 -0
  205. package/src/viewability/ViewabilityHelper.ts +162 -0
  206. package/src/viewability/ViewabilityManager.ts +133 -0
@@ -0,0 +1,738 @@
1
+ import React, { useEffect } from "react";
2
+ import { ScrollView, Text } from "react-native";
3
+ import "@quilted/react-testing/matchers";
4
+ import { ProgressiveListView } from "recyclerlistview";
5
+
6
+ import Warnings from "../errors/Warnings";
7
+ import AutoLayoutView from "../native/auto-layout/AutoLayoutView";
8
+ import CellContainer from "../native/cell-container/CellContainer";
9
+
10
+ import { mountFlashList } from "./helpers/mountFlashList";
11
+
12
+ describe("FlashList", () => {
13
+ beforeEach(() => {
14
+ jest.clearAllMocks();
15
+ jest.useFakeTimers();
16
+ });
17
+
18
+ it("renders items", () => {
19
+ const flashList = mountFlashList();
20
+ expect(flashList).toContainReactComponent(Text, { children: "One" });
21
+ expect(flashList).toContainReactComponent(ProgressiveListView, {
22
+ isHorizontal: false,
23
+ });
24
+ });
25
+
26
+ it("sets ProgressiveListView to horizontal", () => {
27
+ const flashList = mountFlashList({ horizontal: true });
28
+ expect(flashList).toContainReactComponent(ProgressiveListView, {
29
+ isHorizontal: true,
30
+ });
31
+ });
32
+
33
+ it("calls prepareForLayoutAnimationRender", () => {
34
+ const flashList = mountFlashList({
35
+ keyExtractor: (item) => item,
36
+ });
37
+ const warn = jest.spyOn(console, "warn").mockReturnValue();
38
+ const prepareForLayoutAnimationRender = jest.spyOn(
39
+ flashList.instance!.recyclerlistview_unsafe!,
40
+ "prepareForLayoutAnimationRender"
41
+ );
42
+ flashList.instance.prepareForLayoutAnimationRender();
43
+ expect(prepareForLayoutAnimationRender).toHaveBeenCalledTimes(1);
44
+ expect(warn).not.toHaveBeenCalled();
45
+ });
46
+
47
+ it("sends a warning when prepareForLayoutAnimationRender without keyExtractor", () => {
48
+ const flashList = mountFlashList();
49
+ const warn = jest.spyOn(console, "warn").mockReturnValue();
50
+ const prepareForLayoutAnimationRender = jest.spyOn(
51
+ flashList.instance!.recyclerlistview_unsafe!,
52
+ "prepareForLayoutAnimationRender"
53
+ );
54
+ flashList.instance.prepareForLayoutAnimationRender();
55
+ expect(prepareForLayoutAnimationRender).not.toHaveBeenCalled();
56
+ expect(warn).toHaveBeenCalledWith(Warnings.missingKeyExtractor);
57
+ });
58
+
59
+ it("disables initial scroll correction on recyclerlistview if initialScrollIndex is in first row", () => {
60
+ let flashList = mountFlashList({ initialScrollIndex: 0, numColumns: 3 });
61
+ expect(
62
+ flashList.instance["getUpdatedWindowCorrectionConfig"]()
63
+ .applyToInitialOffset
64
+ ).toBe(false);
65
+
66
+ flashList = mountFlashList({ initialScrollIndex: 3, numColumns: 3 });
67
+ expect(
68
+ flashList.instance["getUpdatedWindowCorrectionConfig"]()
69
+ .applyToInitialOffset
70
+ ).toBe(true);
71
+
72
+ flashList = mountFlashList({ initialScrollIndex: 2, numColumns: 3 });
73
+ expect(
74
+ flashList.instance["getUpdatedWindowCorrectionConfig"]()
75
+ .applyToInitialOffset
76
+ ).toBe(false);
77
+ });
78
+
79
+ it("assigns distance from window to window correction object", () => {
80
+ const flashList = mountFlashList({ estimatedFirstItemOffset: 100 });
81
+ expect(
82
+ flashList.instance["getUpdatedWindowCorrectionConfig"]().value.windowShift
83
+ ).toBe(-100);
84
+ });
85
+
86
+ it("only forwards onBlankArea prop to AutoLayout when needed", () => {
87
+ const flashList = mountFlashList();
88
+ const autoLayoutView = flashList.find(AutoLayoutView)?.instance;
89
+ expect(autoLayoutView.props.onBlankAreaEvent).toBeUndefined();
90
+ flashList.setProps({ onBlankArea: () => {} });
91
+ expect(autoLayoutView.props.onBlankAreaEvent).not.toBeUndefined();
92
+ });
93
+
94
+ it("calls render item only when data of the items has changed", () => {
95
+ const renderItemMock = jest.fn(({ item }) => {
96
+ return <Text>{item}</Text>;
97
+ });
98
+ const flashList = mountFlashList({
99
+ renderItem: renderItemMock,
100
+ data: ["One", "Two", "Three", "Four"],
101
+ });
102
+
103
+ // because we have 4 data items
104
+ expect(renderItemMock).toHaveBeenCalledTimes(4);
105
+ // reset counter
106
+ renderItemMock.mockClear();
107
+ // changes layout of all four items
108
+ flashList.setProps({ numColumns: 2 });
109
+ // render item should be called 0 times because only layout of items would have changed
110
+ expect(renderItemMock).toHaveBeenCalledTimes(0);
111
+ flashList.unmount();
112
+ });
113
+
114
+ it("keeps component mounted based on prepareForLayoutAnimationRender being called", () => {
115
+ // Tracks components being unmounted
116
+ const unmountMock = jest.fn();
117
+ const Item = ({ text }: { text: string }) => {
118
+ useEffect(() => {
119
+ return unmountMock;
120
+ }, []);
121
+ return <Text>{text}</Text>;
122
+ };
123
+
124
+ const flashList = mountFlashList({
125
+ keyExtractor: (item) => item,
126
+ renderItem: ({ item }) => {
127
+ return <Item text={item} />;
128
+ },
129
+ data: ["One", "Two", "Three", "Four"],
130
+ });
131
+
132
+ // Change data without prepareForLayoutAnimationRender
133
+ flashList.setProps({ data: ["One", "Two", "Three", "Five"] });
134
+ expect(unmountMock).not.toHaveBeenCalled();
135
+
136
+ // Before changing data, we run prepareForLayoutAnimationRender.
137
+ // This ensures component gets unmounted instead of being recycled to ensure layout animations run as expected.
138
+ flashList.instance.prepareForLayoutAnimationRender();
139
+ flashList.setProps({ data: ["One", "Two", "Three", "Six"] });
140
+ expect(unmountMock).toHaveBeenCalledTimes(1);
141
+ });
142
+
143
+ it("fires onLoad event", () => {
144
+ const onLoadMock = jest.fn();
145
+
146
+ // empty list
147
+ mountFlashList({ data: [], onLoad: onLoadMock });
148
+ expect(onLoadMock).toHaveBeenCalledWith({
149
+ elapsedTimeInMs: expect.any(Number),
150
+ });
151
+
152
+ onLoadMock.mockClear();
153
+
154
+ // non-empty list
155
+ const flashList = mountFlashList({ onLoad: onLoadMock });
156
+ flashList.find(ProgressiveListView)?.instance.onItemLayout(0);
157
+ expect(onLoadMock).toHaveBeenCalledWith({
158
+ elapsedTimeInMs: expect.any(Number),
159
+ });
160
+ });
161
+
162
+ it("loads an empty state", () => {
163
+ const EmptyComponent = () => {
164
+ return <Text>Empty</Text>;
165
+ };
166
+ const flashList = mountFlashList({
167
+ data: [],
168
+ ListEmptyComponent: EmptyComponent,
169
+ });
170
+ expect(flashList).toContainReactComponent(EmptyComponent);
171
+ });
172
+ it("loads header and footer in empty state", () => {
173
+ const HeaderComponent = () => {
174
+ return <Text>Empty</Text>;
175
+ };
176
+ const FooterComponent = () => {
177
+ return <Text>Empty</Text>;
178
+ };
179
+ const flashList = mountFlashList({
180
+ data: [],
181
+ ListHeaderComponent: HeaderComponent,
182
+ ListFooterComponent: FooterComponent,
183
+ });
184
+ expect(flashList).toContainReactComponent(HeaderComponent);
185
+ expect(flashList).toContainReactComponent(FooterComponent);
186
+ });
187
+
188
+ it("reports layout changes to the layout provider", () => {
189
+ const flashList = mountFlashList();
190
+ const reportItemLayoutMock = jest.spyOn(
191
+ flashList.instance.state.layoutProvider,
192
+ "reportItemLayout"
193
+ );
194
+ flashList.find(ProgressiveListView)?.instance.onItemLayout(0);
195
+ expect(reportItemLayoutMock).toHaveBeenCalledWith(0);
196
+ flashList.unmount();
197
+ });
198
+
199
+ it("should prefer overrideItemLayout over estimate and average", () => {
200
+ const flashList = mountFlashList({
201
+ overrideItemLayout: (layout) => {
202
+ layout.size = 50;
203
+ },
204
+ });
205
+ expect(flashList.instance.state.layoutProvider.averageItemSize).toBe(200);
206
+ expect(
207
+ flashList.instance.state
208
+ .layoutProvider!.getLayoutManager()!
209
+ .getLayouts()[0].height
210
+ ).toBe(50);
211
+ });
212
+
213
+ it("should override span with overrideItemLayout", () => {
214
+ const renderItemMock = jest.fn(({ item }) => {
215
+ return <Text>{item}</Text>;
216
+ });
217
+ mountFlashList({
218
+ overrideItemLayout: (layout) => {
219
+ layout.span = 2;
220
+ },
221
+ numColumns: 2,
222
+ estimatedItemSize: 300,
223
+ renderItem: renderItemMock,
224
+ });
225
+ expect(renderItemMock).toHaveBeenCalledTimes(3);
226
+
227
+ renderItemMock.mockClear();
228
+ mountFlashList({
229
+ overrideItemLayout: (layout, _, index) => {
230
+ if (index > 2) {
231
+ layout.span = 2;
232
+ }
233
+ },
234
+ data: new Array(20).fill(""),
235
+ numColumns: 3,
236
+ estimatedItemSize: 100,
237
+ renderItem: renderItemMock,
238
+ });
239
+
240
+ expect(renderItemMock).toHaveBeenCalledTimes(11);
241
+ });
242
+
243
+ it("overrideItemLayout should consider 0 as a valid span", () => {
244
+ const renderItemMock = jest.fn(({ item }) => {
245
+ return <Text>{item}</Text>;
246
+ });
247
+ mountFlashList({
248
+ overrideItemLayout: (layout, _, index) => {
249
+ if (index < 4) {
250
+ layout.span = 0;
251
+ }
252
+ },
253
+ data: new Array(20).fill(""),
254
+ numColumns: 2,
255
+ renderItem: renderItemMock,
256
+ });
257
+ expect(renderItemMock).toHaveBeenCalledTimes(14);
258
+ });
259
+
260
+ it("reports onViewableItemsChanged for viewable items", () => {
261
+ const onViewableItemsChanged = jest.fn();
262
+ const onViewableItemsChangedForItemVisiblePercentThreshold = jest.fn();
263
+ const flashList = mountFlashList({
264
+ estimatedItemSize: 300,
265
+ viewabilityConfig: {
266
+ minimumViewTime: 250,
267
+ },
268
+ viewabilityConfigCallbackPairs: [
269
+ {
270
+ onViewableItemsChanged:
271
+ onViewableItemsChangedForItemVisiblePercentThreshold,
272
+ viewabilityConfig: {
273
+ itemVisiblePercentThreshold: 50,
274
+ waitForInteraction: true,
275
+ },
276
+ },
277
+ ],
278
+ onViewableItemsChanged,
279
+ });
280
+
281
+ // onViewableItemsChanged is not called before 250 ms have elapsed
282
+ expect(onViewableItemsChanged).not.toHaveBeenCalled();
283
+
284
+ jest.advanceTimersByTime(250);
285
+ // Initial viewable items
286
+ expect(onViewableItemsChanged).toHaveBeenCalledWith({
287
+ changed: [
288
+ {
289
+ index: 0,
290
+ isViewable: true,
291
+ item: "One",
292
+ key: "0",
293
+ timestamp: expect.any(Number),
294
+ },
295
+ {
296
+ index: 1,
297
+ isViewable: true,
298
+ item: "Two",
299
+ key: "1",
300
+ timestamp: expect.any(Number),
301
+ },
302
+ {
303
+ index: 2,
304
+ isViewable: true,
305
+ item: "Three",
306
+ key: "2",
307
+ timestamp: expect.any(Number),
308
+ },
309
+ ],
310
+ viewableItems: [
311
+ {
312
+ index: 0,
313
+ isViewable: true,
314
+ item: "One",
315
+ key: "0",
316
+ timestamp: expect.any(Number),
317
+ },
318
+ {
319
+ index: 1,
320
+ isViewable: true,
321
+ item: "Two",
322
+ key: "1",
323
+ timestamp: expect.any(Number),
324
+ },
325
+ {
326
+ index: 2,
327
+ isViewable: true,
328
+ item: "Three",
329
+ key: "2",
330
+ timestamp: expect.any(Number),
331
+ },
332
+ ],
333
+ });
334
+ expect(
335
+ onViewableItemsChangedForItemVisiblePercentThreshold
336
+ ).not.toHaveBeenCalled();
337
+
338
+ // onViewableItemsChangedForItemVisiblePercentThreshold waits for interaction before reporting viewable items
339
+ flashList.instance.recordInteraction();
340
+ jest.advanceTimersByTime(250);
341
+ expect(
342
+ onViewableItemsChangedForItemVisiblePercentThreshold
343
+ ).toHaveBeenCalledWith({
344
+ changed: [
345
+ {
346
+ index: 0,
347
+ isViewable: true,
348
+ item: "One",
349
+ key: "0",
350
+ timestamp: expect.any(Number),
351
+ },
352
+ {
353
+ index: 1,
354
+ isViewable: true,
355
+ item: "Two",
356
+ key: "1",
357
+ timestamp: expect.any(Number),
358
+ },
359
+ {
360
+ index: 2,
361
+ isViewable: true,
362
+ item: "Three",
363
+ key: "2",
364
+ timestamp: expect.any(Number),
365
+ },
366
+ ],
367
+ viewableItems: [
368
+ {
369
+ index: 0,
370
+ isViewable: true,
371
+ item: "One",
372
+ key: "0",
373
+ timestamp: expect.any(Number),
374
+ },
375
+ {
376
+ index: 1,
377
+ isViewable: true,
378
+ item: "Two",
379
+ key: "1",
380
+ timestamp: expect.any(Number),
381
+ },
382
+ {
383
+ index: 2,
384
+ isViewable: true,
385
+ item: "Three",
386
+ key: "2",
387
+ timestamp: expect.any(Number),
388
+ },
389
+ ],
390
+ });
391
+
392
+ onViewableItemsChanged.mockReset();
393
+ onViewableItemsChangedForItemVisiblePercentThreshold.mockReset();
394
+ // Mocking a scroll that will make the first item not visible and the last item visible
395
+ jest
396
+ .spyOn(
397
+ flashList.instance!.recyclerlistview_unsafe!,
398
+ "getCurrentScrollOffset"
399
+ )
400
+ .mockReturnValue(200);
401
+ flashList.instance!.recyclerlistview_unsafe!.props.onVisibleIndicesChanged?.(
402
+ [0, 1, 2, 3],
403
+ [],
404
+ []
405
+ );
406
+ flashList.instance!.recyclerlistview_unsafe!.props.onScroll?.(
407
+ { nativeEvent: { contentOffset: { x: 0, y: 200 } } },
408
+ 0,
409
+ 200
410
+ );
411
+ jest.advanceTimersByTime(250);
412
+ expect(onViewableItemsChanged).toHaveBeenCalledWith({
413
+ changed: [
414
+ {
415
+ index: 3,
416
+ isViewable: true,
417
+ item: "Four",
418
+ key: "3",
419
+ timestamp: expect.any(Number),
420
+ },
421
+ ],
422
+ viewableItems: [
423
+ {
424
+ index: 0,
425
+ isViewable: true,
426
+ item: "One",
427
+ key: "0",
428
+ timestamp: expect.any(Number),
429
+ },
430
+ {
431
+ index: 1,
432
+ isViewable: true,
433
+ item: "Two",
434
+ key: "1",
435
+ timestamp: expect.any(Number),
436
+ },
437
+ {
438
+ index: 2,
439
+ isViewable: true,
440
+ item: "Three",
441
+ key: "2",
442
+ timestamp: expect.any(Number),
443
+ },
444
+ {
445
+ index: 3,
446
+ isViewable: true,
447
+ item: "Four",
448
+ key: "3",
449
+ timestamp: expect.any(Number),
450
+ },
451
+ ],
452
+ });
453
+ expect(
454
+ onViewableItemsChangedForItemVisiblePercentThreshold
455
+ ).toHaveBeenCalledWith({
456
+ changed: [
457
+ {
458
+ index: 3,
459
+ isViewable: true,
460
+ item: "Four",
461
+ key: "3",
462
+ timestamp: expect.any(Number),
463
+ },
464
+ {
465
+ index: 0,
466
+ isViewable: false,
467
+ item: "One",
468
+ key: "0",
469
+ timestamp: expect.any(Number),
470
+ },
471
+ ],
472
+ viewableItems: [
473
+ {
474
+ index: 1,
475
+ isViewable: true,
476
+ item: "Two",
477
+ key: "1",
478
+ timestamp: expect.any(Number),
479
+ },
480
+ {
481
+ index: 2,
482
+ isViewable: true,
483
+ item: "Three",
484
+ key: "2",
485
+ timestamp: expect.any(Number),
486
+ },
487
+ {
488
+ index: 3,
489
+ isViewable: true,
490
+ item: "Four",
491
+ key: "3",
492
+ timestamp: expect.any(Number),
493
+ },
494
+ ],
495
+ });
496
+ });
497
+
498
+ it("viewability reports take into account estimatedFirstItemOffset", () => {
499
+ const onViewableItemsChanged = jest.fn();
500
+ mountFlashList({
501
+ estimatedFirstItemOffset: 200,
502
+ estimatedItemSize: 300,
503
+ onViewableItemsChanged,
504
+ viewabilityConfig: { itemVisiblePercentThreshold: 50 },
505
+ });
506
+
507
+ // onViewableItemsChanged is not called before 250 ms have elapsed
508
+ expect(onViewableItemsChanged).not.toHaveBeenCalled();
509
+
510
+ jest.advanceTimersByTime(250);
511
+ // Initial viewable items
512
+ expect(onViewableItemsChanged).toHaveBeenCalledWith({
513
+ changed: [
514
+ {
515
+ index: 0,
516
+ isViewable: true,
517
+ item: "One",
518
+ key: "0",
519
+ timestamp: expect.any(Number),
520
+ },
521
+ {
522
+ index: 1,
523
+ isViewable: true,
524
+ item: "Two",
525
+ key: "1",
526
+ timestamp: expect.any(Number),
527
+ },
528
+ ],
529
+ viewableItems: [
530
+ {
531
+ index: 0,
532
+ isViewable: true,
533
+ item: "One",
534
+ key: "0",
535
+ timestamp: expect.any(Number),
536
+ },
537
+ {
538
+ index: 1,
539
+ isViewable: true,
540
+ item: "Two",
541
+ key: "1",
542
+ timestamp: expect.any(Number),
543
+ },
544
+ ],
545
+ });
546
+ });
547
+
548
+ it("should not overlap header with sitcky index 0", () => {
549
+ const HeaderComponent = () => {
550
+ return <Text>Empty</Text>;
551
+ };
552
+ const flashList = mountFlashList({
553
+ ListHeaderComponent: HeaderComponent,
554
+ stickyHeaderIndices: [0],
555
+ });
556
+ // If sticky renders there'll be 6
557
+ expect(flashList.findAll(Text).length).toBe(5);
558
+ });
559
+ it("rerenders all items when layout manager changes", () => {
560
+ let countMounts = 0;
561
+ let currentId = 0;
562
+
563
+ // Effect will be triggered once per mount
564
+ const RenderComponent = ({ id }: { id?: number }) => {
565
+ useEffect(() => {
566
+ countMounts++;
567
+ }, [id]);
568
+ return <Text>Test</Text>;
569
+ };
570
+ const renderItem = () => {
571
+ return <RenderComponent id={currentId} />;
572
+ };
573
+ const flashList = mountFlashList({
574
+ data: new Array(100).fill("1"),
575
+ estimatedItemSize: 70,
576
+ renderItem,
577
+ });
578
+
579
+ const scrollTo = (y: number) => {
580
+ flashList.find(ScrollView)?.trigger("onScroll", {
581
+ nativeEvent: { contentOffset: { x: 0, y } },
582
+ });
583
+ };
584
+
585
+ // Mocking some scrolls
586
+ scrollTo(200);
587
+ scrollTo(400);
588
+ scrollTo(600);
589
+ scrollTo(3000);
590
+ scrollTo(2000);
591
+
592
+ // changing id will trigger effects if components rerender
593
+ currentId = 1;
594
+
595
+ // capturing current component count to check later
596
+ const currentComponentCount = countMounts;
597
+
598
+ // resetting count
599
+ countMounts = 0;
600
+
601
+ // items widths before layout manager change should be 400
602
+ flashList.findAll(CellContainer).forEach((cell) => {
603
+ expect(cell.instance.props.style.width).toBe(400);
604
+ });
605
+
606
+ // This will cause a layout manager change
607
+ flashList.find(ScrollView)?.trigger("onLayout", {
608
+ nativeEvent: { layout: { height: 400, width: 900 } },
609
+ });
610
+
611
+ // If counts match, then all components were updated
612
+ expect(countMounts).toBe(currentComponentCount);
613
+
614
+ // items widths after layout manager change should be 900
615
+ flashList.findAll(CellContainer).forEach((cell) => {
616
+ expect(cell.instance.props.style.width).toBe(900);
617
+ });
618
+
619
+ flashList.unmount();
620
+ });
621
+ it("sends a warning when estimatedItemSize is not set", () => {
622
+ const warn = jest.spyOn(console, "warn").mockReturnValue();
623
+
624
+ const flashList = mountFlashList({
625
+ disableDefaultEstimatedItemSize: true,
626
+ renderItem: ({ item }) => <Text style={{ height: 200 }}>{item}</Text>,
627
+ });
628
+ const layoutData = flashList.instance.state.layoutProvider
629
+ .getLayoutManager()!
630
+ .getLayouts();
631
+ layoutData[0].height = 100;
632
+ layoutData[1].height = 200;
633
+ layoutData[2].height = 300;
634
+ flashList.find(ProgressiveListView)?.instance.onItemLayout(0);
635
+ expect(flashList.instance.state.layoutProvider.averageItemSize).toBe(100);
636
+ flashList.find(ProgressiveListView)?.instance.onItemLayout(1);
637
+ flashList.find(ProgressiveListView)?.instance.onItemLayout(2);
638
+ jest.advanceTimersByTime(1000);
639
+ const averageItemSize =
640
+ flashList.instance.state.layoutProvider.averageItemSize;
641
+ expect(warn).toHaveBeenCalledWith(
642
+ Warnings.estimatedItemSizeMissingWarning.replace(
643
+ "@size",
644
+ averageItemSize.toString()
645
+ )
646
+ );
647
+ expect(flashList.instance.state.layoutProvider.averageItemSize).toBe(175);
648
+ flashList.unmount();
649
+ });
650
+ it("clears size warning timeout on unmount", () => {
651
+ const warn = jest.spyOn(console, "warn").mockReturnValue();
652
+
653
+ const flashList = mountFlashList({
654
+ disableDefaultEstimatedItemSize: true,
655
+ });
656
+ flashList.find(ProgressiveListView)?.instance.onItemLayout(0);
657
+ flashList.unmount();
658
+ jest.advanceTimersByTime(1000);
659
+ expect(warn).toBeCalledTimes(0);
660
+ });
661
+ it("measure size of horizontal list when appropriate", () => {
662
+ let flashList = mountFlashList({
663
+ data: new Array(1).fill("1"),
664
+ horizontal: true,
665
+ });
666
+ const forceUpdate = jest.spyOn(flashList.instance, "forceUpdate");
667
+ // should contain 1 actual text and 1 dummy on mount
668
+ expect(flashList.findAll(Text).length).toBe(2);
669
+
670
+ // Trigger onLoad
671
+ flashList.instance["onItemLayout"](0);
672
+ jest.advanceTimersByTime(600);
673
+
674
+ expect(forceUpdate).toBeCalledTimes(1);
675
+
676
+ // TODO: Investigate why forceUpdate isn't working in tests, forcing an update
677
+ flashList.setProps({ overrideItemLayout: () => {} });
678
+
679
+ // After update the dummy should get removed
680
+ expect(flashList.findAll(Text).length).toBe(1);
681
+
682
+ flashList.unmount();
683
+
684
+ flashList = mountFlashList({
685
+ data: new Array(1).fill("1"),
686
+ horizontal: true,
687
+ disableHorizontalListHeightMeasurement: true,
688
+ });
689
+ // should contain 1 actual text as measurement is disabled
690
+ expect(flashList.findAll(Text).length).toBe(1);
691
+ flashList.unmount();
692
+ });
693
+ it("cancels post load setTimeout on unmount", () => {
694
+ const flashList = mountFlashList({
695
+ data: new Array(1).fill("1"),
696
+ horizontal: true,
697
+ });
698
+ const forceUpdate = jest.spyOn(flashList.instance, "forceUpdate");
699
+ flashList.instance["onItemLayout"](0);
700
+ flashList.unmount();
701
+ jest.advanceTimersByTime(600);
702
+ expect(forceUpdate).toBeCalledTimes(0);
703
+ });
704
+ it("uses 250 as draw distance on Android/iOS", () => {
705
+ const flashList = mountFlashList();
706
+ flashList.find(ProgressiveListView)?.instance.onItemLayout(0);
707
+ jest.advanceTimersByTime(1000);
708
+ expect(
709
+ flashList
710
+ .find(ProgressiveListView)
711
+ ?.instance.getCurrentRenderAheadOffset()
712
+ ).toBe(250);
713
+ flashList.unmount();
714
+ });
715
+ it("force updates items only when renderItem change", () => {
716
+ const renderItem = jest.fn(() => <Text>Test</Text>);
717
+ const flashList = mountFlashList({
718
+ data: new Array(1).fill("1"),
719
+ renderItem,
720
+ });
721
+ flashList.setProps({ data: new Array(1).fill("1") });
722
+ expect(renderItem).toBeCalledTimes(1);
723
+ const newRenderItem = jest.fn(() => <Text>Test</Text>);
724
+ flashList.setProps({
725
+ data: new Array(1).fill("1"),
726
+ renderItem: newRenderItem,
727
+ });
728
+ expect(newRenderItem).toBeCalledTimes(1);
729
+ });
730
+ it("forwards disableAutoLayout prop correctly", () => {
731
+ const flashList = mountFlashList();
732
+ expect(flashList.find(AutoLayoutView)?.props.disableAutoLayout).toBe(
733
+ undefined
734
+ );
735
+ flashList.setProps({ disableAutoLayout: true });
736
+ expect(flashList.find(AutoLayoutView)?.props.disableAutoLayout).toBe(true);
737
+ });
738
+ });