简体   繁体   English

TypeError:当我尝试使用JEST测试方法时,dispatch不是函数

[英]TypeError: dispatch is not a function when I try to test a method using JEST

I have a method that receives some values as a parameter and then dispatches an action. 我有一个方法,该方法接收一些值作为参数,然后调度一个动作。 The problem is that when I shallow my component to test this method, I have an error saying that dispatch is not a function. 问题是,当我使组件变浅以测试此方法时,出现一个错误,指出dispatch不是函数。

TEST: 测试:

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)

METHOD: 方法:

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}})
}

Can anyone help me? 谁能帮我? I do not know what I am doing wrong. 我不知道我在做什么错。 Thanks in advance! 提前致谢!

Okay so now I have this test: 好吧,现在我有这个测试:

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();

});        
})

The problem now I get is: ReferenceError: submit is not defined Any suggestions @RIYAJ KHAN ? 我现在遇到的问题是:ReferenceError:未定义提交任何建议@RIYAJ KHAN吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 开玩笑时出现错误,我尝试运行测试 - Error on jest when I try to run the test 尝试测试异步操作时,`TypeError: store.dispatch(...).then is not a function` - `TypeError: store.dispatch(…).then is not a function` when trying to test async actions 当我尝试测试我的重试工具 function 时,jest.advanceTimersByTime 不起作用 - jest.advanceTimersByTime doesn't work when I try to test my retry util function 玩笑错误 TypeError: (0 , _jest.test) is not a function - Jest error TypeError: (0 , _jest.test) is not a function TypeError: dispatch is not a function when using react-context api - TypeError: dispatch is not a function when using react-context api 如何使用Jest在ES6类方法中测试对redux存储的调度? - How can I test a dispatch to a redux store in an ES6 class method with Jest? 尝试在 React/Redux/Jest 中测试 dispatch action 函数时获取 .then 的 undefined - Getting .then of undefined when trying to test a dispatch action function in React/Redux/Jest &#39;TypeError: store.dispatch is not a function&#39; redux action testing with jest - 'TypeError: store.dispatch is not a function' redux action testing with jest 当我尝试使用ngIf运行方法时,获取“ERROR TypeError:jit_nodeValue_6(...)不是函数” - Getting “ERROR TypeError: jit_nodeValue_6(…) is not a function” when I try to run a method with ngIf 如何使用Jest在异步调用中测试动作缩减器并进行调度? - How Can I test a an action reducer with an async call and dispatch inside using Jest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM