【发布时间】:2021-03-23 14:02:43
【问题描述】:
我想测试当用户通过交互式 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');
}
}
【问题讨论】:
标签: reactjs typescript jestjs