Find an element inside another using react testing library
Asked Answered
B

1

15

In my react js application, testing the application using react testing library, i have the next situation. I try to find the data from const d = utils.getAllByTestId('el')[0], and now i want to test if inside d exists an element that has a role: const i = d.getByRole('img'), but i get d.getByRole is not a function. From the documentation i got that getByRole method could be attached only using on the element that is using render() method, but i did not find something that could solve my issue.
Question: How to achieve what i describe above in react testing library?

Balefire answered 23/12, 2021 at 15:8 Comment(0)
C
33

React Testing Library's within API can be used to query the nested element.

const d = utils.getAllByTestId('el')[0]
const i = within(d).getByRole('img')
Cheremkhovo answered 23/12, 2021 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.