Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

75
Views
jest unit tests for redux action

I'm trying to achieve 100% code coverage by writing all possible unit tests for my action. The following is my action code

export const getData = async (
  requestUUID: string,
  dispatch?: (type?: { type: string, payload?: unknown }) => {
    type: string,
    payload?: unknown,
  }
): Promise<Cart> => {
  const url = `${getApiUrl()}${API_V1_CART}`;
  dispatch({ type: GET_REQUEST });

  try {
    const result = await apiCall(
      {
        method: "GET",
        withCredentials: true,
        url,
        timeout: 1000,
      },
      dispatch,
      requestUUID
    );
    dispatch({ type: GET_SUCCESS, payload: result });
    return result;
  } catch (e) {
    dispatch({ type: GET_FAILED, payload: e });
    return e;
  }
};

I have written a unit test for the above as action as follows

describe('getData', () => {
  describe('success', () => {
    let result;
    const mockDispatch = jest.fn();
    const mockData = {};
    beforeAll(async () => {
      (apiCall as jest.Mock).mockResolvedValueOnce(mockData);
      result = await getBasketData('uuid', mockDispatch);
    });

    it('should call the correct api', () => {
      expect(apiCall).toHaveBeenCalledWith(
        {
          method: 'GET',
          timeout: 1000,
          url: 'http://localhost/api/v1/data',
          withCredentials: true,
        },
        mockDispatch,
        'uuid',
      );
    });

    it('should return the correct result', () => {
      expect(result).toEqual(mockData);
    });
  });
});

This test only covers 77.78% of code coverage. What are the other tests i can write for this to achieve 100% code coverage?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can write test for the catch block.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!