使用 React 测试库在另一个元素中查找元素
Find an element inside another using 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()
方法的元素上,但我没有找到可以解决我的问题的东西。
问:如何在 React 测试库中实现我上面描述的内容?
React Testing Library的within
API可以用来查询嵌套元素
const d = utils.getAllByTestId('el')[0]
const i = within(d).getByRole('img')
在我的 react js 应用程序中,使用 react 测试库测试应用程序,我有下一个情况。我尝试从 const d = utils.getAllByTestId('el')[0]
中查找数据,现在我想测试 d
中是否存在具有 role
的元素:const i = d.getByRole('img')
,但我得到 d.getByRole is not a function
.从文档中我得到 getByRole
方法只能附加在使用 render()
方法的元素上,但我没有找到可以解决我的问题的东西。
问:如何在 React 测试库中实现我上面描述的内容?
React Testing Library的within
API可以用来查询嵌套元素
const d = utils.getAllByTestId('el')[0]
const i = within(d).getByRole('img')