最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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)
})
发布评论

评论列表(0)

  1. 暂无评论