简体   繁体   English

在 textarea 输入上的 Jest 测试写入

[英]Jest test write on textarea input

I am trying to write some mock string to a textarea using Jest and then press Enter but it does not work for whatever reason.我正在尝试使用 Jest 将一些模拟字符串写入文本区域,然后按 Enter,但无论出于何种原因它都不起作用。 My code so far:到目前为止我的代码:

test('Should add a message', () => {
  const element = wrapper.find('textarea');
  element.instance().value = 'abc';
  element.simulate('keypress', { key: 'Enter' });

  const newWrapper = wrapper.find('user');
  expect(newWrapper.length).toBe(1);
});

My component:我的组件:

<textarea
  onKeyUp={sendMessage}
  placeholder='Type your message here and press enter to send...'
  cols='30'
  rows='5'
></textarea>

Just to be clear the textarea is definitely there as the following test passes:只是为了清楚 textarea 在以下测试通过时肯定存在:

test('Should have a textarea', () => {
  const element = wrapper.find('textarea');
  expect(element.length).toBe(1);
});

In your case, what you are looking for is 'keyUp' , not 'keypress' .在您的情况下,您正在寻找的是'keyUp' ,而不是'keypress' And instead of key , use keyCode .而不是key ,使用keyCode The keyCode for Enter is 13 . EnterkeyCode13

Like this:像这样:

element.simulate('keyUp', { keyCode: 13 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM