'React / Jest / react testing library test case for function but not getting covered
I am currently trying to test the handleClick() function in React / Jest / React-testing-library:
// state
const [toggle, setToggle] = React.useState<boolean>(true);
const Toggle: React.FunctionComponent = () => {
const handleClick = () => setToggle(toggle => !toggle);
return (
<button onClick={handleClick}>{toggle ? 'Open' : 'Close'}</button>
)
}
test case:
it('Should cover the setToggle function', () => {
const toggleFunction = jest.fn((toggle => !toggle));
let tree = create(<button onClick={toggleFunction}>Item</button>);
const button = tree.root.findByType('button')
button.props.onClick()
expect(toggleFunction).toBeCalledTimes(1);
});
This test passes, however the setToggle(toggle => !toggle highlights as not covered in code coverage? What am I doing wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
