Home > Back-end >  Find an element inside another using react testing library
Find an element inside another using react testing library

Time:12-24

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?

CodePudding user response:

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')
  • Related