diff --git a/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js b/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js index d649fca7723d..b02757cadf9f 100644 --- a/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js +++ b/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js @@ -1612,11 +1612,10 @@ it('adjusts render area with non-zero initialScrollIndex', async () => { content: {width: 10, height: 200}, }); simulateScroll(component, {x: 0, y: 10}); // simulate scroll offset for initialScrollIndex - - // TODO: Rewrite test to tolerate subtle timing changes. - jest.advanceTimersToNextTimer(3); }); + await advanceUntilRenderAreaChanged(component); + // We should expand the render area after receiving a message indicating we // arrived at initialScrollIndex. expect(component).toMatchSnapshot(); @@ -2624,6 +2623,29 @@ function simulateScroll(component, position) { }); } +async function advanceUntilRenderAreaChanged(component) { + const instance = component.getInstance(); + const initialRenderArea = instance.state.cellsAroundViewport; + const MAX_TIMER_STEPS = 20; + + for (let step = 0; step < MAX_TIMER_STEPS; step++) { + const currentRenderArea = instance.state.cellsAroundViewport; + const renderAreaChanged = + currentRenderArea.first !== initialRenderArea.first || + currentRenderArea.last !== initialRenderArea.last; + + if (renderAreaChanged) { + return; + } + + await act(() => { + jest.advanceTimersToNextTimer(1); + }); + } + + throw new Error(`Render area did not change`); +} + function performAllBatches() { jest.runAllTimers(); }