Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find an element inside another using react testing library

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?

like image 746
Asking Avatar asked Dec 03 '25 07:12

Asking


1 Answers

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')
like image 64
Tyler Durden Avatar answered Dec 04 '25 23:12

Tyler Durden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!