Reputation: 169
I'm totally lost trying to test a simple form made with React, how do I know if the submit button is working? After some research I thought that the way to do it is to make a mock function and then check if it was called but I'm pretty sure I'm doing it completely wrong.
onObjSubmit(event){
event.preventDefault()
..... (fetch something)
}
render(){
return (
<form id="myForm" onSubmit={event => this.onObjSubmit(event)}>
<input type="text" id="name" /><br />
<input type="text" id="last_name" /><br />
<input type="submit" value="submit">
</form>
);
}
And the test:
it('Test', () => {
const wrapper = shallow(<TestComp />);
const fn = jest.fn();
wrapper.instance().onObjSubmit = fn;
wrapper.update();
wrapper.find('#myForm').simulate('submit');
expect(fn).toBeCalled();
});
Can someone please point me in the right direction?
Upvotes: 5
Views: 10618
Reputation: 244
You're not the only one who feels lost with this one. This thread on Enzyme's issues page has a lot of suggested approaches to solving this problem.
Upvotes: 1