TypeError: dispatch is not a function when I try t

2019-08-02 23:46发布

问题:

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:

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!

回答1:

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 ?