5

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?

Oscar Franco
  • 5,691
  • 5
  • 34
  • 56
  • The toggle component doesn't have a a `onTouchTap` attribute, only a `onToggle` attribute. It could be that the `touchTap` event is therefore not triggering it? – Aron Mar 17 '17 at 09:54
  • also tried using 'click' but doesn't seem to work – Oscar Franco Mar 17 '17 at 12:14
  • as far as I know, all the material-ui components should be using onTouchTap because of performance/latency issues – Oscar Franco Mar 17 '17 at 12:23

1 Answers1

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
Oscar Franco
  • 5,691
  • 5
  • 34
  • 56