简体   繁体   English

笑话和酵素| 在componentDidMount中测试PropTypes函数

[英]Jest & Enzyme | Test PropTypes Function in componentDidMount

I have this code in my component, that takes listUsers, as a required PropType func. 我的组件中有此代码,该代码将listUsers作为必需的PropType函数。 I want to test that in the componentDidMount() , it should be called only once. 我想测试在componentDidMount() ,它应该只被调用一次。

Component Code: 组件代码:

   static propTypes = {
    securityMode: PropTypes.string.isRequired,
    listUsers: PropTypes.func.isRequired
  };

  componentDidMount() {
    const { listUsers } = this.props;
    listUsers();
  }

    onCreateUserSucess= response => {
    this.setState({ isCreateUserModalOpen: false });
    const { listUsers, notify } = this.props;
    listUsers();
    this.closeCreateUserModal();
    notify({
      title: 'Created User',
      message: `User ${response.username} was added to group: ${response.group}`,
      status: STATUS.success,
      dismissible: true,
      dismissAfter: 3000
    });
  };

I get an error, saying that spyOn can only be appliead to functions. 我收到一个错误消息,说spyOn仅适用于函数。 Can someone help me to test the. 有人可以帮我测试一下。 componentDidMount and onCreateUserSucess . componentDidMountonCreateUserSucess I even tried to mock the functions but I always get failings. 我什至尝试模拟功能,但总是失败。

Testing componentDidMount is pretty simple. 测试componentDidMount非常简单。 Let's suppose that the component you created above is called UsersDisplayer . 假设您在上面创建的组件称为UsersDisplayer

class UsersDisplayer extends Component {
   constructor(props) {
      // ...
   }

   // The code above goes here.
}

In order to test whether the listUsers function runs or not, you should do something similar to this: 为了测试listUsers函数是否运行,您应该执行以下操作:

// Import React, shallow and UsersDisplayer at the top.

describe('<UsersDisplayer />', () => {
   let usersDisplayerWrapper;
   let listUsers;

   beforeEach(() => {
      listUsers = jest.fn();

      usersDisplayerWrapper = <UsersDisplayer listUsers={listUsers} />;
   });

   describe('componentDidMount', () => {
      it('calls listUsers props function', () => {
          // The `componentDidMount` lifecycle method is called during the rendering of the component within the `beforeEach` block, that runs before each test suites.
         expect(listUsers).toHaveBeenCalled();
      });
   });

   describe('onCreateUserSucess', () => {
      it('calls listUsers props function', () => {
         // Create a dummy `response` data that will be passed to the function.
         const response = {
            username: 'username',
            group: 'group'
         };

         // Simply just call the function of the instance.
         usersDisplayerWrapper.instance().onCreateUserSucess(response);

         expect(listUsers).toHaveBeenCalled();
      });
   });
});

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

相关问题 用玩笑+酶+ react-redux测试propType的最佳实践? - Best practice to test propTypes with jest + enzyme + react-redux? 在 ReactJS 中使用 jest 和酶测试回调函数? - Test a callback Function using jest and enzyme in ReactJS? 开玩笑/酶测试:this.props.showOverlay不是函数 - jest/enzyme test: this.props.showOverlay is not a function Jest/Enzyme Test Function Call in Callback - Jest/Enzyme Test Function Call in Callback 笑话/酶| 测试遮罩/取消遮罩密码功能 - Jest/Enzyme | Test a mask/unmask Password function 测试使用 Jest 和 Enzyme 更改状态的异步 componentDidMount - Testing asynchronous componentDidMount that changes state with Jest and Enzyme 如何为 componentDidMount() 编写单元测试用例并导出默认 connect(null, updateProps)(<componentname> ) 在 React 中使用 JEST/酶?</componentname> - How to write unit test case for componentDidMount() and export default connect(null, updateProps)(<ComponentName>) with JEST/Enzyme in React? 酶“containsMatchingElement”和测试代码中缺少PropTypes - enzyme “containsMatchingElement” and missing PropTypes in test code 在componentDidMount中调用React-Native Jest测试函数 - React-Native Jest test function is called in componentDidMount Jest/Enzyme Test 在 React 功能组件中将 Child prop 触发为 function - Jest/Enzyme Test firing Child prop as function in react functional component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM