react mock usestate code example
Example: mockimplementation for setstate
import * as React from 'react';
describe('Some message', () => {
const setState = jest.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const useStateMock: any = (initState: any) => [initState, setState];
afterEach(() => {
jest.clearAllMocks();
});
it('Is a test where we want to mock useState', () => {
jest.spyOn(React, 'useState').mockImplementation(useStateMock);
const wrapper = shallow(<Component {...props} />);
// trigger setState somehow
expect(setState).toHaveBeenCalledTimes(1);
// Other tests here
});
});