【发布时间】:2019-02-19 06:25:57
【问题描述】:
使用 react 16.3.1、jest 16.3.1、酶 3.3.0。
在我的 React Class 组件中,我创建了一个 react ref,我使用它来确保在安装组件时浏览器位于页面顶部。
class PageView extends React.Component {
constructor(props) {
super(props);
this.topOfPageRef = React.createRef();
}
componentDidMount() {
ReactDOM.findDOMNode(this.topOfPageRef.current).scrollIntoView();
}
render(){
const { x } = this.props;
return (
<React.Fragment>
<div className="main-wrapper" ref={this.topOfPageRef}>
Top
</div>
)}
</React.Fragment>
);
}
}
这一切都在浏览器中完美运行,但在我的酶测试中失败。
我的测试很简单,它只是尝试渲染组件。
it('should render component correctly', () => {
const props = {
...defaultProps,
};
const wrapper = mount(<PageView {...props} />);
expect(wrapper).toMatchSnapshot();
});
TypeError: Cannot read property 'scrollIntoView' of null
我已经尝试了 shallow 和 mount 方法,虽然找到的元素不为空,但它似乎是 HTMLDivElement 的一个反应实例,它缺少 scrollIntoView 方法。
【问题讨论】:
标签: javascript reactjs testing jestjs enzyme