如何测试反应组件中是否触发了滚动?

时间:2018-02-03 12:32:48

标签: reactjs enzyme jest

我试图编写一个测试来测试滚动事件是否会在给出shouldScroll道具时触发,有人可以帮助我吗?这是我试图测试的代码:

export default class HelloWorld extends React.Component {
  componentDidMount() {
    if (this.props.shouldScroll) {
      window.addEventListener('scroll', this.test);
    }
  }

  test() {
    console.log('scrolled...');
  }

  render() {
    return (
      <div>
        hello world
      </div>
    );
  }
}

测试:

it('should fire onScroll event', () => {
  // how do I test above???
});

1 个答案:

答案 0 :(得分:0)

您需要test(event)才能使用它。此外,您需要一个足够长的页面以使用滚动条,以便触发滚动事件。

您可以多次添加hello world</br>,以便该页面足够长,可以使用滚动条。

  test(event) {
   console.log('scrolled...');
  }
相关问题