kaniosrn-j
12/2/2019 - 10:09 AM

Testing React Hooks snippet - useState

// Note: There isn's a way to test an actual state value that is controlled by useState.
// The easiest way to test is testing what setState was called with. For example,mock useState:

const initialSelected = 0;

const mockSetSelected = jest.fn();

const mockUseState = jest.fn().mockReturnValue([initialSelected, mockSetSelected]);

React.useState = mockUseState;

// code that should set selected to 1

expect(mockSetSelected).toHaveBeenCalledWith(1);