#1722·enzyme

Can't check if form submit works

Author: floodicoCreated Jul 23, 2018Updated Sep 6, 2022

Hello! I want to test my registration form submit, but can't check if submit handler is called. Here is my form:

     <form onSubmit={this.handleSubmit}>
        <label htmlFor="email">Email: </label> 
        <input
          name="email"
          id="email"
          type="email" />
        <label htmlFor="password">Password:</label>
        <input
          name="password"
          id="password"
          type="password" />
        <label htmlFor="password">Confirm password:</label>
        <input
          name="confirm"
          id="confirm"
          type="password" />
        <input type="submit" value="Sign Up"/>
      </form>
  

There is my test:

  it('submit works', () => {
    let submit = jest.fn();
    let wrapper = mount(<SignupForm onSubmit={submit}/>);
    wrapper.simulate('submit', { submit });
    expect(submit).toBeCalled()
  })

I get FAIL - Expected mock function to have been called.

debug()

I use mount because i can set props and this is how looks wrapper.debug():

SignupForm onSubmit={[Function: mockConstructor]}>
      <form onSubmit={[Function]}>
      ....
      </form>
    </SignupForm>