【问题标题】:Unable to access input from the label that is its parent with dom-testing-library无法使用 dom-testing-library 从作为其父标签的标签访问输入
【发布时间】:2018-08-19 20:51:57
【问题描述】:

当标签环绕输入时,我发现自己在尝试从标签访问输入时遇到了麻烦。这看起来是一个很容易解决的问题,但我玩得很开心。

<label htmlFor="termsAgreement">
    <input type="checkbox" name="termsAgreement"/>
    <p>Here are the terms of use</p>
</label>

我认为以下方法会起作用,但没有运气:

getByLabelText('Here are the terms of use', {selector: 'input'});

可以在这里找到一个带有几个测试的小示例 sn-p:

react-testing-library demo

【问题讨论】:

    标签: reactjs jestjs react-testing-library


    【解决方案1】:

    看起来我使用name 字段而不是预期的id 字段来定位相关输入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 2012-04-18
      • 2017-09-22
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多