Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jest单元测试。模拟函数返回,监视函数调用_Javascript_Reactjs_Unit Testing_Jestjs - Fatal编程技术网

Javascript Jest单元测试。模拟函数返回,监视函数调用

Javascript Jest单元测试。模拟函数返回,监视函数调用,javascript,reactjs,unit-testing,jestjs,Javascript,Reactjs,Unit Testing,Jestjs,我正在使用Jest开发React应用程序 我必须测试组件的功能: handleSubmit = event => { event && event.preventDefault(); this.formValidation() ? this.login() : this.handleErrors(this.state); }; 您能告诉我如何模拟this.formValidation方法返回以监视this.login或this.handleErrors for

我正在使用Jest开发React应用程序

我必须测试组件的功能:

handleSubmit = event => {
  event && event.preventDefault();
  this.formValidation() ? this.login() : this.handleErrors(this.state);
};
您能告诉我如何模拟
this.formValidation
方法返回以监视
this.login
this.handleErrors

  formValidation = () => {
    return (
      validateEmail(this.state.email) && isPasswordValid(this.state.password)
    );
  };

  login = () => {
    authenticationService.login(this.state.email, this.state.password).then(
      () => this.props.history.push({ pathname: "/template" }),
      message => this.setState({ errors: { api: message } })
    );
  };

  handleErrors = ({ email, password }) => {
    this.setState({
      errors: {
        email: !validateEmail(email) ? "Email is required" : null,
        password: !isPasswordValid(password)
          ? "Password is required. (7+ chars: mixed, number, special)"
          : null
      }
    });
  };

感谢您的帮助

以下是单元测试解决方案:

index.jsx

import React,{Component}来自'React';
类SomeComponent扩展组件{
构造函数(){
this.state={};
}
handleSubmit=(事件)=>{
event&&event.preventDefault();
this.formValidation()?this.login():this.handleErrors(this.state);
};
formValidation(){}
handleErrors(州){}
登录(){}
render(){
返回(
);
}
}
导出默认组件;
index.test.jsx

从“React”导入React;
从“酶”导入{shall};
从“”导入组件;
描述('59741487',()=>{
让包装纸;
const mFormValidation=jest.fn();
const mLogin=jest.fn();
const mHandleErrors=jest.fn();
在每个之前(()=>{
SomeComponent.prototype.formValidation=mFormValidation;
SomeComponent.prototype.login=mLogin;
SomeComponent.prototype.handleErrors=mHandleErrors;
包装器=浅();
});
之后(()=>{
开玩笑。clearAllMocks();
});
它('应该呈现',()=>{
expect(wrapper.find('form')).toBeTruthy();
});
描述(#handleSubmit’,()=>{
它('应该登录',()=>{
mFormValidation.mockReturnValueOnce(true);
const mEvent={preventDefault:jest.fn()};
wrapper.find('form').simulate('submit',mEvent);
expect(mEvent.preventDefault).toBeCalledTimes(1);
预期(mLogin)。已被催收时间(1);
});
它('应该处理错误',()=>{
mFormValidation.mockReturnValueOnce(false);
const mEvent={preventDefault:jest.fn()};
wrapper.find('form').simulate('submit',mEvent);
expect(mEvent.preventDefault).toBeCalledTimes(1);
expect(mHandleErrors).toBeCalledWith({});
});
});
});
单元测试结果和覆盖率报告:

通过src/stackoverflow/59741487/index.test.jsx(13.687s)
59741487
✓ 应渲染(12毫秒)
#手推
✓ 应登录(2ms)
✓ 应处理错误(3ms)
-----------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
-----------|----------|----------|----------|----------|-------------------|
所有文件| 100 | 100 | 57.14 | 100 ||
index.jsx | 100 | 100 | 57.14 | 100 ||
-----------|----------|----------|----------|----------|-------------------|
测试套件:1个通过,共1个
测试:3次通过,共3次
快照:共0个
时间:15.854s
源代码: