-
Notifications
You must be signed in to change notification settings - Fork 812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test Modals with enzyme 3 #563
Comments
Found solution - do not use So everything becomes more simple and easy to test, just not forget to do |
This seems to be the most common solution, but it appears the node prop has been deprecated. Using React 16.2 and enzyme 3.1, I am seeing this error:
Is anyone else seeing this? BTW, getElement() does not seem to return portal as a prop. Example: const wrapper = mount(
<div>
<Provider store={store}>
<MyModal isOpen={true} />
</Provider>
</div>
);
console.log('modal node', wrapper.find(ReactModal).node) // error
console.log('modal element', wrapper.find(ReactModal).getElement(0)) // ReactModal element with props, etc, but no portal
console.log('from query', document.querySelector(`.ReactModalPortal`)) // Finds it, but I would rather not do it this way |
Hi there, On top of what @ramusus suggested, here's the code that worked for me, it might be helpful: Assuming I have the following <Modal isOpen={isOpen} {...props}>
<div className="my-modal-window">
Hello World
</div>
</Modal> My tests were like this: function getPortalWrapper(props) {
const wrapper = mount(
<div id="root">
<Component {...props} />
</div>
);
const { portal } = wrapper.find(ReactModal).first();
return new ReactWrapper(portal, true);
}
// ...
it("renders modal when open flag is true", () => {
const wrapper = getPortalWrapper({ isOpen: true });
expect(wrapper.find(".my-modal-window").exists()).toEqual(true);
}); Now my tests look like this: it('renders modal when open flag is true', () => {
const props = { isOpen: true };
const wrapper = mount(
<div id="root">
<Component {...props} />
</div>
);
wrapper.update();
expect(wrapper.find('.my-modal-window').exists()).toEqual(true);
expect(wrapper.text()).toContain('Hello World');
}); Thanks a lot, @ramusus and @cgalbiati |
Actually, I have multiple tests with react-modal and In order to get access to the right react-modal I ended up with the following code: import ReactModal from 'react-modal';
it('renders modal when open flag is true', () => {
const wrapper = mount(
<div id="root">
<Component {...props} />
</div>
);
const modal = wrapper.find(ReactModal).instance().portal.content; //
// Then depending on the environment you can query items within modal:
// 1. if tests are running in browser environment
expect(wrapper.querySelector('.title').innerHTML).toEqual('Page title');
// 2. otherwise, any library that can query the string like cheerio can be used (I did not test this)
expect(cheerio.load(modal).find('.title').text()).toEqual('Page title');
} |
Continue discussion started here #71 (comment)
After upgrading enzyme from version 2 to latest 3.2.0 my related to retrieving subtree of Modal component tests gets broken.
I'm using solution from here http://remarkablemark.org/blog/2017/05/17/testing-react-modal/
After upgrade
portalWrapper
is empty even withis_open={true}
(before it has full subtree), so It became impossible to retrieve content. Any solution for this problem?I tested with react 15 and 16
The text was updated successfully, but these errors were encountered: