我正在使用 Jest 测试用 TypeScript 编写的 React 组件。我无法使用.simulate(),因为它已被弃用,而只支持直接调用组件的onClick()函数 prop。这是我的测试代码:
// 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);
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,我收到错误:Argument of type 'MouseEvent' is not assignable to parameter of type 'MouseEvent<{}, MouseEvent>'.我的问题是,如何模拟 MouseEvent<{}, MouseEvent> 类型的事件?