'Jest snapshot testing of FlatList renderItem

I have a wrapper for Flatlist called FlatListShadow but for this post FlatListShadow and FlatList is the same thing

I need to test the renderItem function in FlatListShadow which looks like this

renderItem={({ item }) => (
      <Device
        title={item.deviceName}
        platform={item.platform}
        updatedAt={item.updatedAt}
        status={item.status}
        selectDevice={() => selectDevice(item.deviceId)}
        isSelected={selectedDeviceIdList.includes(item.deviceId)}
      />
    )}

Unfortuanately in the snapshot it only gives this information

renderItem={[Function]}


Solution 1:[1]

If you are using jest :

describe('EasyToUseSection', () => {
  it.only('flatlist should return renderItem correctly', () => {
    const itemData = {
      name: 'Name',
      desc: 'Description',
    };
   const { getByTestId } = renderComponent();
   expect(getByTestId('flatlist')).toBeDefined();
   const element = getByTestId('flatlist').props.renderItem(itemData);
   expect(element.props.data).toStrictEqual(itemData);
   expect(element.type).toBe(Device);
   });
});

This way the data that is sent is checked and also the component rendered type can be checked

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rachel Cynthia