Skip to content

fireEvent.scroll doesn't update the FlatList's render window on React Native 0.73 #1540

Closed
@j-piasecki

Description

@j-piasecki

Describe the bug

Since facebook/react-native#38529 React Native's VirtualizedList doesn't update its contentLength when handling the scroll event. This means that using fireEvent.scroll(...) is not enough to render items outside the initialNumToRender - invoking onContentSizeChange is also necessary.

Expected behavior

Steps to Reproduce

Try the following snippet on RN 0.72 and 0.73. Without the commented-out line, the test will be failing on 73 but not on 72.

import React from 'react';
import {View, FlatList, Text} from 'react-native';
import {render, screen, fireEvent} from '@testing-library/react-native';

const DATA = new Array(100).fill(0).map((_, i) => `Item ${i}`);

function Item({title}: {title: string}) {
  return (
    <View>
      <Text>{title}</Text>
    </View>
  );
}

function Scrollable() {
  return (
    <View style={{flex: 1}}>
      <FlatList
        testID="test-flatlist"
        data={DATA}
        renderItem={x => <Item title={x.item} />}
        initialNumToRender={10}
      />
    </View>
  );
}

test('example', async () => {
  render(<Scrollable />);

  const flatlist = screen.getByTestId('test-flatlist');

  const item1 = screen.getByText('Item 0');
  const item2 = screen.getByText('Item 7');

  fireEvent.scroll(flatlist, {
    nativeEvent: {
      contentOffset: {
        y: 480,
      },
      contentSize: {
        height: 480,
        width: 240,
      },
      layoutMeasurement: {
        height: 480,
        width: 240,
      },
    },
  });

  // fireEvent(flatlist, 'onContentSizeChange', 240, 480);

  await new Promise(x => setTimeout(x, 100));

  const item3 = screen.getByText('Item 15');
});

Screenshots

Versions

npmPackages:
    @testing-library/react-native: ^12.4.1 => 12.4.1
    react: 18.2.0 => 18.2.0
    react-native: 0.73.0-rc.6 => 0.73.0-rc.6
    react-test-renderer: 18.2.0 => 18.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions