【问题标题】:Find an element inside another using react testing library使用反应测试库在另一个元素中查找元素
【发布时间】:2022-01-24 13:20:56
【问题描述】:

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

【问题讨论】:

    标签: reactjs react-testing-library


    【解决方案1】:

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

    const d = utils.getAllByTestId('el')[0]
    const i = within(d).getByRole('img')
    

    【讨论】:

      猜你喜欢
      • 2016-01-21
      • 2015-06-02
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      相关资源
      最近更新 更多