Skip to content

Commit c0bf154

Browse files
chiciometa-codesync[bot]
authored andcommitted
Re-enabled VirtualizedList "retains batch render region when an item is appended" test (#56653)
Summary: This PR is a follow-up of #56358, in order to improve and VirtualizedList tests. I re-enabled the skipped test `retains batch render region when an item is appended`. With React 19, the previous approach that was using `jest.runAllTimersAsync` in this test path was unstable because the pre-update render region could still be processing updates. I adopted the same approach used in #56358 by introducing a small helper function, `advanceUntilLastCellIndexRendered`, which advances timers one step at a time and stops when the expected state is reached: `cellsAroundViewport.last === items.length - 1`. As part of this follow-up, I also aligned `advanceUntilRenderAreaChanged` to use performNextBatch for consistent stepwise timer advancement. ## Changelog: [GENERAL][FIXED] - Re-enabled VirtualizedList "retains batch render region when an item is appended" tes Pull Request resolved: #56653 Test Plan: - Ran the VirtualizedList-test.js and verified all test cases passed. - Verified test modification correctness by intentionally breaking the related snapshot and check that it is breaking/it is failing. Reviewed By: cortinico Differential Revision: D104291284 Pulled By: Abbondanzo fbshipit-source-id: e32f8707a837a1805ce130d526768228f684e3e7
1 parent a3e87c6 commit c0bf154

1 file changed

Lines changed: 55 additions & 45 deletions

File tree

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

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,56 +1836,49 @@ it('retains initial render region when an item is appended', async () => {
18361836
expect(component).toMatchSnapshot();
18371837
});
18381838

1839-
// TODO: Revisit this test case after upgrading to React 19.
1840-
skipTestSilenceLinter(
1841-
'retains batch render region when an item is appended',
1842-
async () => {
1843-
const items = generateItems(10);
1844-
const ITEM_HEIGHT = 10;
1839+
it('retains batch render region when an item is appended', async () => {
1840+
const items = generateItems(10);
1841+
const ITEM_HEIGHT = 10;
18451842

1846-
let component;
1847-
await act(() => {
1848-
component = create(
1849-
<VirtualizedList
1850-
initialNumToRender={1}
1851-
maxToRenderPerBatch={1}
1852-
{...baseItemProps(items)}
1853-
{...fixedHeightItemLayoutProps(ITEM_HEIGHT)}
1854-
/>,
1855-
);
1856-
});
1843+
let component;
1844+
await act(() => {
1845+
component = create(
1846+
<VirtualizedList
1847+
initialNumToRender={1}
1848+
maxToRenderPerBatch={1}
1849+
{...baseItemProps(items)}
1850+
{...fixedHeightItemLayoutProps(ITEM_HEIGHT)}
1851+
/>,
1852+
);
1853+
});
18571854

1858-
await act(() => {
1859-
simulateLayout(component, {
1860-
viewport: {width: 10, height: 50},
1861-
content: {width: 10, height: 100},
1862-
});
1863-
performAllBatches();
1855+
await act(() => {
1856+
simulateLayout(component, {
1857+
viewport: {width: 10, height: 50},
1858+
content: {width: 10, height: 100},
18641859
});
1860+
});
18651861

1866-
await act(async () => {
1867-
await jest.runAllTimersAsync();
1868-
});
1862+
await advanceUntilLastCellIndexRendered(component, items.length - 1);
18691863

1870-
await act(() => {
1871-
component.update(
1872-
<VirtualizedList
1873-
initialNumToRender={1}
1874-
maxToRenderPerBatch={1}
1875-
{...baseItemProps(items)}
1876-
{...fixedHeightItemLayoutProps(ITEM_HEIGHT)}
1877-
data={generateItems(11)}
1878-
/>,
1879-
);
1880-
});
1864+
await act(() => {
1865+
component.update(
1866+
<VirtualizedList
1867+
initialNumToRender={1}
1868+
maxToRenderPerBatch={1}
1869+
{...baseItemProps(items)}
1870+
{...fixedHeightItemLayoutProps(ITEM_HEIGHT)}
1871+
data={generateItems(11)}
1872+
/>,
1873+
);
1874+
});
18811875

1882-
// Adding an item to the list after batch render should keep the existing
1883-
// rendered items rendered. We batch render 10 items, then add an 11th. Expect
1884-
// the first ten items to be present, with a spacer for the 11th until the
1885-
// next batch render.
1886-
expect(component).toMatchSnapshot();
1887-
},
1888-
);
1876+
// Adding an item to the list after batch render should keep the existing
1877+
// rendered items rendered. We batch render 10 items, then add an 11th. Expect
1878+
// the first ten items to be present, with a spacer for the 11th until the
1879+
// next batch render.
1880+
expect(component).toMatchSnapshot();
1881+
});
18891882

18901883
it('constrains batch render region when an item is removed', async () => {
18911884
const items = generateItems(10);
@@ -2639,13 +2632,30 @@ async function advanceUntilRenderAreaChanged(component) {
26392632
}
26402633

26412634
await act(() => {
2642-
jest.advanceTimersToNextTimer(1);
2635+
performNextBatch();
26432636
});
26442637
}
26452638

26462639
throw new Error(`Render area did not change`);
26472640
}
26482641

2642+
async function advanceUntilLastCellIndexRendered(component, targetLastIndex) {
2643+
const instance = component.getInstance();
2644+
const MAX_TIMER_STEPS = 20;
2645+
2646+
for (let step = 0; step < MAX_TIMER_STEPS; step++) {
2647+
if (instance.state.cellsAroundViewport.last === targetLastIndex) {
2648+
return;
2649+
}
2650+
2651+
await act(() => {
2652+
performNextBatch();
2653+
});
2654+
}
2655+
2656+
throw new Error(`Target last index ${targetLastIndex} not rendered`);
2657+
}
2658+
26492659
function performAllBatches() {
26502660
jest.runAllTimers();
26512661
}

0 commit comments

Comments
 (0)