I have a component like this to test:
import {doSomethingMocked} form 'mockedLibrary';
const TheComponent = (props) => {
const [isToTest, setIsToTest] = useState(false);
const handleClick = useCallback(()=>{
if(isToTest) return;
setIsToTest(true);
doSomethingMocked();
}, [isToTest]);
return (
<button onClick={handleClick}>Click me</button>
);
}
The Unit Test is something like this
import {render, fireEvent, act} form '@testing-library/react';
import {doSomethingMocked} form 'mockedLibrary';
jest.mock('mockedLibrary');
describe('The Component', ()=>{
beforeEach(async ()=>{
const sut = render(<MyProvider value={{}}><TheComponent/></MyProvider>);
await act(async()=> await fireEvent.click(sut.getByText('Click me'))); // in theory the act now is unnecesary, any way still the state dows not toggle
await act(async()=> await fireEvent.click(sut.getByText('Click me')));
});
it('Should only doSomething once', sync ()=>{
expect(doSomethingMocked).toHaveBeenCalledTimes(1); // but it's called twice
})
})
How can I test the boolean useState toggles?
I already run the component and works fine, but test wise, the useState change never happens.
Previously with enzyme after the wraper.update() was enought to test it.
