I'm writing test for the below component with React Testing Library. And when I checked the test coverage, it showed me that coverage for the function passed to the prop 'errorRender' has not been covered.
Below is my component :
import React from 'react';
import Icon from './Icon';
import Image from './Image';
function Container(props) {
const {img, caption} = props;
return (
<figure>
<div>
<Image
img={img}
errorRender={() => (
<div>
<div>
<Icon/>
</div>
</div>
)}
/>
</div>
{caption && (
<figcaption>{caption}</figcaption>
)}
</figure>
);
}
export default Container;
How could I test the function passed to the errorRender prop? I was thinking to avoid testing the child Component 'Image' as it would be tested separately, but dropping this doesn't give me 100% test coverage which is required in our project.
I was mocking the 'Image' child component as below in my test file :
jest.mock('./Image', () => {
return function Image() {
return <div> Image </div>
};
});
Then did this :
jest.mock('./Image', () => {
return function Image({errorRender}) {
errorRender();
return <div> Image </div>
};
});
Although the above mocking is giving me 100% test coverage but being new to the React Testing, I'm unsure is it the right way to do so? Should we test the values passed to the child component in parent's component test file? And what should be the right way ?