Using ref for unit testing callbacks

Viewed 231

Suppose I have a ReactJS class something like this:

class MyComponent extends Component {
  onTouch = () => {
    if(this.calendar1) {
      this.calendar1.getValue();
      this.calendar1.setValue('something');
      this.setState({key: 'something'});
    }
  }

  render() {
    return (<div>
              <AnotherComponent onTouch={this.onTouch} ref={node => this.calendar1 = node}/>  
            </div>);
  }
}

How do I write a testcase to unit test the functionality of the callback? I read through this page https://reactjs.org/blog/2016/11/16/react-v15.4.0.html#mocking-refs-for-snapshot-testing But this doesn't work for me as element.type is only html elements.

0 Answers
Related