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">
&rsaquo;
</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?










share|improve this question























  • 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














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">
&rsaquo;
</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?










share|improve this question























  • 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












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">
&rsaquo;
</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?










share|improve this question















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">
&rsaquo;
</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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:05









skyboyer

3,07611028




3,07611028










asked Nov 11 at 11:51









faraz

57031330




57031330











  • 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















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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto