When I try to create a Jest snapshot of my React component 'LoadingModal', which contains a Semantic-UI-React 'Modal' component, the snapshot ends up empty, with 'null' inside the .snap file. I've tested taking snapshots of other Semantic-UI-React components like 'Button', 'Dropdown', and 'Sidebar', which render and snapshot with the correct html output just fine.
This doesn't look like its an issue with Jest snapshotting itself, but rather react-test-renderer with its .create() and .toJson() methods returning null.
Why would react-test-renderer properly render some Semantic-UI-React components, but not others like 'Modal'? react-test-render seems to be the standard method of serializing React components for snapshotting, and is the method used in Jest's snapshotting docs.
I am using react@16.2.0, jest@22.1.4, react-test-renderer@16.2.0, and semantic-ui-react@0.77.1
Update:
It looks like for some reason the Semantic-UI-React Modal component in particular is set to render null when not rendered inside a browser.
See my issue on the github repo for more information.
LoadingModal.test.js
import React from 'react'
import renderer from 'react-test-renderer'
import LoadingModal from './LoadingModal'
test('should render correctly', () => {
const tree = renderer.create(
<LoadingModal/>
).toJSON()
expect(tree).toMatchSnapshot()
})
LoadingModal.test.js.snap
// Jest Snapshot v1
exports[`should render correctly 1`] = `null`;
LoadingModal.js
import React from 'react';
import PropTypes from 'prop-types'
import { Modal, Dimmer, Loader} from 'semantic-ui-react'
class LoadingModal extends React.Component {
static propTypes = {
header: PropTypes.func,
content: PropTypes.func,
loading: PropTypes.bool,
onClose: PropTypes.func,
size: PropTypes.string,
height: PropTypes.string
};
static defaultProps = {
size: 'tiny',
height: '500px'
}
_render_content = (content) => {
if (this.props.loading === true) {
return <Dimmer active inverted><Loader active size='large' inline='centered'/></Dimmer>
} else {
return content && content()
}
}
render () {
const {
header,
content,
loading,
onClose,
size,
height
} = this.props;
return (
<Modal size={size} dimmer='blurring' open={true} onClose={onClose}>
{header && header()}
<Modal.Content>
<div style={{height: height}}>
{this._render_content(content)}
</div>
</Modal.Content>
</Modal>
);
}
}
export default LoadingModal