unexpected-react
unexpected-react copied to clipboard
Support testing for a component to render nothing
it('renders nothing', () => {
const RenderNull = React.createClass({
render: () => null
})
expect(<RenderNull/>, 'to have rendered', null)
})
The error message you get:
expected <RenderNull /> to have rendered null
No matching assertion, did you mean:
<ReactElement> to have [exactly] rendered <ReactElement>
<ReactElement> to have rendered [with all children] [with all wrappers] <ReactElement>
<ReactShallowRenderer> to have [exactly] rendered <ReactElement>
<ReactShallowRenderer> to have rendered [with all children] [with all wrappers] <ReactElement>
<RenderedReactElement> to have [exactly] rendered <ReactElement>
<RenderedReactElement> to have rendered [with all children] [with all wrappers] <ReactElement>
It would be nice to support the following:
expect(<RenderNull />, 'to have rendered', null)
and
expect(<RenderNull />, 'to have rendered nothing')
This should be pretty easy to add. This would be a great first PR :)
@bruderstein how do you identify that a renderer rendered null
?
You can check that its getRenderOutput()
returns null. However, that function is unavailable when using the raw JSX as the subject, and when using a shallow renderer (as given by TestUtils.createRenderer()
) it is not recognized as a ReactElement
type.