Reactjs 酶试验/键控(移位)反应用于立即停止

Reactjs 酶试验/键控(移位)反应用于立即停止,reactjs,unit-testing,enzyme,onkeypress,Reactjs,Unit Testing,Enzyme,Onkeypress,我试图在按下按键事件移位时测试StopImmediateProporation enter// wrapper.simulate('keypress', { key: 'Shift' }); wrapper.simulate('keydown', { keyCode: 16 }); const stopPropogation = jest.fn(); expect(stopPropogation).toHaveBeenCalledTimes(0); wrapper.find('.classNa

我试图在按下按键事件移位时测试StopImmediateProporation

enter// wrapper.simulate('keypress', { key: 'Shift' });
wrapper.simulate('keydown', { keyCode: 16 });
const stopPropogation = jest.fn();
expect(stopPropogation).toHaveBeenCalledTimes(0);

wrapper.find('.className').simulate('keyDown', { key: 'Shift' });


// const event = new KeyboardEvent('keydown', { keyCode: 16 });
// // const dispatchStopProp = jest.fn();
// document.dispatchEvent(event);
// // expect(dispatchStopProp).toHaveBeenCalledTimes(1);
// const result = wrapper.dive().instance().stopPropogation();
// expect(result).toEqual(1);

// const onKeyDown = sinon.spy();
// const wrapper = mount(<TestComponent onkeydown={onkeydown}/>);
// const input = wrapper.find('input');
// input.simulate('keyDown', { keyCode: 16 });
// expect(onKeyDown.called).to.be.true; code here
我的方法似乎都不管用。任何建议都会有帮助

单元测试动机:


当在应用程序页面上单击键盘shift键时,将调用StopperPogation。

如果您试图在应用程序中跟踪StopperPogation,则不能仅声明具有相同名称的jest.fn并跟踪它

你需要监视这个函数,我喜欢使用SinonJS

例如:

var spy = sinon.spy(MyComponent.prototype, 'stopPropagation');
// Do something that invokes MyComponent.stopPropagation

expect(spy.calledOnce).to.be.true;