酶模拟给定位置上的onClick事件

问题描述

我正在编写一个测试用例,以模拟用户单击给定插入符号位置上的Textarea。

我试图使用selectionStart和selectionEnd来模拟带有插入符号位置的'click'事件:

it('onClick',() => {
  const event = { target: { selectionStart: 3,selectionEnd: 3 } };
  wrapper.find('textarea').simulate('click',event);
})

这不起作用,我认为这是通过onCLick事件在元素内设置光标位置的错误方法

关于如何使用textArea ..的光标位置模拟单击事件的任何建议?

解决方法

您可以在textarea的dom元素中更改selectionStartselectionEnd

it('onClick',() => {
  const wrapper = mount(<MyComp></MyComp>) // <-- Note that shallow doesn't work
  const textareaWrapper = component3.find('textarea') // find textarea
  const textarea = textareaWrapper.getDOMNode() // get textarea's dom element
  textarea.selectionStart = 3 // set cursor position start
  textarea.selectionEnd = 3 // set cursor position end
  textareaWrapper.simulate('click') // simulate click
  ...
})

Ps:您应该mount您的元素才能访问dom元素。