简体   繁体   English

如何使用 React 钩子测试功能组件内部的功能?

[英]How to test functions inside functional components with React hooks?

I have a functional component which uses useState hook as shown below:我有一个使用 useState 钩子的功能组件,如下所示:

import React,{useState} from 'react';
    export const PList = (props) =>{
      const [obj, setObj]= useState({
        value: null,
       open: null
      });
      let onDeleteList = (data) =>{
        setObj({....});
      };
      return(
        <React.Fragment>
            { 
                props.arr.map((p,i)=>{
                    return <childA onDeleteList={onDeleteList} />
                })
            }
         </React.Fragment>
      );
    }

I an using Jest+ enzyme and looking to write a test case to test the onDeleteList function in this component.我使用 Jest+ 酶并希望编写一个测试用例来测试此组件中的 onDeleteList function。 I have tried with the below code:我已经尝试使用以下代码:

describe('PList',()=>{
   let wrapper = shallow(<Plist {...props}/>);
   it('should call onDeleteList',()=>{
      expect(wrapper.find('onDeleteList')).toBeTruthy();
   });
 });

I am not sure if this is the correct way, the coverage report still shows the onDeleteList() is not covered in the test case.我不确定这是否是正确的方法,覆盖率报告仍然显示 onDeleteList() 未包含在测试用例中。 Can anyone please advise how can I write a test case for this scenario?谁能告诉我如何为这种情况编写测试用例?

This is how you should be testing:这就是你应该如何测试:

import childA here 

describe('PList',()=>{
   let wrapper = shallow(<Plist {...props}/>);
   it('should call onDeleteList',()=>{
      const onDeleteListHandler=wrapper.find(childA).at(0).prop("onDeleteList");
    onDeleteListhandler(pass your data here);
     expect(onDeleteListhandler).toHaveBeenCalledTimes(1);
    

   });
   
 });

暂无
暂无

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

相关问题 如何通过浅安装 React Functional 组件来对 React 钩子进行单元测试? - How to unit test react hooks by shallow mounting React Functional components? 在 React 的基于类的组件中使用带有钩子的功能组件是否安全? - Is that safe to use functional components with hooks inside class based components in React? 如何在 React 功能组件中模拟 ipcRenderer.on 函数 - How to mock ipcRenderer.on functions inside React functional components React - 功能组件内部的内部功能 - React - Inner functions inside functional components 具有挂钩的功能组件中的React导航头 - React Navigation Header in Functional Components with Hooks 性能 - 功能组件内部的功能 - Performance - Functions inside functional components 如何使用带有钩子的反应功能组件测试句柄 function 调用 - How to test a handle function call with a react functional component with hooks 没有React钩子,没有类组件而不是功能组件,这种React代码的外观如何? - How would this React code look without React-hooks and with class components rather than functional components? 使用 Mocha 进行单元测试,使用 React Hooks + Redux 的功能组件中的酶调度函数 - Unit Testing with Mocha,Enzyme dispatched functions in functional components which uses React Hooks + Redux React Class vs 功能组件:当使用钩子和功能组件时,我的一个功能不断重新渲染 - React Class vs Functional Component: When using hooks and functional components one of my functions constantly re-renders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM