Can't check if form submit works
See original GitHub issueHello! 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:
- Created 5 years ago
- Reactions:3
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
@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 forSignupForm
.