模拟点击material-ui切换组件测试

Simulate click on material-ui toggle component test

所以我正在尝试使用 jest 和 enzyme 测试一些基于 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

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

通过以下方式绕过它:

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