简体   繁体   English

如何在使用jest和react-testing-library进行测试时设置组件的本地状态?

[英]How to set component's local state while testing using jest and react-testing-library?

Using AirBnB's enzyme, we can setState of a component: 使用AirBnB的酶,我们可以设置组件的状态:

const loginComponent = shallow(<Login />);
loginComponent.setState({ error: true });

I want to do same thing using react-testing-library. 我想使用react-testing-library做同样的事情。

Thanks! 谢谢!

You can't do that with react-testing-library. 你不能用react-testing-library做到这一点。 This is because RTL wants you to test your component as a user would. 这是因为RTL希望您以用户的身份测试组件。

What does this mean? 这是什么意思? In real life, your component will change the state after something happens. 在现实生活中,你的组件会在事情发生后改变状态。 Maybe the user entered wrong data or the API returned an error code. 也许用户输入了错误的数据或API返回了错误代码。

Rather than changing the state directly, you should try to reproduce the set of actions that change the state. 您应该尝试重现更改状态的操作集,而不是直接更改状态。

This approach is a bit harder to implement than what Enzyme offers but your tests will be more robust. 这种方法比Enzyme提供的方法更难实现,但您的测试将更加强大。 That's because you're going to test the whole flow instead of just focusing on what gets rendered when a particular state occurs. 那是因为你要测试整个流程,而不是只关注特定状态发生时渲染的内容。

On top of that say you refactor your code and change how the state works. 最重要的是,你要重构代码并改变状态的工作方式。 RTL tests won't care as long as the way users interact with your application is the same. 只要用户与您的应用程序交互的方式相同,RTL测试就不会关心。 An Enzyme test would fail though because it doesn't know how to interact with the internals of your component anymore. 酶测试会失败,因为它不知道如何与组件的内部相互作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 react-testing-library 和 jest 测试 function 组件中的 state - How to test state within function component with react-testing-library and jest 使用 react-testing-library 和 jest 测试是否调用了 prop 函数 - Testing if a prop function was called using react-testing-library and jest 如何在Jest和react-testing-library中使用react-hook - How to use react-hooks with Jest and react-testing-library 如何使用 jest、react-testing-library 在 React 中模拟元素上的点击事件 - How to mock a click event on an element in React using jest , react-testing-library 如何使用 React、Jest 和 React-testing-library 为带有令牌的 api 调用编写单元测试? - How can I write unit test for api call with token using React, Jest and React-testing-library? 使用 Redux 和 react-testing-library 测试 React 组件 - Test React Component using Redux and react-testing-library 使用 test 和 react-testing-library 测试反应组件 - Test a react component using test and react-testing-library 如何使用 react-testing-library 和 Jest 按值查询 textarea 元素? - How to query textarea elements by value using react-testing-library and Jest? 使用 Jest + react-testing-library 测试异步 `componentDidMount()` - Testing async `componentDidMount()` with Jest + react-testing-library 如何使用 react-testing-library 测试材质 ui MenuList 组件上的按键按下事件 - How to test key down event on material ui MenuList component using react-testing-library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM