-2
import service from "Api/service";
import { ROOT_URL, saveError } from "Api";
import {
  FETCH_USER_REQUEST,
  FETCH_USER_FAILURE,
  FETCH_USER_SUCCESS
} from "Constants/redux";
// it fetch the details of an user
export const fetchDetails = () => {
  return (dispatch, getState) => {
    dispatch(fetchDetailsRequest());
    return service
      .get(`${ROOT_URL}/employees/list`)
      .then(response => {
         dispatch(fetchDetailsSuccess(response));
      })
      .catch(error => {
        console.log(error);
        saveError(error, getState());
        dispatch(fetchDetailsFailure(error));
      });
  };
};

export const ffetchDetailsRequest = () => {
  return {
    type:  FETCH_USER_REQUEST
  }
};

export const fetchDetailsSuccess = (res) => {
    return {
      res,
      type: FETCH_USER_SUCCESS
    }
};

export const ffetchDetailsFailure = (err) => {
  return {
    err,
    type:  FETCH_USER_FAILURE
  }
};

//I am new to Jest. I have below file. I need to write unit tests for it. Can anyone //help how I should start or give me an abstract form of the possible unit tests I can write.

skyboyer
  • 22,209
  • 7
  • 57
  • 64

1 Answers1

0

This is what I would do:

import {
  fetchDetailsRequest, fetchDetailsSuccess, fetchDetailsFailure, fetchDetails,
} from './myModule';
import {
  FETCH_USER_REQUEST, FETCH_USER_SUCCESS, FETCH_USER_FAILURE,
} from 'Constants/redux';
import service from 'Api/service';
import { saveError } from 'Api';

jest.mock('Api/service');
jest.mock('Api', () => ({
  ROOT_URL: 'url',
  saveError: jest.fn(),
}));

describe('my_module', () => {
  it('fetchDetailsRequest', () => {
    expect(fetchDetailsRequest()).toEqual({ type: FETCH_USER_REQUEST });
  });

  it('fetchDetailsSuccess', () => {
    const res = 'MyRes';
    expect(fetchDetailsSuccess(res)).toEqual({ res, type: FETCH_USER_SUCCESS });
  });

  it('fetchDetailsFailure', () => {
    const err = 'MyErr';
    expect(fetchDetailsFailure(err)).toEqual({ err, type: FETCH_USER_FAILURE });
  });

  describe('fetchDetails', () => {
    it('success', async () => {
      const response = 'response data';
      service.get.mockResolvedValue(response);
      const dispatch = jest.fn();
      await fetchDetails()(dispatch);
      expect(dispatch).toHaveBeenCalledTimes(2);
      expect(dispatch).toHaveBeenNthCalledWith(1, fetchDetailsRequest());
      expect(dispatch).toHaveBeenNthCalledWith(2, fetchDetailsSuccess(response));
    });

    it('failure', async () => {
      const error = 'response data';
      service.get.mockRejectedValue(error);
      const dispatch = jest.fn();
      const getState = jest.fn();
      const getStateRes = 'stateRes';
      getState.mockImplementation(() => getStateRes);
      await fetchDetails()(dispatch, getState);
      expect(dispatch).toHaveBeenCalledTimes(2);
      expect(dispatch).toHaveBeenNthCalledWith(1, fetchDetailsRequest());
      expect(dispatch).toHaveBeenNthCalledWith(2, fetchDetailsFailure(error));
      expect(saveError).toHaveBeenCalledWith(error, getStateRes);
    });
  });
});
Max
  • 1,020
  • 7
  • 13
  • 1
    Question askers are expected to show some effort in regards to what they have attempted, this asker has attempted nothing. Please don't waste your time on people who are only looking to have others do their job or homework for them. – Ian Kemp Jan 14 '20 at 20:29