简体   繁体   English

如何使用酶监视反应事件侦听器?

[英]How to spy a react event listener by using enzyme?

It seems to be a fairly simple task, but I can't figure out what I'm doing wrong... 这似乎是一个相当简单的任务,但我无法弄清楚自己在做什么错...

Page.jsx Page.jsx

class Page extends React.Component {
  constructor(props) {
    super(props);
  }

  handlePageClick = (e) => {
    console.log('page click!!');
  }

  render() {
    return(
      <div className='at-page' onClick={this.handlePageClick}>
        {this.props.children}
      </div>
    );
  }
}

export default Page;

Page.test.js Page.test.js

...
it('page should listen for click events', () => {
  const wrapper = mount(<Page><div>Hello Page</div></Page>);
  const instance = wrapper.instance();
  const spy = jest.spyOn(instance, 'handlePageClick');

  const page = wrapper.find('.at-page');
  page.prop('onClick')();

  expect(spy).toHaveBeenCalled();
}
...

I'm getting... 我越来越...

expect(jest.fn()).toHaveBeenCalled() Expected mock function to have been called. expect(jest.fn()).toHaveBeenCalled() Expected mock function to have been called.

I figured out what is wrong here. 我发现这里有什么问题。 Please, let me know if you have a better solution. 请让我知道您是否有更好的解决方案。

1) I should spy my function before I create my wrapper 1)在创建包装器之前,我应该监视我的函数

2) I cannot use arrow functions as methods in my class 2)我不能在课堂上使用箭头函数作为方法

Page.jsx Page.jsx

class Page extends React.Component {
  constructor(props) {
    super(props);

    this.handlePageClick = this.handlePageClick.bind(this);
  }

  handlePageClick(e) {
    console.log('page click!!');
  }

  render() {
    return(
      <div className='at-page' onClick={this.handlePageClick}>
        {this.props.children}
      </div>
    );
  }
}

export default Page;

Page.test.js Page.test.js

...
it('page should listen for click events', () => {
  const spy = jest.spyOn(Page.prototype, 'handlePageClick');
  const wrapper = mount(<Page><div>Hello Page</div></Page>);
  const page = wrapper.find('.at-page');

  page.prop('onClick')();

  expect(spy).toHaveBeenCalled();
}
...

使用Simon库和方法存根

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

相关问题 为什么在React单元测试中的模拟事件中使用sinon.spy模拟函数和React Component的asen.shallow渲染时,这是未定义的? - Why is this is undefined when mocking event in React unit testing, using sinon.spy to mock function and enzyme.shallow render for React Component? 如何窥探(使用 Jest)酶中的子组件方法? - How to spy (using Jest) on the child component method in enzyme? 如何使用 Jest/Enzyme 在 React 中测试 keydown 事件? - How to test keydown event in React with Jest/Enzyme? 如何在 React 中删除事件监听器 - How to remove event listener in React 如何在 React 中添加事件监听器? - How to add event listener in React? 如何使用酶模拟div上的事件来测试反应应用程序? - How to simulate mouse over event on a div using enzyme for testing a react application? 用笑话和酶进行测试。 侦查反应组件的方法 - Testing With Jest and Enzyme. Spy On a method of a react component 如何使用 Jest 和 Enzyme 在 React 中测试 OnSubmit - how to test OnSubmit in React using Jest and Enzyme 如何使用Jest,Enzyme for React-Native模拟单元测试中的事件 - How to simulate an event on a unit test with Jest, Enzyme for React-Native 使用transitionend事件侦听器进行响应以创建转换 - Using transitionend event listener with react to create a transition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM