[React Testing] Improve Test Confidence with the User Event Module

Posted answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[React Testing] Improve Test Confidence with the User Event Module相关的知识,希望对你有一定的参考价值。

The User Event module is part of the Testing Library family of tools and lets you fire events on DOM nodes that more closely resemble the way your users will interact with your elements. Let’s refactor our fire event usages to use that instead.

 

import user from @testing-library/user-event

test(v2: entering an invalid value shows an error message, () => {
  const { getByLabelText, getByRole } = render(<FavoriteNumber />)
  const input = getByLabelText(/favorite number/i)
  // simulate a user type on input
  user.type(input, ‘10‘)
  expect(getByRole(alert)).toHaveTextContent(/the number is invalid/i)
})

 

以上是关于[React Testing] Improve Test Confidence with the User Event Module的主要内容,如果未能解决你的问题,请参考以下文章

[React] Improve developer experience for accessing context with a custom React hook

无法使用 react-testing-library 运行测试“无法在模块外使用 import 语句”错误

[React Testing] Error State with React Testing Library, findBy*

react-testing-library - 屏幕与渲染查询

[React Testing] Test your Custom Hook Module with react-hooks-testing-library

使用 `react-testing-library` 和 `cypress` 有啥区别?