【发布时间】:2018-02-02 15:49:52
【问题描述】:
我想测试当从 React 组件调用方法时,它会触发一个函数作为道具传递给组件。 方法是这样的:
customMethod() {
// Do something
this.props.trackEvent({
category: 'eventCategory',
action: 'eventAction',
label: 'eventAction',
});
// Do something else
}
方法可以通过不同的方式调用,所以我只想做一个通用的测试:如果customMethod被调用,应该用数据触发this.props.trackEvent。
有没有办法使用 jest 和/或酶来触发方法调用?我读过关于做这样的事情:
const wrapper = shallow(<AdPage {...baseProps} />);
wrapper.instance().customMethod();
但它不起作用……任何想法。 我是测试的新手,所以也许我应该对这种测试使用不同的方法?
【问题讨论】:
标签: reactjs testing enzyme jestjs