【问题标题】:React Testing Library: Disabled attribute not being removed when state changesReact 测试库:状态更改时未删除禁用属性
【发布时间】:2021-01-16 20:08:18
【问题描述】:

所以我有一个表单字段组件来呈现 2 个选择字段。当用户从第一个选择中选择一个值时,第二个选择会启用(默认情况下它是禁用的)。

我在每个选择上都设置了 testID:第一个是 typeSelect

我正在做这样的测试:

  userEvent.selectOptions(screen.getByTestId('firstSelect'), ['Yes']);
  expect(screen.getByTestId('firstSelect_option_Yes').selected).toBe(true);
  expect(screen.getByTestId('secondSelect').toBeEnabled();

但是,secondSelect 继续显示它具有disabled = '' 属性。

这是一个错误吗?我读到了节点元素被绑定的信息,但我似乎无法测试禁用的被移除行为,因为它在浏览器中工作得很好。

【问题讨论】:

    标签: reactjs react-testing-library


    【解决方案1】:

    disabled='' 在何时何地向您显示?它可能是您页面初始状态的输出。

    你可以用expect(myField).toHaveProperty('disabled', false);测试天气是否被禁用

    这可能会回答一些疑问:https://github.com/testing-library/jest-dom/discussions/325

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-16
      • 2021-08-23
      • 1970-01-01
      • 2018-09-02
      • 2021-10-05
      • 2021-09-13
      • 1970-01-01
      • 2021-05-28
      相关资源
      最近更新 更多