When calling isEmptyRender on a empty child of a shallow render, isEmptyRender will return false, indicating the render of the child is returning something (i.e. not returning null or false).
Following example illustrates the issue:
it('should fail', () => {
const parent = shallow(<Parent />)
const child = parent.find('EmptyChild') // ~ MessageBox
expect(child.isEmptyRender()).toBe(true) // returns false whilst child render returns null
])
Add a warning indicating mount() should be used when trying to test child render behaviour.
What does Parent look like here?
In my specific case the <Parent /> component is a login form with the following structure:
<div>
<Form>
<Segment>...</Segment>
<MessageBox content={error} />
</Form>
</div>
The EmptyChild from my first comment (updated) is the MessageBox component. The render of this component returns null.
When shallow-rendering, everything gets a render method that's return this.props.children - in other words, when shallow-rendering Parent, MessageBox is not an empty render.
If you do shallow(<Parent />).find(MessageBox).shallow().isEmptyRender(), then you'd get true.
In other words, when shallow-rendering, the only things you should be making assertions about is "what the top-level renders", and "what props they have" - the responsibility for testing what MessageBox renders is with MessageBox's tests, not Parent's.
Right, got it! Thanks for clarifying 馃憤
Most helpful comment
When shallow-rendering, everything gets a
rendermethod that'sreturn this.props.children- in other words, when shallow-renderingParent,MessageBoxis not an empty render.If you do
shallow(<Parent />).find(MessageBox).shallow().isEmptyRender(), then you'd gettrue.In other words, when shallow-rendering, the only things you should be making assertions about is "what the top-level renders", and "what props they have" - the responsibility for testing what MessageBox renders is with MessageBox's tests, not Parent's.