Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
janicduplessis committed Feb 23, 2023
1 parent 217fecb commit ed156ff
Show file tree
Hide file tree
Showing 2 changed files with 609 additions and 2 deletions.
64 changes: 62 additions & 2 deletions packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2115,10 +2115,70 @@ it('virtualizes away last focused index if item removed', () => {
expect(component).toMatchSnapshot();
});

function generateItems(count) {
it('handles maintainVisibleContentPosition', () => {
const items = generateItems(20);
const ITEM_HEIGHT = 10;

let component;
ReactTestRenderer.act(() => {
component = ReactTestRenderer.create(
<VirtualizedList
initialNumToRender={1}
windowSize={1}
maintainVisibleContentPosition={{minIndexForVisible: 0}}
{...baseItemProps(items)}
{...fixedHeightItemLayoutProps(ITEM_HEIGHT)}
/>,
);
});

ReactTestRenderer.act(() => {
simulateLayout(component, {
viewport: {width: 10, height: 50},
content: {width: 10, height: items.length * ITEM_HEIGHT},
});

performAllBatches();
});

// Initial render.
expect(component).toMatchSnapshot();

// Add new items at the start of the list to trigger the maintainVisibleContentPosition adjustment.
const newItems = [...generateItems(10, items.length), ...items];
ReactTestRenderer.act(() => {
component.update(
<VirtualizedList
initialNumToRender={1}
windowSize={1}
maintainVisibleContentPosition={{minIndexForVisible: 0}}
{...baseItemProps(newItems)}
{...fixedHeightItemLayoutProps(ITEM_HEIGHT)}
/>,
);
});

// Previously rendered cells should be rendered still as well as the newly added ones.
expect(component).toMatchSnapshot();

// Simulate scroll adjustment from native maintainVisibleContentPosition.
ReactTestRenderer.act(() => {
simulateContentLayout(component, {
width: 10,
height: newItems.length * ITEM_HEIGHT,
});
simulateScroll(component, {x: 0, y: 10 * ITEM_HEIGHT});
performAllBatches();
});

// Only previously rendered cells should be rendered.
expect(component).toMatchSnapshot();
});

function generateItems(count, startKey = 0) {
return Array(count)
.fill()
.map((_, i) => ({key: i}));
.map((_, i) => ({key: i + startKey}));
}

function generateItemsStickyEveryN(count, n) {
Expand Down
Loading

0 comments on commit ed156ff

Please sign in to comment.