unexpected-react icon indicating copy to clipboard operation
unexpected-react copied to clipboard

Support testing for a component to render nothing

Open albertfdp opened this issue 8 years ago • 3 comments

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')

albertfdp avatar Apr 06 '16 08:04 albertfdp

This should be pretty easy to add. This would be a great first PR :)

bruderstein avatar Apr 13 '16 17:04 bruderstein

@bruderstein how do you identify that a renderer rendered null?

sunesimonsen avatar May 12 '16 13:05 sunesimonsen

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.

gertsonderby avatar Sep 21 '16 11:09 gertsonderby