Failed Server Access

Learn what to do when server access is not granted.

A failing case is similar to a successful one, except that we mock the server request to fail. There are two tests in this scenario, verifying that API_ERROR was dispatched instead of API_FINISHED:

// Failure tests framework
describe('error', () => {
  beforeEach(() => mockRequestError('recipes.json', 404, 'Not found'));

  //testing that API_FINISHED is not dispatched
  it('should NOT dispatch API_FINISHED', () =>
    middleware(apiAction()).then(() =>
       expect(dispatchCalls[1][0].type).not.toBe(API_FINISHED)));
  
  //testing that ERROR is dispatched
  it('should dispatch error', () =>
    middleware(apiAction()).then(() =>
       expect(dispatchCalls[1]).toMatchSnapshot()));
});

Here we have used all the methods discussed previously to test both cases.

Get hands-on with 1200+ tech skills courses.