Reputation: 699
I have created a function in my component. I want to know how can I write unit test cases for that function.
component code is -
const Test = () => {
if(props.Test) {
props.Test();
}
}
my Test code is -
const Test =() => {
console.log("hi");
}
and then pass this Test function into my component. I am getting code coverage is not done in this function. So how to achieve code coverage for this function ?
I am passing a function named Test in this component. I need to check if I am getting this component in props then execute this function. So how Can I write unit test cases for this function in jest ?
Upvotes: 0
Views: 2671
Reputation: 6006
First mock the function, for instance:
const mockTest = jest.fn()
;
Then pass it as a prop to your component, and test if it was called:
expect(mockTest).toHaveBeenCalled();
More on that: https://jestjs.io/docs/en/mock-functions.html
Upvotes: 1