React Native enzyme Testing Flatlist onPress

Viewed 434

I have a FlatList which renders TouchableOpacity components. These touchables have a function call onPress which I want to test. I am using enzyme. Here is the flatList component. The testing error is onPress I get TypeError: touchableList.simulate is not a function

  <FlatList
    keyboardShouldPersistTaps={"handled"}
    data={autoCompleteValues}
    renderItem={({ item }: { item: POI }) => (
      <TouchableOpacity
        testID={"touchableList"}
        onPress={() => setLocation(item)}
        key={item.displayName}
        style={styles.list}
      >
        <Text style={styles.text}>{item.displayName}</Text>
        <Entypo name={"chevron-thin-right"} size={24} color={"#454F63"} />
      </TouchableOpacity>
    )}
  />

Test code, this gives an error TypeError: touchableList.simulate is not a function

  it("should run provided function on press", () => {
    const mockSetLocation = jest.fn();

    const wrapper = shallow(
      <AutoComplete
        autoCompleteValues={mockPOIs}
        setLocation={mockSetLocation}
      />
    );
    const touchableList = wrapper
      .find("FlatList")
      .props()
      .renderItem({ item: { displayName: "test" } });
    expect(touchableList.type).toBe(TouchableOpacity);
    touchableList.simulate("press");

  });
0 Answers
Related