question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't check if form submit works

See original GitHub issue

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>

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
ljharbcommented, Jul 23, 2018

You don’t have to test if onSubmit works - that’d be testing React itself, and the browser, which isn’t your concern.

All you have to test is that the form has an onSubmit prop, that when invoked (explicitly, not using simulate) does what you expect.

Separately, I’d need to see your component code to be sure how to recommend testing that.

3reactions
ljharbcommented, Jul 24, 2018

@floodico right, you don’t need to test that - React and the browser will be sure it’s called. All you need to assert is that the onSubmit is set to the function you expect, and that the function you expect behaves the way you expect.

So, in your case, all you need to do is wrapper.prop('onSubmit') === submit, and you’ve tested everything about that that you need to for SignupForm.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Form Submit jQuery does not work - Stack Overflow
i.e. You want to attach to the submit event (which you've got) but using the required attributes and input types for data validation...
Read more >
10 Things You Should Know About Power Apps Forms
Get The Form's Last Submitted Record (Last Submit Property) 5. Check If A Form Is Unsaved (Unsaved Property) 6. Get A Single Record...
Read more >
Can not submit form after validation - FormValidation
Can not submit form after validation. There are some mistakes causing the issue that the form can't be submitted although the validation is...
Read more >
Form validation isn't working when submitting it using Javascript
If you want to run some JS code upon clicking of the submit button then your javascript method should return true or false...
Read more >
Django Tutorial Part 9: Working with forms - MDN Web Docs
Developers need to write HTML for the form, validate and properly ... and add some validation to ensure that the librarian can't enter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found