vendredi 21 septembre 2018

How can I test the async actions which using 'superagent' in my react project with 'Enzyme' and 'Jest'?

I got a problem in my personal project. I use 'superagent' to get the information from Api and now I want to write test for it. But I cannot use 'fetch-mock' package which used in the Enzyme example.

Here is my action file.

   // getRecommendedProductsActions.js
    export const getRecommendedProducts = () => (dispatch) => {
      dispatch(fetchProducts());
      return request
        .get(URL_PRODUCT_BASE)
        .set('Content-Type', 'application/json')
        .then(res => dispatch(receiveProducts(res.body)))
        .catch(err => dispatch(receiveFailure(err)));
    };

Here is my test file.

// test/getRecommendedProducts.test.js
import configureMockStore from 'redux-mock-store';
import fetchMock from 'fetch-mock';
import thunk from 'redux-thunk';
import { getRecommendedProducts } from '../../src/actions/products';
describe('async actions', () => {
  afterEach(() => {
    fetchMock.reset();
    fetchMock.restore();
  });

  it('creates RECEIVE_PRODUCTS when fetching products has been done', () => {
    fetchMock
      .get('/products', {
        body: httpBody,
        headers: { 'content-type': 'application/json' },
      });

    const expectedActions = successResponse;

    const store = mockStore();

    return store.dispatch(getRecommendedProducts())
      .then(() => expect(store.getActions()).toEqual(expectedActions));
  });

Aucun commentaire:

Enregistrer un commentaire