使用反应测试库在另一个元素中查找元素

Ask*_*ing 11 reactjs react-testing-library

在我的 React js 应用程序中,使用 React 测试库测试应用程序,我遇到了下一种情况。我尝试从中查找数据const d = utils.getAllByTestId('el')[0],现在我想测试内部是否d存在带有role:的元素const i = d.getByRole('img'),但我得到了d.getByRole is not a function。从文档中我得到该getByRole方法只能附加在正在使用render()方法的元素上,但我没有找到可以解决我的问题的东西。
问题:如何在反应测试库中实现我上面描述的目标?

Tyl*_*den 25

React测试库的withinAPI可用于查询嵌套元素。

const d = utils.getAllByTestId('el')[0]
const i = within(d).getByRole('img')
Run Code Online (Sandbox Code Playgroud)