使用反应测试库检查组件的状态 [英] check state of a component using react testing library

查看:47
本文介绍了使用反应测试库检查组件的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have triggered a change in component using react testing library and I want to check the corresponding change in the state of the component. Is it possible to check the state of the component

推荐答案

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

Probably a No-Go as this is a direct violation of the principles of React-Testing-Library

简介

问题

您想为 React 组件编写可维护的测试.作为一个作为这个目标的一部分,你希望你的测试避免包括组件的实现细节,而是专注于制作您的测试让您充满信心.作为其中的一部分,您希望您的测试库长期可维护运行如此重构您的组件(更改实现但不功能)不要破坏您的测试并减慢您和您的团队的速度.

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.

这个解决方案

您的测试与您的软件使用方式越相似,他们可以给你信心.

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

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

Basically, it doesn't concern itself with the internal implementation details of a component, like state, but rather its API, i.e. the props and how a user can interact with it. It's more like black-box testing.

这篇关于使用反应测试库检查组件的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆