@@ -2164,10 +2164,70 @@ it('virtualizes away last focused index if item removed', () => {
2164
2164
expect ( component ) . toMatchSnapshot ( ) ;
2165
2165
} ) ;
2166
2166
2167
- function generateItems ( count ) {
2167
+ it ( 'handles maintainVisibleContentPosition' , ( ) => {
2168
+ const items = generateItems ( 20 ) ;
2169
+ const ITEM_HEIGHT = 10 ;
2170
+
2171
+ let component ;
2172
+ ReactTestRenderer . act ( ( ) => {
2173
+ component = ReactTestRenderer . create (
2174
+ < VirtualizedList
2175
+ initialNumToRender = { 1 }
2176
+ windowSize = { 1 }
2177
+ maintainVisibleContentPosition = { { minIndexForVisible : 0 } }
2178
+ { ...baseItemProps ( items ) }
2179
+ { ...fixedHeightItemLayoutProps ( ITEM_HEIGHT ) }
2180
+ /> ,
2181
+ ) ;
2182
+ } ) ;
2183
+
2184
+ ReactTestRenderer . act ( ( ) => {
2185
+ simulateLayout ( component , {
2186
+ viewport : { width : 10 , height : 50 } ,
2187
+ content : { width : 10 , height : items . length * ITEM_HEIGHT } ,
2188
+ } ) ;
2189
+
2190
+ performAllBatches ( ) ;
2191
+ } ) ;
2192
+
2193
+ // Initial render.
2194
+ expect ( component ) . toMatchSnapshot ( ) ;
2195
+
2196
+ // Add new items at the start of the list to trigger the maintainVisibleContentPosition adjustment.
2197
+ const newItems = [ ...generateItems ( 10 , items . length ) , ...items ] ;
2198
+ ReactTestRenderer . act ( ( ) => {
2199
+ component . update (
2200
+ < VirtualizedList
2201
+ initialNumToRender = { 1 }
2202
+ windowSize = { 1 }
2203
+ maintainVisibleContentPosition = { { minIndexForVisible : 0 } }
2204
+ { ...baseItemProps ( newItems ) }
2205
+ { ...fixedHeightItemLayoutProps ( ITEM_HEIGHT ) }
2206
+ /> ,
2207
+ ) ;
2208
+ } ) ;
2209
+
2210
+ // Previously rendered cells should be rendered still as well as the newly added ones.
2211
+ expect ( component ) . toMatchSnapshot ( ) ;
2212
+
2213
+ // Simulate scroll adjustment from native maintainVisibleContentPosition.
2214
+ ReactTestRenderer . act ( ( ) => {
2215
+ simulateContentLayout ( component , {
2216
+ width : 10 ,
2217
+ height : newItems . length * ITEM_HEIGHT ,
2218
+ } ) ;
2219
+ simulateScroll ( component , { x : 0 , y : 10 * ITEM_HEIGHT } ) ;
2220
+ performAllBatches ( ) ;
2221
+ } ) ;
2222
+
2223
+ // Only previously rendered cells should be rendered.
2224
+ expect ( component ) . toMatchSnapshot ( ) ;
2225
+ } ) ;
2226
+
2227
+ function generateItems ( count , startKey = 0 ) {
2168
2228
return Array ( count )
2169
2229
. fill ( )
2170
- . map ( ( _ , i ) => ( { key : i } ) ) ;
2230
+ . map ( ( _ , i ) => ( { key : i + startKey } ) ) ;
2171
2231
}
2172
2232
2173
2233
function generateItemsStickyEveryN ( count , n ) {
0 commit comments