I am trying to run an unit test for React JS - using jest / enzyme.
The test is failing at the moment.Not really sure why, maybe i am not calling the expect(wrapper.find) correctly. Here is part of my test:
File.test.js
it('renders modal when open flag is true', () => {
const props = { isOpen: true };
const wrapper = mount(
<div id="root">
<Component {...props} />
</div>
);
wrapper.update();
expect(toJson(wrapper)).toMatchSnapshot();
expect(wrapper.find('.loa-download-header').exists()).toEqual(true);
expect(wrapper.text()).toContain(' Please enter a password.');
});
});
Here is part of File.js Shows a piece of the code that I am trying to test as an example.
render() {
return (
<Modal
<div title="Close Window" className="add-custom-field-close" onClick={() => {this.closeModal()}}><FontAwesome name='xbutton' className='fa-times' /></div>
</div>
</div>
<div className='loa-download-header-wrapper'>
<div className='loa-download-header'>
Please enter a password.
</div>
Error: expect(received).toEqual(expected)
Expected value to equal:
true
Received:
false
Any corrections on the code would be extremely helpful Thanks
I've spent some time integrating your code into a sandbox. There are quite a few changes to your code, which I've listed below. I've also included some tests that have been filled in and some that are not. What you should do is spend some time familiarizing yourself with these changes so that you can fill in the rest of tests in
containers/LOAs/__tests__
on your own.Even though I wrote an integrated test for the
LOAs
container, I encourage you to write a unit test for the smallercomponents
, so that you can practice mockingprop
functions, checking if they're being called, and making sure the component functions as expected. Even though it'll be redundant, it'll help you understand the flow, what to test, and how to test (for unit tests, you'll want to use theshallowWrap
instead of themountWrap
function -- or don't use them and use the providedshallow
andmount
functions offered byenzyme
... up to you).Working example: https://codesandbox.io/s/p3ro0615nm
Changes:
UI
andstate
changesthis.setState()
callbacks to keepstate
and secondary actions synchronous. Just as important, this also reduces unwanted component flickering.filter
,map
, andisEmpty
functions (they're convenient and I prefer them over native JS functions)setTimeout
, as this will affect your tests). In thefakeAPI.post
function, I added a fake password to check against, therefore, the supplied password must be 12345!methods
start withhandle
, while passed down methods start withon
.components/LOAModal/LOAModal.js
into smaller/reuseable components to make it easier to readPropType
checking to make sure props were consistent during initial declaration and across components.Notes:
DOM
asenzyme
sees it, then you canconsole.log(wrapper.debug());
inside of yourit
test.jest.useFakeTimers()
in thebeforeEach()
function andjest.runAllTimers()
in theafterEach()
function to simulatesetTimeout
functions without having to wait for real time to pass..catch()
after aPromise
(an API call). If you don'tcatch
your Promises, then it can crash your app.