反应一个功能的测试用例

时间:2020-10-19 06:03:41

标签: reactjs unit-testing jestjs enzyme react-testing-library

我有一个construct-api-data.js以下文件

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 {};
  }
};

对于上面的js文件,我在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');
  });
}

即使在编写了这些测试用例之后,覆盖率报告仍说我的测试覆盖率还不完整。 js文件中以下各行的内容缺失

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

我在这里想念什么?

1 个答案:

答案 0 :(得分:1)

问题是由于传递给函数的数据不正确。 尝试以下方法,

     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');
      });