I need to test a function inside my stateless component as the source code below:
function App(props) {
const handleItemClick = () => {
if (true) {
props.doanything();
}
}
return (
<div onClick={handleItemClick}>
App
</div>
);
}
As suggested - if you can simply test your functionality by simulating user clicks then do take that approach. BUT, saying that testing internal implementation is 'bad practice' is unhelpful and impractical.
There is still a strong case for testing a function directly. Especially when your component is complex with several nested components making asynchronous calls to servers before the function you want to test can be run.
In this scenario - you're left with two choices, that I'm aware of:
I'd love there to be a better one and hopefully someone will suggest something in the comments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With