How to write test cases for a function using jest and enzyme












0















I have a react function which renders a component according to the value of props being passed. The function looks as shown below:



 getPhoneComp() {
if (this.props.contactDetails.countPhone === 1)
return (<PhoneComp contactDetails={this.props.contactDetails.phoneSet[0]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} />);
else if (this.props.contactDetails.countPhone === 2) {
return (
<div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[0]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[1]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
</div>
);

} else if (this.props.contactDetails.countPhone === 3) {
return (
<div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[0]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[1]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[2]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
</div>
);

}
else if (this.props.contactDetails.countPhone === 0) {
return (
<div />
);
}
}


And this function is called inside my render function as shown below:



  render() {
app.logger.getLogger().info("props" + JSON.stringify(this.props));
return (
<div>
{this.getPhoneComp()}
</div>

);
}


Now, I am trying to write a test case for this function, but I am not able to figure out how to proceed.I wrote the below test case and it's not throwing any error, but the coverage is still the same.My test looks like this:



   let phoneComp = shallow(<PhoneContainer contactDetails={contactDetailsState} errorHandler={errorHandlerFn} updateMobileNo={updateMobileNoFn} />);
phoneComp.instance().getPhoneComp();


Can someone please help me with this?










share|improve this question

























  • The answer is correct. Of course, you need to test getPhoneComp thoroughly (with all this.props.contactDetails.countPhone conditions) to get full coverage for this method. If you have problems with coverage after doing that, consider checking what lines lack coverage.

    – estus
    Nov 22 '18 at 10:46













  • @estus, so in my case I have to check this.props.contactDetails.countPhone, so I am not able to figure out how to change the value of the props and check.Also, the answer uses the spyOn function which I have already used earlier as you mentioned in the previous answer, so how to set the value of this.props.contactDetails.countPhone to test the function ?

    – pranami
    Nov 22 '18 at 10:52


















0















I have a react function which renders a component according to the value of props being passed. The function looks as shown below:



 getPhoneComp() {
if (this.props.contactDetails.countPhone === 1)
return (<PhoneComp contactDetails={this.props.contactDetails.phoneSet[0]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} />);
else if (this.props.contactDetails.countPhone === 2) {
return (
<div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[0]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[1]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
</div>
);

} else if (this.props.contactDetails.countPhone === 3) {
return (
<div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[0]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[1]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[2]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
</div>
);

}
else if (this.props.contactDetails.countPhone === 0) {
return (
<div />
);
}
}


And this function is called inside my render function as shown below:



  render() {
app.logger.getLogger().info("props" + JSON.stringify(this.props));
return (
<div>
{this.getPhoneComp()}
</div>

);
}


Now, I am trying to write a test case for this function, but I am not able to figure out how to proceed.I wrote the below test case and it's not throwing any error, but the coverage is still the same.My test looks like this:



   let phoneComp = shallow(<PhoneContainer contactDetails={contactDetailsState} errorHandler={errorHandlerFn} updateMobileNo={updateMobileNoFn} />);
phoneComp.instance().getPhoneComp();


Can someone please help me with this?










share|improve this question

























  • The answer is correct. Of course, you need to test getPhoneComp thoroughly (with all this.props.contactDetails.countPhone conditions) to get full coverage for this method. If you have problems with coverage after doing that, consider checking what lines lack coverage.

    – estus
    Nov 22 '18 at 10:46













  • @estus, so in my case I have to check this.props.contactDetails.countPhone, so I am not able to figure out how to change the value of the props and check.Also, the answer uses the spyOn function which I have already used earlier as you mentioned in the previous answer, so how to set the value of this.props.contactDetails.countPhone to test the function ?

    – pranami
    Nov 22 '18 at 10:52
















0












0








0








I have a react function which renders a component according to the value of props being passed. The function looks as shown below:



 getPhoneComp() {
if (this.props.contactDetails.countPhone === 1)
return (<PhoneComp contactDetails={this.props.contactDetails.phoneSet[0]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} />);
else if (this.props.contactDetails.countPhone === 2) {
return (
<div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[0]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[1]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
</div>
);

} else if (this.props.contactDetails.countPhone === 3) {
return (
<div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[0]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[1]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[2]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
</div>
);

}
else if (this.props.contactDetails.countPhone === 0) {
return (
<div />
);
}
}


And this function is called inside my render function as shown below:



  render() {
app.logger.getLogger().info("props" + JSON.stringify(this.props));
return (
<div>
{this.getPhoneComp()}
</div>

);
}


Now, I am trying to write a test case for this function, but I am not able to figure out how to proceed.I wrote the below test case and it's not throwing any error, but the coverage is still the same.My test looks like this:



   let phoneComp = shallow(<PhoneContainer contactDetails={contactDetailsState} errorHandler={errorHandlerFn} updateMobileNo={updateMobileNoFn} />);
phoneComp.instance().getPhoneComp();


Can someone please help me with this?










share|improve this question
















I have a react function which renders a component according to the value of props being passed. The function looks as shown below:



 getPhoneComp() {
if (this.props.contactDetails.countPhone === 1)
return (<PhoneComp contactDetails={this.props.contactDetails.phoneSet[0]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} />);
else if (this.props.contactDetails.countPhone === 2) {
return (
<div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[0]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[1]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
</div>
);

} else if (this.props.contactDetails.countPhone === 3) {
return (
<div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[0]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[1]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
<div className={classes.sceESpace}><PhoneComp contactDetails={this.props.contactDetails.phoneSet[2]} contactId={this.props.contactDetails.contactId} errorHandler={this.props.errorHandler} updateMobileNo={this.props.updateMobileNo} /></div>
</div>
);

}
else if (this.props.contactDetails.countPhone === 0) {
return (
<div />
);
}
}


And this function is called inside my render function as shown below:



  render() {
app.logger.getLogger().info("props" + JSON.stringify(this.props));
return (
<div>
{this.getPhoneComp()}
</div>

);
}


Now, I am trying to write a test case for this function, but I am not able to figure out how to proceed.I wrote the below test case and it's not throwing any error, but the coverage is still the same.My test looks like this:



   let phoneComp = shallow(<PhoneContainer contactDetails={contactDetailsState} errorHandler={errorHandlerFn} updateMobileNo={updateMobileNoFn} />);
phoneComp.instance().getPhoneComp();


Can someone please help me with this?







reactjs jestjs enzyme






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 10:47









skyboyer

3,54111128




3,54111128










asked Nov 22 '18 at 10:15









pranamipranami

237212




237212













  • The answer is correct. Of course, you need to test getPhoneComp thoroughly (with all this.props.contactDetails.countPhone conditions) to get full coverage for this method. If you have problems with coverage after doing that, consider checking what lines lack coverage.

    – estus
    Nov 22 '18 at 10:46













  • @estus, so in my case I have to check this.props.contactDetails.countPhone, so I am not able to figure out how to change the value of the props and check.Also, the answer uses the spyOn function which I have already used earlier as you mentioned in the previous answer, so how to set the value of this.props.contactDetails.countPhone to test the function ?

    – pranami
    Nov 22 '18 at 10:52





















  • The answer is correct. Of course, you need to test getPhoneComp thoroughly (with all this.props.contactDetails.countPhone conditions) to get full coverage for this method. If you have problems with coverage after doing that, consider checking what lines lack coverage.

    – estus
    Nov 22 '18 at 10:46













  • @estus, so in my case I have to check this.props.contactDetails.countPhone, so I am not able to figure out how to change the value of the props and check.Also, the answer uses the spyOn function which I have already used earlier as you mentioned in the previous answer, so how to set the value of this.props.contactDetails.countPhone to test the function ?

    – pranami
    Nov 22 '18 at 10:52



















The answer is correct. Of course, you need to test getPhoneComp thoroughly (with all this.props.contactDetails.countPhone conditions) to get full coverage for this method. If you have problems with coverage after doing that, consider checking what lines lack coverage.

– estus
Nov 22 '18 at 10:46







The answer is correct. Of course, you need to test getPhoneComp thoroughly (with all this.props.contactDetails.countPhone conditions) to get full coverage for this method. If you have problems with coverage after doing that, consider checking what lines lack coverage.

– estus
Nov 22 '18 at 10:46















@estus, so in my case I have to check this.props.contactDetails.countPhone, so I am not able to figure out how to change the value of the props and check.Also, the answer uses the spyOn function which I have already used earlier as you mentioned in the previous answer, so how to set the value of this.props.contactDetails.countPhone to test the function ?

– pranami
Nov 22 '18 at 10:52







@estus, so in my case I have to check this.props.contactDetails.countPhone, so I am not able to figure out how to change the value of the props and check.Also, the answer uses the spyOn function which I have already used earlier as you mentioned in the previous answer, so how to set the value of this.props.contactDetails.countPhone to test the function ?

– pranami
Nov 22 '18 at 10:52














2 Answers
2






active

oldest

votes


















2














You need to add a spy and an expect statement:



  let node = shallow(<PhoneContainer ... />);

const getPhoneCompSpy = jest.spyOn(node.instance(), 'phoneComp');

expect(getPhoneCompSpy).toHaveBeenCalled();


You can find more details about spys here






share|improve this answer


























  • Thanks for the answer, but I have already used the spyOn menthod, So I have doubt in how to set the this.props.contactDetails.countPhone value to test the function? Can you please just give me a rough idea on updating the props in the test.

    – pranami
    Nov 22 '18 at 10:55






  • 2





    Sure, this is how you would update your props before you define your spy node.setProps({contactDetails: {countPhone: 'foobar'}}); }; node.update();

    – Fabian Hinsenkamp
    Nov 22 '18 at 10:59













  • It works, thank you so much for your help :)

    – pranami
    Nov 22 '18 at 11:22



















0














In the way, Fabian have explained to you, you are only checking if the method is being called and nothing else. The logic inside it, like checking the countPhone and rendering the PhoneComp accordingly, aren't under tests.



IMHO, this is not a good approach at all. You are creating a very fragile test. If you do a minor refactor like rename the method, would break your tests.



In your tests, you have to check what's is actually being rendered at the end and nothing else.



One way to do that is using the find and checking the length for the result.



For example:



let phoneComp = shallow(<PhoneContainer contactDetails={contactDetailsState};
expect(phoneComp.find(PhoneComp)).toHaveLength(1); // or 2 or 3 regarding the case


In this way, you are actually testing the output of the function.






share|improve this answer
























  • A fragile test is a good test, in fact. It's better to get a red test when you don't expect it to break rather than get a green test when you expect it to break.

    – estus
    Nov 22 '18 at 11:54













  • Of course you can also check for what's actually rendered in the end. Which is a valid approach. However, as I understand the question it was especially wanted to test that this very specific method is called. The procedure I described is a generic approach to test methods, no matter if they return jsx or anything else.

    – Fabian Hinsenkamp
    Nov 22 '18 at 12:07











  • Hey Fabian, sorry if I offended you. I just tried to give another perspective about how to test.

    – Arthur Almeida
    Nov 23 '18 at 19:36











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',
autoActivateHeartbeat: false,
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%2f53428619%2fhow-to-write-test-cases-for-a-function-using-jest-and-enzyme%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You need to add a spy and an expect statement:



  let node = shallow(<PhoneContainer ... />);

const getPhoneCompSpy = jest.spyOn(node.instance(), 'phoneComp');

expect(getPhoneCompSpy).toHaveBeenCalled();


You can find more details about spys here






share|improve this answer


























  • Thanks for the answer, but I have already used the spyOn menthod, So I have doubt in how to set the this.props.contactDetails.countPhone value to test the function? Can you please just give me a rough idea on updating the props in the test.

    – pranami
    Nov 22 '18 at 10:55






  • 2





    Sure, this is how you would update your props before you define your spy node.setProps({contactDetails: {countPhone: 'foobar'}}); }; node.update();

    – Fabian Hinsenkamp
    Nov 22 '18 at 10:59













  • It works, thank you so much for your help :)

    – pranami
    Nov 22 '18 at 11:22
















2














You need to add a spy and an expect statement:



  let node = shallow(<PhoneContainer ... />);

const getPhoneCompSpy = jest.spyOn(node.instance(), 'phoneComp');

expect(getPhoneCompSpy).toHaveBeenCalled();


You can find more details about spys here






share|improve this answer


























  • Thanks for the answer, but I have already used the spyOn menthod, So I have doubt in how to set the this.props.contactDetails.countPhone value to test the function? Can you please just give me a rough idea on updating the props in the test.

    – pranami
    Nov 22 '18 at 10:55






  • 2





    Sure, this is how you would update your props before you define your spy node.setProps({contactDetails: {countPhone: 'foobar'}}); }; node.update();

    – Fabian Hinsenkamp
    Nov 22 '18 at 10:59













  • It works, thank you so much for your help :)

    – pranami
    Nov 22 '18 at 11:22














2












2








2







You need to add a spy and an expect statement:



  let node = shallow(<PhoneContainer ... />);

const getPhoneCompSpy = jest.spyOn(node.instance(), 'phoneComp');

expect(getPhoneCompSpy).toHaveBeenCalled();


You can find more details about spys here






share|improve this answer















You need to add a spy and an expect statement:



  let node = shallow(<PhoneContainer ... />);

const getPhoneCompSpy = jest.spyOn(node.instance(), 'phoneComp');

expect(getPhoneCompSpy).toHaveBeenCalled();


You can find more details about spys here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 10:45

























answered Nov 22 '18 at 10:41









Fabian HinsenkampFabian Hinsenkamp

1716




1716













  • Thanks for the answer, but I have already used the spyOn menthod, So I have doubt in how to set the this.props.contactDetails.countPhone value to test the function? Can you please just give me a rough idea on updating the props in the test.

    – pranami
    Nov 22 '18 at 10:55






  • 2





    Sure, this is how you would update your props before you define your spy node.setProps({contactDetails: {countPhone: 'foobar'}}); }; node.update();

    – Fabian Hinsenkamp
    Nov 22 '18 at 10:59













  • It works, thank you so much for your help :)

    – pranami
    Nov 22 '18 at 11:22



















  • Thanks for the answer, but I have already used the spyOn menthod, So I have doubt in how to set the this.props.contactDetails.countPhone value to test the function? Can you please just give me a rough idea on updating the props in the test.

    – pranami
    Nov 22 '18 at 10:55






  • 2





    Sure, this is how you would update your props before you define your spy node.setProps({contactDetails: {countPhone: 'foobar'}}); }; node.update();

    – Fabian Hinsenkamp
    Nov 22 '18 at 10:59













  • It works, thank you so much for your help :)

    – pranami
    Nov 22 '18 at 11:22

















Thanks for the answer, but I have already used the spyOn menthod, So I have doubt in how to set the this.props.contactDetails.countPhone value to test the function? Can you please just give me a rough idea on updating the props in the test.

– pranami
Nov 22 '18 at 10:55





Thanks for the answer, but I have already used the spyOn menthod, So I have doubt in how to set the this.props.contactDetails.countPhone value to test the function? Can you please just give me a rough idea on updating the props in the test.

– pranami
Nov 22 '18 at 10:55




2




2





Sure, this is how you would update your props before you define your spy node.setProps({contactDetails: {countPhone: 'foobar'}}); }; node.update();

– Fabian Hinsenkamp
Nov 22 '18 at 10:59







Sure, this is how you would update your props before you define your spy node.setProps({contactDetails: {countPhone: 'foobar'}}); }; node.update();

– Fabian Hinsenkamp
Nov 22 '18 at 10:59















It works, thank you so much for your help :)

– pranami
Nov 22 '18 at 11:22





It works, thank you so much for your help :)

– pranami
Nov 22 '18 at 11:22













0














In the way, Fabian have explained to you, you are only checking if the method is being called and nothing else. The logic inside it, like checking the countPhone and rendering the PhoneComp accordingly, aren't under tests.



IMHO, this is not a good approach at all. You are creating a very fragile test. If you do a minor refactor like rename the method, would break your tests.



In your tests, you have to check what's is actually being rendered at the end and nothing else.



One way to do that is using the find and checking the length for the result.



For example:



let phoneComp = shallow(<PhoneContainer contactDetails={contactDetailsState};
expect(phoneComp.find(PhoneComp)).toHaveLength(1); // or 2 or 3 regarding the case


In this way, you are actually testing the output of the function.






share|improve this answer
























  • A fragile test is a good test, in fact. It's better to get a red test when you don't expect it to break rather than get a green test when you expect it to break.

    – estus
    Nov 22 '18 at 11:54













  • Of course you can also check for what's actually rendered in the end. Which is a valid approach. However, as I understand the question it was especially wanted to test that this very specific method is called. The procedure I described is a generic approach to test methods, no matter if they return jsx or anything else.

    – Fabian Hinsenkamp
    Nov 22 '18 at 12:07











  • Hey Fabian, sorry if I offended you. I just tried to give another perspective about how to test.

    – Arthur Almeida
    Nov 23 '18 at 19:36
















0














In the way, Fabian have explained to you, you are only checking if the method is being called and nothing else. The logic inside it, like checking the countPhone and rendering the PhoneComp accordingly, aren't under tests.



IMHO, this is not a good approach at all. You are creating a very fragile test. If you do a minor refactor like rename the method, would break your tests.



In your tests, you have to check what's is actually being rendered at the end and nothing else.



One way to do that is using the find and checking the length for the result.



For example:



let phoneComp = shallow(<PhoneContainer contactDetails={contactDetailsState};
expect(phoneComp.find(PhoneComp)).toHaveLength(1); // or 2 or 3 regarding the case


In this way, you are actually testing the output of the function.






share|improve this answer
























  • A fragile test is a good test, in fact. It's better to get a red test when you don't expect it to break rather than get a green test when you expect it to break.

    – estus
    Nov 22 '18 at 11:54













  • Of course you can also check for what's actually rendered in the end. Which is a valid approach. However, as I understand the question it was especially wanted to test that this very specific method is called. The procedure I described is a generic approach to test methods, no matter if they return jsx or anything else.

    – Fabian Hinsenkamp
    Nov 22 '18 at 12:07











  • Hey Fabian, sorry if I offended you. I just tried to give another perspective about how to test.

    – Arthur Almeida
    Nov 23 '18 at 19:36














0












0








0







In the way, Fabian have explained to you, you are only checking if the method is being called and nothing else. The logic inside it, like checking the countPhone and rendering the PhoneComp accordingly, aren't under tests.



IMHO, this is not a good approach at all. You are creating a very fragile test. If you do a minor refactor like rename the method, would break your tests.



In your tests, you have to check what's is actually being rendered at the end and nothing else.



One way to do that is using the find and checking the length for the result.



For example:



let phoneComp = shallow(<PhoneContainer contactDetails={contactDetailsState};
expect(phoneComp.find(PhoneComp)).toHaveLength(1); // or 2 or 3 regarding the case


In this way, you are actually testing the output of the function.






share|improve this answer













In the way, Fabian have explained to you, you are only checking if the method is being called and nothing else. The logic inside it, like checking the countPhone and rendering the PhoneComp accordingly, aren't under tests.



IMHO, this is not a good approach at all. You are creating a very fragile test. If you do a minor refactor like rename the method, would break your tests.



In your tests, you have to check what's is actually being rendered at the end and nothing else.



One way to do that is using the find and checking the length for the result.



For example:



let phoneComp = shallow(<PhoneContainer contactDetails={contactDetailsState};
expect(phoneComp.find(PhoneComp)).toHaveLength(1); // or 2 or 3 regarding the case


In this way, you are actually testing the output of the function.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 11:45









Arthur AlmeidaArthur Almeida

29915




29915













  • A fragile test is a good test, in fact. It's better to get a red test when you don't expect it to break rather than get a green test when you expect it to break.

    – estus
    Nov 22 '18 at 11:54













  • Of course you can also check for what's actually rendered in the end. Which is a valid approach. However, as I understand the question it was especially wanted to test that this very specific method is called. The procedure I described is a generic approach to test methods, no matter if they return jsx or anything else.

    – Fabian Hinsenkamp
    Nov 22 '18 at 12:07











  • Hey Fabian, sorry if I offended you. I just tried to give another perspective about how to test.

    – Arthur Almeida
    Nov 23 '18 at 19:36



















  • A fragile test is a good test, in fact. It's better to get a red test when you don't expect it to break rather than get a green test when you expect it to break.

    – estus
    Nov 22 '18 at 11:54













  • Of course you can also check for what's actually rendered in the end. Which is a valid approach. However, as I understand the question it was especially wanted to test that this very specific method is called. The procedure I described is a generic approach to test methods, no matter if they return jsx or anything else.

    – Fabian Hinsenkamp
    Nov 22 '18 at 12:07











  • Hey Fabian, sorry if I offended you. I just tried to give another perspective about how to test.

    – Arthur Almeida
    Nov 23 '18 at 19:36

















A fragile test is a good test, in fact. It's better to get a red test when you don't expect it to break rather than get a green test when you expect it to break.

– estus
Nov 22 '18 at 11:54







A fragile test is a good test, in fact. It's better to get a red test when you don't expect it to break rather than get a green test when you expect it to break.

– estus
Nov 22 '18 at 11:54















Of course you can also check for what's actually rendered in the end. Which is a valid approach. However, as I understand the question it was especially wanted to test that this very specific method is called. The procedure I described is a generic approach to test methods, no matter if they return jsx or anything else.

– Fabian Hinsenkamp
Nov 22 '18 at 12:07





Of course you can also check for what's actually rendered in the end. Which is a valid approach. However, as I understand the question it was especially wanted to test that this very specific method is called. The procedure I described is a generic approach to test methods, no matter if they return jsx or anything else.

– Fabian Hinsenkamp
Nov 22 '18 at 12:07













Hey Fabian, sorry if I offended you. I just tried to give another perspective about how to test.

– Arthur Almeida
Nov 23 '18 at 19:36





Hey Fabian, sorry if I offended you. I just tried to give another perspective about how to test.

– Arthur Almeida
Nov 23 '18 at 19:36


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428619%2fhow-to-write-test-cases-for-a-function-using-jest-and-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







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin