react-shallow-testutils icon indicating copy to clipboard operation
react-shallow-testutils copied to clipboard

Request for a `softFindWithRef`

Open rjokelai opened this issue 9 years ago • 2 comments
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

rjokelai avatar Feb 15 '16 13:02 rjokelai

Personally I would prefer to get back null than to throw, but you can use:

expect(() => findWithRef(component, "bar")).not.toThrow()

aaronbeall avatar Feb 27 '16 05:02 aaronbeall

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.

sheepsteak avatar Feb 27 '16 15:02 sheepsteak