在带参考的测试中无法读取null的属性'addEventListener'

时间:2019-08-28 19:46:02

标签: reactjs jestjs enzyme

我在div中添加了一个引用,并且正在向componentDidMount()中的引用添加一个事件侦听器。我收到错误

  

在我的酶测试中无法读取“ null的'addEventListener'属性”。

我知道,如果我在添加事件侦听器之前检查该元素可能会起作用,但是我想知道是否可以向测试添加一些更“正确”的东西

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.element = React.createRef();
  }

  componentDidMount() {
    this.element.current.addEventListener('rowClicked', this.onRowClicked);
  }

  render() {
    return (
        <div ref={element => {this.element = element}>
        test div
      </div>
    )
  }
}

这是我的考试:

test("should render", () => {
  const wrapper = shallow(<MyComponent store={store} />).dive();
  const content = wrapper.find(".content");
  expect(content).toMatchSnapshot();
});

0 个答案:

没有答案