Antanio D
Antanio D

Reputation: 59

React test case for a function

I have a below construct-api-data.js file

import getSubmitData from './get-submit-data';
import getInitData from './get-init-data';

export default data => {
  switch (data.actionType) {
    case 'landing':
    case 'save':
       return getSubmitData(data);
    case 'getInitData':
       return getInitData(data);
    default:
       return {};
  }
};

For this above js file, i had written below test cases in react-testing-library

import constructApiData from '../construct-api-data';

describe('constructApiData', () => {
  it('should return object when case is landing', () => {
    expect(
      typeof constructApiData({
        data: { test: 'test', actionType: 'landing' },
      }),
    ).toBe('object');
  });

  it('should return object when case is save', () => {
    expect(
      typeof constructApiData({
        data: { test: 'test', actionType: 'save' },
      }),
    ).toBe('object');
  });

  it('should return object when case is getInitData', () => {
    expect(
      typeof constructApiData({
        data: { test: 'test', actionType: 'getInitData' },
      }),
    ).toBe('object');
  });

  it('should return object when case is default', () => {
    expect(typeof constructApiData({})).toBe('object');
  });
}

Even after writing these test cases, the coverage report says that my test coverage is not complete. Coverage is missing for the below lines in the js file

return getSubmitData(data);
return getInitData(data);

What am I missing here?

Upvotes: 2

Views: 781

Answers (1)

Sarun UK
Sarun UK

Reputation: 6736

The issue is because of the data that you are passing to the function is incorrect. Try the below approach,

     it('should return object when case is save', () => {
        expect(
          typeof constructApiData({ test: 'test', actionType: 'save' })
        ).toBe('object');
      });

      it('should return object when case is getInitData', () => {
        expect(
          typeof constructApiData({ test: 'test', actionType: 'getInitData' })
        ).toBe('object');
      });

Upvotes: 1

Related Questions