javascript - How to copypaste in @testing-libraryuser-event 14? - Stack Overflow

In @testing-libraryuser-event 13, it was sort of trivial with:paste(element, text, eventInit, options

In @testing-library/user-event 13, it was sort of trivial with:

paste(element, text, eventInit, options);

However, in 14, copy and cut was added and past now requires some kind of pointer selection which is not clear from the documentation how it should work now. I have tried looking into the library's tests to see how it is used, but tests rely on a private setup that makes it harder to see what's needed for this case in specific.

To the point of my question, how one would port the paste example from @testing-library/user-event 13 into @testing-library/user-event 14?

In @testing-library/user-event 13, it was sort of trivial with:

paste(element, text, eventInit, options);

However, in 14, copy and cut was added and past now requires some kind of pointer selection which is not clear from the documentation how it should work now. I have tried looking into the library's tests to see how it is used, but tests rely on a private setup that makes it harder to see what's needed for this case in specific.

To the point of my question, how one would port the paste example from @testing-library/user-event 13 into @testing-library/user-event 14?

Share Improve this question asked Dec 12, 2022 at 13:46 cyberglotcyberglot 3954 silver badges34 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

It should work if you first click the element, then paste.

Adapting the v13 example:

test('should paste text in input', async() => {
  render(<MyInput />)

  const text = 'Hello, world!'
  const element = screen.getByRole('textbox', {name: /paste your greeting/i})
  await userEvent.click(element)
  await userEvent.paste(text)
  expect(element).toHaveValue(text)
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742330828a4423684.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信