Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

reactjs - expect(received).toEqual(expected) - error

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 smaller components, so that you can practice mocking prop 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 the shallowWrap instead of the mountWrap function -- or don't use them and use the provided shallow and mount functions offered by enzyme... up to you).

Working example: https://codesandbox.io/s/p3ro0615nm

Changes:

  • Created a container component that handles all UI and state changes
  • Used this.setState() callbacks to keep state and secondary actions synchronous. Just as important, this also reduces unwanted component flickering.
  • Used conditional rendering with the use a ternary operator
  • Used lodash's filter, map, and isEmpty functions (they're convenient and I prefer them over native JS functions)
  • Mocked 2 API calls (noticed that I've used setTimeout, as this will affect your tests). In the fakeAPI.post function, I added a fake password to check against, therefore, the supplied password must be 12345!
  • Added the ability to select and unselect LOAs. If desired, you can simplify all of that with checkboxes.
  • Class methods start with handle, while passed down methods start with on.
  • Broke down components/LOAModal/LOAModal.js into smaller/reuseable components to make it easier to read
  • Added PropType checking to make sure props were consistent during initial declaration and across components.

Notes:

  • A major part of testing is understanding what's happening behind the scenes, so take the time to read the documentation; and, if necessary, do smaller/simpler projects to help familiarize yourself with how React works.
  • When testing, if you need to see the DOM as enzyme sees it, then you can console.log(wrapper.debug()); inside of your it test.
  • You can utilize jest.useFakeTimers() in the beforeEach() function and jest.runAllTimers() in the afterEach() function to simulate setTimeout functions without having to wait for real time to pass.
  • Very important: Always include a .catch() after a Promise (an API call). If you don't catch your Promises, then it can crash your app.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...