react 项目 测试

  • Enzyme 来自 airbnb 公司,是一个用于 React 的 JavaScript 测试工具,方便你判断、操纵和历遍 React Components 输出。Enzyme 的 API 通过模仿 jQuery 的 API ,使得 DOM 操作和历遍很灵活、直观。Enzyme 兼容所有的主要测试运行器和判断库。

  • jest.spyOn : 当需要测试某些必须被完整执行的方法时,常常需要使用jest.spyOn()

    • 1) 如果是箭头函数的定义

      // react 组件中的一部分代码
      test = () => {}
      

      测试用例中mock此函数 方法如下

      const wrapper = mount(<Component />);
      const spyTest = jest.spyOn(wrapper.instance(), 'test'); // 在mount组件之后
      // mock 完成后可以被调用
       spyTest();
      
      1. 如果是bind 绑定的函数
      test() {}
      

      测试用例中mock此函数 方法如下

       const spyTest = jest.spyOn(Component.prototype, 'test'); //在mount组件之前
       mount(<Component />)
      // mock 完成后可以被调用
       spyTest();
      
  • antd form 设计到必填项时候mock方式如下

    wrapper.find('input#id').simulate('change', {target: {value: 'test'}});
    
原文地址:https://www.cnblogs.com/Running00/p/11584194.html