Skip to content

Commit ed156ff

Browse files
Add test
1 parent 217fecb commit ed156ff

File tree

2 files changed

+609
-2
lines changed

2 files changed

+609
-2
lines changed

packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,10 +2115,70 @@ it('virtualizes away last focused index if item removed', () => {
21152115
expect(component).toMatchSnapshot();
21162116
});
21172117

2118-
function generateItems(count) {
2118+
it('handles maintainVisibleContentPosition', () => {
2119+
const items = generateItems(20);
2120+
const ITEM_HEIGHT = 10;
2121+
2122+
let component;
2123+
ReactTestRenderer.act(() => {
2124+
component = ReactTestRenderer.create(
2125+
<VirtualizedList
2126+
initialNumToRender={1}
2127+
windowSize={1}
2128+
maintainVisibleContentPosition={{minIndexForVisible: 0}}
2129+
{...baseItemProps(items)}
2130+
{...fixedHeightItemLayoutProps(ITEM_HEIGHT)}
2131+
/>,
2132+
);
2133+
});
2134+
2135+
ReactTestRenderer.act(() => {
2136+
simulateLayout(component, {
2137+
viewport: {width: 10, height: 50},
2138+
content: {width: 10, height: items.length * ITEM_HEIGHT},
2139+
});
2140+
2141+
performAllBatches();
2142+
});
2143+
2144+
// Initial render.
2145+
expect(component).toMatchSnapshot();
2146+
2147+
// Add new items at the start of the list to trigger the maintainVisibleContentPosition adjustment.
2148+
const newItems = [...generateItems(10, items.length), ...items];
2149+
ReactTestRenderer.act(() => {
2150+
component.update(
2151+
<VirtualizedList
2152+
initialNumToRender={1}
2153+
windowSize={1}
2154+
maintainVisibleContentPosition={{minIndexForVisible: 0}}
2155+
{...baseItemProps(newItems)}
2156+
{...fixedHeightItemLayoutProps(ITEM_HEIGHT)}
2157+
/>,
2158+
);
2159+
});
2160+
2161+
// Previously rendered cells should be rendered still as well as the newly added ones.
2162+
expect(component).toMatchSnapshot();
2163+
2164+
// Simulate scroll adjustment from native maintainVisibleContentPosition.
2165+
ReactTestRenderer.act(() => {
2166+
simulateContentLayout(component, {
2167+
width: 10,
2168+
height: newItems.length * ITEM_HEIGHT,
2169+
});
2170+
simulateScroll(component, {x: 0, y: 10 * ITEM_HEIGHT});
2171+
performAllBatches();
2172+
});
2173+
2174+
// Only previously rendered cells should be rendered.
2175+
expect(component).toMatchSnapshot();
2176+
});
2177+
2178+
function generateItems(count, startKey = 0) {
21192179
return Array(count)
21202180
.fill()
2121-
.map((_, i) => ({key: i}));
2181+
.map((_, i) => ({key: i + startKey}));
21222182
}
21232183

21242184
function generateItemsStickyEveryN(count, n) {

0 commit comments

Comments
 (0)