title: Testing fetch action in react/redux app
body: <p>Im starting with unit testing and Jest. What I want is to test the action's response after fetching some resources from the db.</p>
  
  <p>This is the action code:</p>
  
  <pre><code>export function loadPortlets() {
     return function(dispatch) {
       return portletApi.getAllPortlets().then(response =&gt; {
         dispatch(loadPortletsSuccess(response));
         dispatch(hideLoading());
       }).catch(error =&gt; {
          dispatch({ type: null, error: error });
          dispatch(hideLoading());
          throw(error);
       });
     };
  }
  </code></pre>
  
  <p>This code is fetching data from:</p>
  
  <pre><code>  static getAllPortlets() {
  
      return fetch(`${API_HOST + API_URI}?${RES_TYPE}`)
        .then(response =&gt;
          response.json().then(json =&gt; {
            if (!response.ok) {
              return Promise.reject(json);
            }
  
            return json;
          })
        );
  }
  </code></pre>
  
  <p>And this is the test:</p>
  
  <pre><code>import configureMockStore from 'redux-mock-store';
  import thunk from 'redux-thunk';
  import fetch from 'isomorphic-fetch';
  import fetchMock from 'fetch-mock';
  import * as actions from '../portletActions';
  import * as types from '../actionTypes';
  
  const middlewares = [thunk];
  const mockStore = configureMockStore(middlewares);
  
  const mockResponse = (status, statusText, response) =&gt; {
    return new window.Response(response, {
      status: status,
      statusText: statusText,
      headers: {
        'Content-type': 'application/json'
      }
    });
  };
  
  describe('async actions', () =&gt; {
    afterEach(() =&gt; {
      fetchMock.reset();
      fetchMock.restore();
    })
  
    it('calls request and success actions if the fetch response was successful', () =&gt; {
      window.fetch = jest.fn().mockImplementation(() =&gt;
        Promise.resolve(mockResponse(200, null, [{ portlets: ['do something'] }])));
  
      const store = mockStore({ portlets: []});
  
      return store.dispatch(actions.loadPortlets())
        .then(() =&gt; {
          const expectedActions = store.getActions();
          expect(expectedActions[0]).toContain({ type: types.LOAD_PORTLETS_SUCCESS });
        })
    });
  
  });
  </code></pre>
  
  <p>And this is the result of running the test:</p>
  
  <pre><code>FAIL  src\actions\__tests__\portletActions.tests.js                                                                                                                      
    ● async actions › calls request and success actions if the fetch response was successful                                                                                
  
      expect(object).toContain(value)                                                                                                                                       
  
      Expected object:                                                                                                                                                      
        {"portlets": [// here an array of objects], "type": "LOAD_PORTLETS_SUCCESS"}                                                            
      To contain value:                                                                                                                                                     
        {"type": "LOAD_PORTLETS_SUCCESS"}                                                                                                                                   
  
        at store.dispatch.then (src/actions/__tests__/portletActions.tests.js:56:34)
        at &lt;anonymous&gt;
        at process._tickCallback (internal/process/next_tick.js:188:7)
  </code></pre>
  
  <p>In the redux docs for this example (<a href="https://redux.js.org/recipes/writing-tests" rel="nofollow noreferrer">https://redux.js.org/recipes/writing-tests</a>), they receive a result containing only the action types executed, but I'm getting the <strong>real data</strong> and the action inside the array.</p>
  
  <p>So I'm not sure if the code is wrong, or the test, or both!</p>
  
  <p>Thanks in advance, any help is highly appreciated!</p>