[英]How can I check a click on a child component using jest, if the callback isn't passed as prop?
I have the following component:我有以下组件:
<MaxFrContainer data-test="wellness-report-card">
<PrimaryBlueButton
onClick={() => {
// open popup
}}
>
generate
</PrimaryBlueButton>
</MaxFrContainer>
Jest complains that the onClick
call isn't tested, but how do I test it? Jest 抱怨
onClick
调用没有经过测试,但我该如何测试呢? Whatever he callback would be, it is defined within the component and not passed down as a prop.无论他的回调是什么,它都是在组件中定义的,而不是作为道具传递的。 Also, this similar thing repeats in my code.
此外,类似的事情在我的代码中重复出现。 Sometimes it opens a popup, sometimes it makes an API call.
有时它会打开一个弹出窗口,有时它会进行 API 调用。
How do I get these tested我如何进行这些测试
Can you be more specified what do you do in //open popup
?你能更具体地说明你在
//open popup
做什么吗?
If I would create such component there are should be separate module with business logic, and in onClick handler I should just call some method from this module.如果我要创建这样的组件,应该有单独的具有业务逻辑的模块,并且在 onClick 处理程序中,我应该只从这个模块调用一些方法。
<MaxFrContainer data-test="wellness-report-card">
<PrimaryBlueButton
onClick={() => MaxFrContainerController.clickHandler()}
>
generate
</PrimaryBlueButton>
</MaxFrContainer>
And to test onClick you should only check that clickHandler
of mock MaxFrContainerController
was called.而测试的onClick应该只检查
clickHandler
模拟的MaxFrContainerController
被调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.