Javascript 如何用Jest和酶测试反应中的问题
我有以下代码用于Javascript 如何用Jest和酶测试反应中的问题,javascript,reactjs,jestjs,enzyme,Javascript,Reactjs,Jestjs,Enzyme,我有以下代码用于onSubmit函数 handleSubmit(事件){ event.preventDefault(); this.setState({isDone:true}); 常量名称匹配=()=>{ return!(this.state.firstName.match(^Cel.*)和&this.state.firstName.endsWith(“on”)) } if(nameMatch()){ 警报(“请输入有效名称”); 返回; } 如果(!this.state.lastName.e
onSubmit
函数
handleSubmit(事件){
event.preventDefault();
this.setState({isDone:true});
常量名称匹配=()=>{
return!(this.state.firstName.match(^Cel.*)和&this.state.firstName.endsWith(“on”))
}
if(nameMatch()){
警报(“请输入有效名称”);
返回;
}
如果(!this.state.lastName.endsWith(“on”)){
警告(“也检查姓氏”);
返回;
}
此参数为.getFullName();
}
要运行上述测试,我需要执行以下操作:
it('check submit',()=>{
handleSubmit({preventDefault:()=>{});
log(wrapper.handleSubmit());
});
在这里,我需要名字和姓氏的状态。在测试用例中,我如何做到这一点?如何将它们作为参数传递给测试 这是解决方案,您可以使用
酶的方法来触发react dom。文件:
例如:
index.tsx
:
import React,{Component}来自'React';
导出接口组件状态{
名字:字符串;
lastName:string;
isDone:布尔型;
[键:字符串]:任意;
}
export类SomeComponent扩展了Component谢谢,我确实使用了这个概念,但没有使用typescript。帮助很大。