LoadingModal.test.js
import React from 'react'
import renderer from 'react-test-renderer'
import { Modal } from 'semantic-ui-react'
test('should render correctly', () => {
const tree = renderer.create(
<Modal></Modal>
).toJSON()
expect(tree).toMatchSnapshot()
})
LoadingModal.test.js.snap
// Jest Snapshot v1
exports[`should render correctly 1`] = `null`;
Modal renders normally when snapshotted with react-test-renderer and Jest
Modal renders null when snapshotted with react-test-renderer and Jest
I am using [email protected], [email protected], [email protected], and [email protected]
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.
After looking through the changelog, it looks like this line below in Modal.js is the culprit, causing the Modal to render null when it is not rendered inside a browser. What is the purpose of short circuiting like this unless rendered inside a browser? No other components seem to do this.
// Short circuit when server side rendering
if (!isBrowser()) {
return isValidElement(trigger) ? trigger : null
}
See my stack overflow post on this issue for more context
This line solved problems with SSR rendering, see #2259 for more context.
However, you can unblock this condition, see how it done in our tests.
The #2259 isBrowser change did not fix the issue. Even if you run the snapshot test with jsdom, <Modal /> still renders null. This is because of the <Portal /> component and the rendering happening on document.body.
I ended up solving this by mocking the Portal component.
jest.mock('semantic-ui-react/dist/commonjs/addons/Portal/Portal', () => ({ children }) => children);
I posted more of an explanation on stackoverflow. I'm so excited that I thought about this, because now I can use 'react-test-renderer' and enzyme to actually test modals now.
Most helpful comment
The #2259
isBrowserchange did not fix the issue. Even if you run the snapshot test with jsdom,<Modal />still rendersnull. This is because of the<Portal />component and the rendering happening ondocument.body.I ended up solving this by mocking the
Portalcomponent.I posted more of an explanation on stackoverflow. I'm so excited that I thought about this, because now I can use 'react-test-renderer' and
enzymeto actually test modals now.