Skip to content

Commit d684bf0

Browse files
Add test
1 parent 6209521 commit d684bf0

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
@@ -2164,10 +2164,70 @@ it('virtualizes away last focused index if item removed', () => {
21642164
expect(component).toMatchSnapshot();
21652165
});
21662166

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) {
21682228
return Array(count)
21692229
.fill()
2170-
.map((_, i) => ({key: i}));
2230+
.map((_, i) => ({key: i + startKey}));
21712231
}
21722232

21732233
function generateItemsStickyEveryN(count, n) {

0 commit comments

Comments
 (0)