react-shallow-testutils
react-shallow-testutils copied to clipboard
Request for a `softFindWithRef`
trafficstars
A softer version of findWithRef would be beneficial when, e.g., hiding components:
class Foo extends Component {
render() {
return (<div>
{this.props.show ? <span ref="bar">Text</span> : undefined}
</div>)
}
}
With <Foo show={false} /> the span will be hidden so softFindWithRef(component, 'bar') would return e.g., undefined instead of throwing error
Personally I would prefer to get back null than to throw, but you can use:
expect(() => findWithRef(component, "bar")).not.toThrow()
The reason the functions that find a single result throw is to mirror how the react-addons-test-utils work. I use what @aaronbeall has put in his comment. In my opinion, either something should be there or it shouldn't and you want the test to fail as fast as possible if you're wrong.