【问题标题】:Simulate click on material-ui toggle component test模拟点击material-ui切换组件测试
【发布时间】:2017-08-08 19:35:45
【问题描述】:

所以我正在尝试使用 jest 和酶来测试一些基于 material-ui 切换组件的功能。

我有一个通用的 clickIt 函数,它适用于其他 material-ui 组件,但在这个函数中,它似乎不会触发状态更改

function clickIt(wrapper, selector) {
  let elem = wrapper;

  if (selector) {
    elem = wrapper.find(selector);
  }

  const node = ReactDOM.findDOMNode(elem.node);
  TestUtils.Simulate.touchTap(node);
}

在测试中:

const toggle = wrapper.find('#subscribe-toggle');

expect(toggle.props().checked).to.be(true);

clickIt(toggle);

expect(toggle.props().checked).to.be(true); // <- fails

你知道如何解决这个问题吗?

【问题讨论】:

  • 切换组件没有onTouchTap 属性,只有onToggle 属性。可能是 touchTap 事件没有触发它?
  • 也尝试使用“点击”,但似乎不起作用
  • 据我所知,由于性能/延迟问题,所有 material-ui 组件都应该使用 onTouchTap

标签: javascript reactjs material-ui enzyme


【解决方案1】:

通过使用解决它:

// clickIt(toggle);
// toggle.last().simulate('click');
toggle.props().onChange(); // None of the above work, you can thank material ui for that one

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2020-06-18
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    相关资源
    最近更新 更多