在不同情况下测试方法-

时间:2019-03-14 15:24:07

标签: reactjs jestjs enzyme

我该如何测试没有传递任何onBlur,onChange,onClick事件的方法。

此文件仅包含case,break和switch。因此,我不太确定如何启动该文件。 我已经测试了基本道具,并且渲染正确

使用JEST-ENZYME REACT 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
};

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

1 个答案:

答案 0 :(得分:1)

这是pure function,因此测试它只是根据给定的输入验证它是否返回正确的输出即可。

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

});