【发布时间】:2019-04-01 06:05:16
【问题描述】:
我正在尝试使用 Enzyme + Sinon 间谍测试 React 组件。我的反应代码在我的组件实例中使用属性初始化器语法:
class FixedButton extends React.Component {
handleMouseOver = (e) => {
const { fixedButtonHover = _fixedButtonHover } = this.props;
const { backgroundColor, color } = fixedButtonHover;
e.target.style.backgroundColor = backgroundColor;
e.target.style.color = color;
}
}
我正试图弄清楚如何监视 handleMouseOver 函数,尽管据我了解,由于上下文绑定到实例而不是原型上的属性,所以我无法监视它。
我知道该函数被调用,并且我知道我可以通过将我的语法修改为标准属性样式来使间谍正常工作。我也知道组件的挂载实例具有方法作为属性。
这是两者不能一起工作的情况之一,还是有我没看到的技巧?
编辑:这是我尝试建议解决方案的方式,但 spy.callOnce 与 spy.call 一样返回 false:
test('FixedButton component methods are called as expected', t => {
const wrapper = mount(<FixedButton />);
const instance = wrapper.instance();
const spy = sinon.spy(instance, 'handleMouseOver');
const button = wrapper.find('button');
button.simulate('mouseover');
t.is(spy.calledOnce, true);
})
【问题讨论】:
标签: javascript reactjs sinon enzyme