代码之家  ›  专栏  ›  技术社区  ›  Augusto Oliveira

Shift修饰符不适用于反应测试

  •  0
  • Augusto Oliveira  · 技术社区  · 3 年前

    我有一个文本区域组件,假设在不使用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();
      }
    };
    

    我用维特斯做试车手。

    0 回复  |  直到 3 年前
    推荐文章