reactjs - 测试具有不同案例场景的方法 -
问题描述
我如何测试一个没有任何 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
};
如果有人可以协助设置,我将不胜感激谢谢
解决方案
这是一个纯函数,因此测试它只是验证它是否根据给定的输入返回正确的输出:
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!
});
});
推荐阅读
- mongodb - 如何在 mongodb 中将 NumberDecimal 值相乘
- java - 如何使用 mapreduce 作业聚合和显示前 n 个项目
- css - 在侧边栏中使用自定义样式
- react-native - 无法从 Axios 中获取数据
- javascript - 未捕获的 SyntaxError:计算时出现意外标识符
- jpype - python.exe中发生未处理的win32异常
- java - 通过 NetBeans 8.2 运行索引文件时,如何解决 PHP 中的“调用未定义函数 mysql_pconnect()”错误?
- sql - 如何将表单向导 POST 请求保存到数据库
- javascript - 如何向 JSON 对象添加新的键/值对?
- wordpress - Facebook 的应用内浏览器返回错误“net::ERR_FAILED”,但没有其他浏览器