我正在为组件编写 Jest,以确保单击按钮时将执行handleLogin。不知道为什么我不断收到 jest.fn() 未执行一次的错误。我在这里删除了大部分不必要的代码
class Login extends React.Component{
constructor(props){
super(props);
this.state = { }
this.handleLogin = this.handleLogin.bind(this);
}
handleLogin(e){
e.preventDefault();
const { email, password, invalidEmail, invalidPassword } = this.state;
if(!invalidEmail && !invalidPassword){
userAPI.login({email, password}).then(data=>{
window.localStorage.setItem('email',data.email);
window.localStorage.setItem('password',data.password);
}).catch(error=>{
this.setState({
incorrectCredential: true // login failed due to wrong credential
})
})
}else{
this.setState({
invalidCredential: true // login failed due to wrong format
})
} // login failed
}
render(){
return(
<header>
<div className="login" data-test="login">
<button className="login_content_main_join" type="submit" onClick={this.handleLogin} data-test="submit">Log in</button>
</div>
</header> …Run Code Online (Sandbox Code Playgroud)