javascript - 使用 react 测试库检查组件的状态

标签 javascript reactjs react-testing-library

我已经使用 react 测试库触发了组件的更改,我想检查组件状态的相应更改。
是否可以检查组件的状态

最佳答案

可能是不可行的,因为这直接违反了 React-Testing-Library 的原则

Intro

The problem

You want to write maintainable tests for your React components. As a part of this goal, you want your tests to avoid including implementation details of your components and rather focus on making your tests give you the confidence for which they are intended. As part of this, you want your testbase to be maintainable in the long run so refactors of your components (changes to implementation but not functionality) don't break your tests and slow you and your team down.

This solution

The more your tests resemble the way your software is used, the more confidence they can give you.



基本上,它不关心组件的内部实现细节,例如 state ,而是它的 API,即 Prop 以及用户如何与之交互。它更像是黑盒测试。

关于javascript - 使用 react 测试库检查组件的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61813319/

相关文章:

javascript - 如何在 Node.js 脚本中获取有关 stdout 重定向文件名的信息

javascript - jquery 返回未定义的错误

javascript - bootstrap 的不稳定问题 -> 仅在大屏幕上行宽度不一致

javascript - Grails 下 AngularJS 应用程序中部分 html 的正确路径是什么

jquery - 如何创建具有不同图像高度和宽度的图像网格 slider (砌体 slider )

reactjs - expect(...).toHaveAttribute 不是函数 - 为什么?

css - 如何在 ReactJs 的显示/隐藏中将单词更改为图标

javascript - 尝试将数据从子级传递到父级时出现 setState 问题

reactjs - 如果在 useEffect 之后没有发生任何事情,则进行 react 测试

jestjs - 在 React-Testing-Library 中使用自定义/pages/_app.js 测试 Next.js 应用程序