From fe06cbfbf332a999e065bdc13ef5cdeed20c5517 Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Sun, 16 Jun 2024 23:02:19 -0700 Subject: [PATCH] Add missing `act`s to VirtualizedList-test and migrate to async act (#44997) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/44997 Wrap `VirtualizedList-test` uses of `react-test-renderer` in `act` as appropriate, so as to pass under current React and mostly pass under React 19, with further fixes to come. Changelog: [Internal] Reviewed By: robhogan Differential Revision: D58649295 fbshipit-source-id: 5e0fa791d581fbf004a2ca7eaa5c4b4d9a15ddfe --- .../Lists/__tests__/VirtualizedList-test.js | 1360 +++++++++-------- 1 file changed, 759 insertions(+), 601 deletions(-) diff --git a/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js b/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js index 2a5214438bc..33ea0d60c2f 100644 --- a/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js +++ b/packages/virtualized-lists/Lists/__tests__/VirtualizedList-test.js @@ -18,49 +18,58 @@ import {act, create} from 'react-test-renderer'; jest.useFakeTimers(); describe('VirtualizedList', () => { - it('renders simple list', () => { - const component = create( - } - getItem={(data, index) => data[index]} - getItemCount={data => data.length} - />, - ); + it('renders simple list', async () => { + let component; + await act(() => { + component = create( + } + getItem={(data, index) => data[index]} + getItemCount={data => data.length} + />, + ); + }); expect(component).toMatchSnapshot(); }); - it('renders simple list using ListItemComponent', () => { + it('renders simple list using ListItemComponent', async () => { function ListItemComponent({item}) { return ; } - const component = create( - data[index]} - getItemCount={data => data.length} - />, - ); + let component; + await act(() => { + component = create( + data[index]} + getItemCount={data => data.length} + />, + ); + }); expect(component).toMatchSnapshot(); }); - it('warns if both renderItem or ListItemComponent are specified. Uses ListItemComponent', () => { + it('warns if both renderItem or ListItemComponent are specified. Uses ListItemComponent', async () => { jest.spyOn(console, 'warn').mockImplementationOnce(() => {}); function ListItemComponent({item}) { return ; } - const component = create( - ( - - )} - getItem={(data, index) => data[index]} - getItemCount={data => data.length} - />, - ); + let component; + await act(() => { + component = create( + ( + + )} + getItem={(data, index) => data[index]} + getItemCount={data => data.length} + />, + ); + }); expect(console.warn).toBeCalledWith( 'VirtualizedList: Both ListItemComponent and renderItem props are present. ListItemComponent will take precedence over renderItem.', @@ -97,29 +106,35 @@ describe('VirtualizedList', () => { console.error.mockRestore(); }); - it('renders empty list', () => { - const component = create( - } - getItem={(data, index) => data[index]} - getItemCount={data => data.length} - />, - ); + it('renders empty list', async () => { + let component; + await act(() => { + component = create( + } + getItem={(data, index) => data[index]} + getItemCount={data => data.length} + />, + ); + }); expect(component).toMatchSnapshot(); }); - it('renders empty list after batch', () => { - const component = create( - } - getItem={(data, index) => data[index]} - getItemCount={data => data.length} - />, - ); + it('renders empty list after batch', async () => { + let component; + await act(() => { + component = create( + } + getItem={(data, index) => data[index]} + getItemCount={data => data.length} + />, + ); + }); - act(() => { + await act(() => { simulateLayout(component, { viewport: {width: 10, height: 50}, content: {width: 10, height: 200}, @@ -131,148 +146,177 @@ describe('VirtualizedList', () => { expect(component).toMatchSnapshot(); }); - it('renders null list', () => { - const component = create( - } - getItem={(data, index) => data[index]} - getItemCount={data => 0} - />, - ); + it('renders null list', async () => { + let component; + await act(() => { + component = create( + } + getItem={(data, index) => data[index]} + getItemCount={data => 0} + />, + ); + }); expect(component).toMatchSnapshot(); }); - it('scrollToEnd works with null list', () => { + it('scrollToEnd works with null list', async () => { const listRef = React.createRef(null); - create( - } - getItem={(data, index) => data[index]} - getItemCount={data => 0} - ref={listRef} - />, - ); + await act(() => { + create( + } + getItem={(data, index) => data[index]} + getItemCount={data => 0} + ref={listRef} + />, + ); + }); listRef.current.scrollToEnd(); }); - it('renders empty list with empty component', () => { - const component = create( - } - ListFooterComponent={() =>