gpt4 book ai didi

javascript - enzyme shallow() 和 addEventListener

转载 作者:行者123 更新时间:2023-11-30 19:59:21 26 4
gpt4 key购买 nike

我有这个组件,它在应用程序中运行良好:

class TheComponent extends Component {

componentDidMount() {
this.watchForClicks();
}

watchForClicks() {
this.elementRef.addEventListener('click', () => {
console.log('there went one!');
});
}

render() {
return (
<div
ref={theElement => {
this.elementRef = theElement;
}}
>
...
</div>
);
}
}

export default TheComponent;

这个测试:

describe('<TheComponent />', () => {
context('do a test:', function() {
it.only('fails!', () => {
wrapper = shallow(<TheElement />)
})
});
});

为什么会出现此错误?

undefined is not an object (evaluating 'this.elementRef.addEventListener')

最佳答案

如你所见in the docs , ShallowWrapper API 没有 ref() 方法,但您可以使用 mount()相反,哪个 does have the ref() method .

关于javascript - enzyme shallow() 和 addEventListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53565389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com