TypeError:当我尝试使用JEST测试方法时,dispatch不是函数 [英] TypeError: dispatch is not a function when I try to test a method using JEST
本文介绍了TypeError:当我尝试使用JEST测试方法时,dispatch不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个方法,该方法接收一些值作为参数,然后调度一个动作.问题是,当我使组件变浅以测试此方法时,出现错误消息,指出dispatch不是函数.
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.
测试:
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}})
}
有人可以帮助我吗?我不知道我在做什么错. 预先感谢!
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();
});
})
我现在遇到的问题是:ReferenceError:提交未定义 有什么建议@RIYAJ KHAN吗?
The problem now I get is: ReferenceError: submit is not defined Any suggestions @RIYAJ KHAN ?
这篇关于TypeError:当我尝试使用JEST测试方法时,dispatch不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文