最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Simulate click on material-ui toggle component test - Stack Overflow

programmeradmin4浏览0评论

So I'm trying to test some functionality that is based on the material-ui toggle ponent, using jest and enzyme.

I have a generic clickIt function that works well for other material-ui ponents, but in this one it doesn't seem to be triggering the state change

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

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

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

And on the test:

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

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

clickIt(toggle);

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

Any idea on how to solve this?

So I'm trying to test some functionality that is based on the material-ui toggle ponent, using jest and enzyme.

I have a generic clickIt function that works well for other material-ui ponents, but in this one it doesn't seem to be triggering the state change

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

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

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

And on the test:

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

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

clickIt(toggle);

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

Any idea on how to solve this?

Share Improve this question asked Mar 17, 2017 at 9:51 Oscar FrancoOscar Franco 6,2606 gold badges37 silver badges70 bronze badges 3
  • The toggle ponent doesn't have a a onTouchTap attribute, only a onToggle attribute. It could be that the touchTap event is therefore not triggering it? – Aron Commented Mar 17, 2017 at 9:54
  • also tried using 'click' but doesn't seem to work – Oscar Franco Commented Mar 17, 2017 at 12:14
  • as far as I know, all the material-ui ponents should be using onTouchTap because of performance/latency issues – Oscar Franco Commented Mar 17, 2017 at 12:23
Add a ment  | 

1 Answer 1

Reset to default 6

got around it by using:

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

评论列表(0)

  1. 暂无评论