我可以通过添加这个功能来解决这个问题。
function backspace(element) {
let actuallyTyped = element.value;
const backspaceKey = {
key: 'Backspace',
code: 8,
inputType: 'deleteContentBackward',
};
const sharedEventConfig = {
key: backspaceKey.key,
charCode: backspaceKey.code,
keyCode: backspaceKey.code,
which: backspaceKey.code,
modifier: backspaceKey.modifier,
};
const downEvent = fireEvent.keyDown(element, sharedEventConfig);
if (downEvent) {
actuallyTyped = actuallyTyped.slice(0, -1);
fireEvent.input(element, {
target: { value: actuallyTyped },
inputType: backspaceKey.inputType,
bubbles: true,
cancelable: true,
});
}
fireEvent.keyUp(element, sharedEventConfig);
}
然后在我的测试任务中调用它。
// delete up 5 times
let count = 5;
do {
backspace(input);
} while (count--);