react formik, test if submit function called on click - jest,enzyme
up vote
1
down vote
favorite
I am testing a react component which contains a form(using formik) and I need to test if on submit button clicked, whether submit function is called or not.
at the moment, the test is failing.
now, the form has required fields schema too using yup
so, I was wondering whether I need to fill up all the fields before testing it.
because at the moment, it doesnt submit until the form has errors i.e. if the required fieldss are empty. so does that obstruct the testing of the button click and the function being called or not?
describe('submitform', () =>
let wrapper = '';
const handleSubmit = jest.fn();
beforeEach(() =>
wrapper = mount(<ExampleButton >
<span className="visible-sm">Next</span>
<span className="visible-xs font-entity">
›
</span>
</ExampleButton>
);
);
afterEach(() =>
wrapper.unmount();
);
it('call function on click',async ()=>
// let btn = wrapper.find('#btnEx').find('button').find('#btnEx');
let btn = wrapper.find('button').simulate('click');
console.log('wrapper : ',btn.debug());
// btn.props().onClick();
expect(handleSubmit).toHaveBeenCalled();
);
)
how do I fill up the fields then, before testing? or is it even required for me to fill up the fields before testing on click?
javascript reactjs jestjs enzyme formik
add a comment |
up vote
1
down vote
favorite
I am testing a react component which contains a form(using formik) and I need to test if on submit button clicked, whether submit function is called or not.
at the moment, the test is failing.
now, the form has required fields schema too using yup
so, I was wondering whether I need to fill up all the fields before testing it.
because at the moment, it doesnt submit until the form has errors i.e. if the required fieldss are empty. so does that obstruct the testing of the button click and the function being called or not?
describe('submitform', () =>
let wrapper = '';
const handleSubmit = jest.fn();
beforeEach(() =>
wrapper = mount(<ExampleButton >
<span className="visible-sm">Next</span>
<span className="visible-xs font-entity">
›
</span>
</ExampleButton>
);
);
afterEach(() =>
wrapper.unmount();
);
it('call function on click',async ()=>
// let btn = wrapper.find('#btnEx').find('button').find('#btnEx');
let btn = wrapper.find('button').simulate('click');
console.log('wrapper : ',btn.debug());
// btn.props().onClick();
expect(handleSubmit).toHaveBeenCalled();
);
)
how do I fill up the fields then, before testing? or is it even required for me to fill up the fields before testing on click?
javascript reactjs jestjs enzyme formik
doesExampleButton
component contain a form(if so its name is really confusing)? where do you passhandleSubmit
mock? what does mean "test is failing"? is it aboutfunction has not been called
or something else?
– skyboyer
Nov 11 at 18:10
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am testing a react component which contains a form(using formik) and I need to test if on submit button clicked, whether submit function is called or not.
at the moment, the test is failing.
now, the form has required fields schema too using yup
so, I was wondering whether I need to fill up all the fields before testing it.
because at the moment, it doesnt submit until the form has errors i.e. if the required fieldss are empty. so does that obstruct the testing of the button click and the function being called or not?
describe('submitform', () =>
let wrapper = '';
const handleSubmit = jest.fn();
beforeEach(() =>
wrapper = mount(<ExampleButton >
<span className="visible-sm">Next</span>
<span className="visible-xs font-entity">
›
</span>
</ExampleButton>
);
);
afterEach(() =>
wrapper.unmount();
);
it('call function on click',async ()=>
// let btn = wrapper.find('#btnEx').find('button').find('#btnEx');
let btn = wrapper.find('button').simulate('click');
console.log('wrapper : ',btn.debug());
// btn.props().onClick();
expect(handleSubmit).toHaveBeenCalled();
);
)
how do I fill up the fields then, before testing? or is it even required for me to fill up the fields before testing on click?
javascript reactjs jestjs enzyme formik
I am testing a react component which contains a form(using formik) and I need to test if on submit button clicked, whether submit function is called or not.
at the moment, the test is failing.
now, the form has required fields schema too using yup
so, I was wondering whether I need to fill up all the fields before testing it.
because at the moment, it doesnt submit until the form has errors i.e. if the required fieldss are empty. so does that obstruct the testing of the button click and the function being called or not?
describe('submitform', () =>
let wrapper = '';
const handleSubmit = jest.fn();
beforeEach(() =>
wrapper = mount(<ExampleButton >
<span className="visible-sm">Next</span>
<span className="visible-xs font-entity">
›
</span>
</ExampleButton>
);
);
afterEach(() =>
wrapper.unmount();
);
it('call function on click',async ()=>
// let btn = wrapper.find('#btnEx').find('button').find('#btnEx');
let btn = wrapper.find('button').simulate('click');
console.log('wrapper : ',btn.debug());
// btn.props().onClick();
expect(handleSubmit).toHaveBeenCalled();
);
)
how do I fill up the fields then, before testing? or is it even required for me to fill up the fields before testing on click?
javascript reactjs jestjs enzyme formik
javascript reactjs jestjs enzyme formik
edited Nov 11 at 18:05
skyboyer
3,07611028
3,07611028
asked Nov 11 at 11:51
faraz
57031330
57031330
doesExampleButton
component contain a form(if so its name is really confusing)? where do you passhandleSubmit
mock? what does mean "test is failing"? is it aboutfunction has not been called
or something else?
– skyboyer
Nov 11 at 18:10
add a comment |
doesExampleButton
component contain a form(if so its name is really confusing)? where do you passhandleSubmit
mock? what does mean "test is failing"? is it aboutfunction has not been called
or something else?
– skyboyer
Nov 11 at 18:10
does
ExampleButton
component contain a form(if so its name is really confusing)? where do you pass handleSubmit
mock? what does mean "test is failing"? is it about function has not been called
or something else?– skyboyer
Nov 11 at 18:10
does
ExampleButton
component contain a form(if so its name is really confusing)? where do you pass handleSubmit
mock? what does mean "test is failing"? is it about function has not been called
or something else?– skyboyer
Nov 11 at 18:10
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248455%2freact-formik-test-if-submit-function-called-on-click-jest-enzyme%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
does
ExampleButton
component contain a form(if so its name is really confusing)? where do you passhandleSubmit
mock? what does mean "test is failing"? is it aboutfunction has not been called
or something else?– skyboyer
Nov 11 at 18:10