返回
React @testing-library UserEvent.paste用法更新到14版本后不生效
2023-01-03
1853 0把testing-library从13.5.0更新到了14.1.1,发现原来paste的case过不了。
官网上V13的用法和Demo是这样的:
paste(element, text, eventInit, options)
test('should paste text in input', () => {
render(<MyInput />)
const text = 'Hello, world!'
const element = getByRole('textbox', {name: /paste your greeting/i})
userEvent.paste(element, text)
expect(element).toHaveValue(text)
})
但是更新V14包后这样写就有问题,paste方法第一个参数不再传element,而是先focus到目标元素上再进行paste操作。更新后的写法是这样的:
paste(clipboardData?: DataTransfer|string): Promise<void>
it('should paste', async () => {
render(<Screen />)
screen.getByRole('textbox').focus()
await userEvent.paste('1234')
})
这个问题在GitHub上也看到很多人提出了。
您可能感兴趣:
网友点评
提交