I'm trying to write test cases in React Native using Jest and Testing Library. I have a .map
function where it renders a component with TouchableOpacity
.
{data.map((item, index) => (
<SafeAreaView key={index}>
<ItemView data={item} index={index} />
{index >= data.length - 1 ? (
<View style={{ marginTop: 16 }} />
) : (
<View style={styles.itmSeperator} />
)}
</SafeAreaView>
))}
The ItemView looks like
const ItemView = ({ data, index }) => (
<TouchableOpacity
testID="TouchablePress"
onPress={() => onPress(data)}
style={{
alignItems: "center",
paddingVertical: 16,
}}
>
<Text style={styles.text}>{data.name}</Text>
</TouchableOpacity>
);
For the above if I call
fireEvent(getByTestId("TouchablePress"), "press");
in tests file the test is getting file stating found multiple component with same id "TouchablePress".
Any suggestions on how to cover this test without getting failed?