Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading