javascript - 类型错误 : dispatch is not a function when I try to test a method using JEST

标签 javascript reactjs unit-testing jestjs enzyme

我有一个方法,它接收一些值作为参数,然后调度一个操作。问题是,当我浅化组件来测试此方法时,出现错误,指出调度不是函数。

测试:

test('it changes the state when submit is clicked', () => {
    const wrapper = shallow(<WizardForm store={store}/>);
    const values = {
    entrySign: 'archivoSign',
    signCertificateFile: 'file',
    signCertificate: 'text',
    entryAuth: 'notArchivoAuth',
    authCertificateFile: 'file',
    authCertificate: 'text'
}
const form = wrapper.instance();
//in this method I get the error
form.submit(values)

方法:

submit(values) {
  var authCertificate = this.checkAuth(values);
  var signCertificate = this.checkSign(values);
  let req = {
    authCertificate: authCertificate,
    signCertificate: signCertificate,
    userId: this.state.userId
  }
  const { dispatch } = this.props
  dispatch({type: 'CERTIFICATES_FETCH_REQUESTED', payload: {req}})
}

有人可以帮助我吗?我不知道我做错了什么。 提前致谢!

最佳答案

好的,现在我有这个测试:

it('works', () => {
const values = {
  username: 'marc',
  name: 'marc',
  email: 'marc@hotmail.com',
  entrySign: 'archivoSign',
  signCertificateFile: 'file',
  signCertificate: 'text',
  entryAuth: 'notArchivoAuth',
  authCertificateFile: 'file',
  authCertificate: 'text'
} 
const mapDispatchToProps = (dispatch) => ({
  submit
});
const mockedStore = createMockStore();
const WizardFormWrapper = connect(reduxFormReducer, mapDispatchToProps)(WizardForm);
const wrapper = shallowWithStore(<WizardFormWrapper />, mockedStore);
   wrapper.props().submit();

});        
})

现在我遇到的问题是:ReferenceError:提交未定义 有什么建议@RIYAJ KHAN 吗?

关于javascript - 类型错误 : dispatch is not a function when I try to test a method using JEST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45935922/

相关文章:

javascript - Node.js 解析路由的最小函数

javascript - 使用CSS3关键帧 super 彩虹改变javascript donut 数据的颜色

javascript - 在 Google Apps 脚本中创建哈希数组

javascript - 如何将 html 转换为 JSX、ReactJS 组件?

ios - 如果编译时间超过可接受的水平,我可以创建一个失败的单元测试吗?

java - 无法在 JMockit 中模拟通用接口(interface)

javascript - 如何从 JSF clientId 生成唯一的 JavaScript 变量名称?

javascript - 无法渲染基于关闭功能的组件

javascript - React Hooks,如何在两个函数之间共享状态

unit-testing - 我什么时候应该用 TDD 编写不同类型的测试?