我想测试当用户通过交互式 HTMLElement 按 Enter 键时是否调用 console.log 函数。我一直在尝试使用 Typescript 在 Jest 中模拟以下函数的事件对象,但它不起作用。如何使用 Jest 模拟 React/Typescript 中的事件对象?
例子:
import React from 'react';
function handleKey(event: React.KeyboardEvent) {
if (event.key === 'Enter') {
console.log('The enter key has been clicked');
}
}
Run Code Online (Sandbox Code Playgroud)