简体   繁体   English

如果回调未作为道具传递,如何使用 jest 检查对子组件的点击?

[英]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.

相关问题 如何使用Jest将spyOn里面的方法传递给组件? - How to spyOn method inside prop passed down to a component using Jest? 当它作为道具传递时,如何将状态从父组件更新到子组件? - How can I update the state from parent component to child component when its passed as a prop? 如何测试回调单击从父组件传递的子组件? - How test callback click in child component that was passed from parent component? 开玩笑如何从作为道具传递给子组件的父组件调用 function - Jest how to call a function from parent component that is passed as a prop to child component 为什么相同的道具在父组件中是数组而在传递给子组件时不再是数组 - Why the same prop is array in parent component and isn't array anymore if passed to child component 我如何绑定到已通过prop传递给子组件的函数,以便可以在子组件的函数中调用它 - How do I bind to a function that has been passed in to a child component via a prop so that I can call it in a function in the child component 无法加载通过 React App 中的父组件作为道具传递给子组件的图像 - Can't load images passed on to a child component as a prop by parent component in a React App 如何显示作为道具传递给功能组件的数组元素? - How can I display array elements passed as a prop to functional component? 为什么我不能将此道具传递给子组件? - Why can't I pass this prop to the child component? ReactJS-我无法为子组件传递proptrought Route - ReactJS - I can't pass prop trought Route for a child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM