Say I have the following application:
class Child extends React.Component {
render() {
return <button onClick={this.handleChildOnClick}>{this.props.children}</button>;
}
handleChildOnClick() {
this.props.onChildClick('foo');
}
}
class Parent extends React.Component {
render() {
return <Child onChildClick={this.handleParentOnClick}>click me</Child>;
}
handleParentOnClick(text) {
this.props.onParentClick(12345);
}
}
class App extends React.Component {
render() {
return <Parent onParentClick={(num) => console.log(num)} />;
}
}
I'm having a hard time figuring out the proper way to test the Parent component. The Child and App one are not a problem, but the Parent...
I mean, how do I test that a click on the Child component is going to invoke the Parent click callback without:
- ...rendering
Child.Parentshould be tested in isolation as a shallow render.Childwill also be tested in isolation and if I do a mount render, I'm basically testing the click callback on theChildtwice. - ...directly invoking
handleParentOnClickon theParentinstance. I shouldn't depend on the exact implementation ofParentfor this. If I change the name of the callback function, the test will break and it could very well be a false positive.
Is there a third option?