'Jest test react hook method to get better code coverage

I have a component, that sets state based on the onClick method as follows:

const [active, setActive] = React.useState(0);

<button type="button" onClick={() => {setActive(() => index); inner.fc(index)}} key={inner.id}>{inner.title}</button>

The test itself is:

it('Should test setActive index of button', (): void => {
    const setIndex = jest.fn(() => 1);
    let tree = create(<button type="button" onClick={setIndex} key='1'>1 button</button>);
    const button = tree.root.findByType('button')
    button.props.onClick()
    expect(setIndex()).toBe(1);
});

This passes but when I look at the coverage report, it flags the setActive method as not covered? How do I test that React.useState method correctly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source