首页 > 解决方案 > 测试具有不同案例场景的方法 -

问题描述

我如何测试一个没有任何 onBlur、onChange、onClick 事件传递的方法。

该文件仅包含 case 、 break 和 switch 。所以我不确定如何开始这个文件。我已经测试了基础道具并且它可以正确渲染

使用 JEST - 酶反应 JS

另一种调用 BuildShapeObj 的方法

updateFilterList = (filterList) => {
  let localFilters = Object.assign({}, this.state.filters)
  // NOTE: need to change localFilters[props.currentColumnID] with filterList.column
  localFilters[this.props.currentColumnID] = filterList
  if (Object.keys(localFilters[this.props.currentColumnID]).length === 0) {
  delete localFilters[this.props.currentColumnID]
 }
 let updateObj = this.buildShapeObj({switchValue: 'filtering', shapeObjValue: localFilters})
  let updateBoolean = this.shouldUpdateViewXML(localFilters)
  if (updateBoolean) {
  this.props.updateViewXMLValue(updateObj)
}
  this.setState({
  filters: localFilters
})
}

这里只是方法的一部分

buildShapeObj = updateObj => {
  let pipe = "shape";
  let shapeObj = {};

  switch (updateObj.type) {
    case "sort":
      shapeObj = {
        0: {
          pipe,
          action: "transform",
          columnName: updateObj.column,
          sort: updateObj.value
        }
      };
      break;
    case "group":
      shapeObj = {
        0: {
          pipe,
          columnName: updateObj.column,
          transformType: "replaceElement",
          matchValue: "resetanalysis"
        },
        1: {
          pipe,
          columnName: updateObj.column,
          transformType: "replaceElement",
          matchValue: "resetallgrouping"
        }
        // ...
      };
      break;
  }
  return shapeObj; //end of method line 350
};

如果有人可以协助设置,我将不胜感激谢谢

标签: reactjsjestjsenzyme

解决方案


这是一个纯函数,因此测试它只是验证它是否根据给定的输入返回正确的输出:

describe('buildShapeObj', () => {

  it('should handle sort', () => {
    expect(buildShapeObj({
      type: 'sort',
      column: 'the column',
      value: 'the value'
    })).toEqual({
      0: {
        pipe: 'shape',
        action: 'transform',
        columnName: 'the column',
        sort: 'the value'
      }
    });  // Success!
  });

  it('should handle group', () => {
    expect(buildShapeObj({
      type: 'group',
      column: 'the column'
    })).toEqual({
      0: {
        pipe: 'shape',
        columnName: 'the column',
        transformType: "replaceElement",
        matchValue: "resetanalysis"
      },
      1: {
        pipe: 'shape',
        columnName: 'the column',
        transformType: "replaceElement",
        matchValue: "resetallgrouping"
      }
    });  // Success!
  });

});

推荐阅读