我有一个文本区域组件,假设在不使用shift修饰符的情况下按下回车键时提交表单。
在我的应用程序上,它工作正常,但下面的测试返回了一个错误。
it('should not submit the form when the shift key is pressed and the message is not empty', async () => {
render(<NewMessageForm onSubmit={onSubmit} />);
const element = screen.getByRole('textbox');
const message = 'Hello World';
await userEvent.type(element, message);
await userEvent.type(element, '{shift}{enter}');
expect(onSubmit).not.toHaveBeenCalled();
});
这是我的组件上的事件处理程序。
const handleKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => {
if (event.key === 'Enter' && !event.shiftKey) {
// prevent the textarea from inserting a new line
event.preventDefault();
if (!event.currentTarget.value) return;
customMethods.submitForm();
methods.reset();
}
};
我用维特斯做试车手。