title: Jest mock async calls inside react component
body: <p>I'm new to jest/enzyme and am trying to mock a call to an aync function that returns a Promise, the call is made inside a react component in the  componentDidMount method.</p>
  
  <p>The test is attempting to test that componentDidMount sets the array returned by the Promise in the state.</p>
  
  <p>The issue I'm having is that the test finishes and passes before the array is added to the state. I am trying to use the 'done' callback to have the test wait until the promise resolves but this doesn't seem to work.</p>
  
  <p>I have tried to move the expect calls to the line before the done() call but that doesn't seem to work either.</p>
  
  <p>Can anyone tell me what I am doing wrong here? </p>
  
  <p>Component being tested:</p>
  
  <pre><code>componentDidMount() {
    this.props.adminApi.getItems().then((items) =&gt; {
      this.setState({ items});
    }).catch((error) =&gt; {
      this.handleError(error);
    });
  }
  </code></pre>
  
  <p>My test:</p>
  
  <pre><code>    import React from 'react';
      import { mount } from 'enzyme';
      import Create from '../../../src/views/Promotion/Create';
  
      import AdminApiClient from '../../../src/api/';
      jest.mock('../../../src/api/AdminApiClient');
  
      describe('view', () =&gt; {
  
        describe('componentDidMount', () =&gt; {
  
          test('should load items into state', (done) =&gt; {
            const expectedItems = [{ id: 1 }, { id: 2 }];
  
            AdminApiClient.getItems.mockImplementation(() =&gt; {
              return new Promise((resolve) =&gt; {
                resolve(expectedItems);
                done();
              });
            });
  
            const wrapper = mount(
              &lt;Create adminApi={AdminApiClient} /&gt;
            );
  
            expect(wrapper.state().items).toBe(expectedItems);
          });
  
        });
      });
  </code></pre>