Setup

Learn to set up our tests to test the async middleware.

To make our tests cleaner, we will use the structure discussed previously and mock the axios API as discussed in the Async Action Creators lesson.

We will also use the sample API action creators from earlier to drive the tests and a fake data response from the server:

import apiMiddleware from 'middleware/api';
import { mockRequest, mockRequestError } from 'axios';
import { API_STARTED, API_FINISHED, API, API_ERROR } from 'constants';

const data = { title: 'hello' };


const setData = data => ({
  type: 'SET_DATA',
  payload: data
});

//api object
const apiAction = () => ({
  type: API,
  payload: {
    success: setData,
    url: 'fake.json'
  }
});

// testing apiMiddleware
describe("api middleware", () => {
  let next, dispatch, middleware;

  beforeEach(() => {
    next       = jest.fn();
    dispatch   = jest.fn();
    middleware = apiMiddleware({ dispatch })(next);
  });
  // more tests
  describe('general', () => { /* TODO */ });
  describe('success', () => { /* TODO */ });
  describe('error', () => { /* TODO */ });
});

Get hands-on with 1200+ tech skills courses.