mockimplementation for setstate

Solutions on MaxInterview for mockimplementation for setstate by the best coders in the world

showing results for - "mockimplementation for setstate"
Mandy
31 Feb 2019
1import * as React from 'react';
2
3describe('Some message', () => {
4    const setState = jest.fn();
5    // eslint-disable-next-line @typescript-eslint/no-explicit-any
6    const useStateMock: any = (initState: any) => [initState, setState];
7
8    afterEach(() => {
9      jest.clearAllMocks();
10    });
11
12    it('Is a test where we want to mock useState', () => {
13          jest.spyOn(React, 'useState').mockImplementation(useStateMock);
14          const wrapper = shallow(<Component {...props} />);
15          // trigger setState somehow
16          expect(setState).toHaveBeenCalledTimes(1);
17          // Other tests here
18    });
19});
20