bruderstein / unexpected-react

Plugin for http://unexpected.js.org to enable testing the full React virtual DOM, and also the shallow renderer
http://bruderstein.github.io/unexpected-react
MIT License
186 stars 19 forks source link

Support testing for a component to render nothing #16

Open albertfdp opened 8 years ago

albertfdp commented 8 years ago
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')
bruderstein commented 8 years ago

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

sunesimonsen commented 8 years ago

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

gertsonderby commented 8 years ago

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.