I am using Jest to test a React component written in TypeScript. I can't use .simulate()
because that is being deprecated in favor of just directly calling the component's onClick()
function prop. Here is my test code:
// Get the onClick function
const buttonOnClick = wrapper.find('#diffpicker-button').first().props().onClick;
// Make sure the function is not undefined
if (!buttonOnClick) return;
// Mock event
const clickEvent = new MouseEvent('click');
Object.assign(clickEvent, { preventDefault: jest.fn() });
// Call the onClick function
buttonOnClick(clickEvent);
However, when I do this I get the error: Argument of type 'MouseEvent' is not assignable to parameter of type 'MouseEvent<{}, MouseEvent>'.
My question is, how do I mock an event of type MouseEvent<{}, MouseEvent>?