Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate click on material-ui toggle component test

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

I have a generic clickIt function that works well for other material-ui components, 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?

like image 342
Oscar Franco Avatar asked Mar 17 '17 09:03

Oscar Franco


1 Answers

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
like image 157
Oscar Franco Avatar answered Nov 11 '22 22:11

Oscar Franco