19

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?

0

1 Answer 1

37

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.