Skip to content

Instantly share code, notes, and snippets.

@seamusleahy
Created June 27, 2017 03:27
Show Gist options
  • Save seamusleahy/412018682fc3ab00de0febf50cecc21f to your computer and use it in GitHub Desktop.
Save seamusleahy/412018682fc3ab00de0febf50cecc21f to your computer and use it in GitHub Desktop.
How to test the content inside of a react-bootstrap <Modal>
import { mount, ReactWrapper } from 'enzyme';
import { Modal } from 'react-bootstrap';
// The Enzyme selector query will not work because the actual inside HTML of the modal lives else where in the DOM
// via portals. See: https://react-bootstrap.github.io/react-overlays/#portals
function getWrapperForModalPortal(parentWrapper) {
// The Modal instance provides a reference to HTML content:
// [1] https://github.com/react-bootstrap/react-bootstrap/blob/dddb9b966c7f3e79495be5c5730f7cce04813aaf/src/Modal.js#L228
// [2] https://github.com/react-bootstrap/react-bootstrap/blob/419051127d913bbb149e81ed221500108ba0d7f3/test/ModalSpec.js#L32
const modalInstance = parentWrapper.find(Modal).getNode()._modal.getDialogElement();
return new ReactWrapper(modalInstance, modalInstance);
}
describe('Modal', () => {
it('', () => {
const modal = wrapper(<div>
<Modal show>
<div className="TestClassName">example</div>
</Modal>
</div>
);
const modalWrapper = getWrapperForModalPortal(wrapper);
const testDiv = modalWrapper.find('div').get(0);
expect(testDiv.className).toEqual('TestClassName');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment