2017-11-28 17 views
0

KeyDownイベント - Shiftが押されたときにstopImmediatePropogation()をテストしようとしています。酵素テスト/ stopImmediatePropogationのためのkeyDown(shift)のための反応

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 

いずれの方法も機能していないようです。どんな提案も参考になります。ユニットテストに

動機:キーボード「シフト」キーがアプリページ上でクリックされたときに

stopPropogation()が呼び出されます。

答えて

1

stopPropagationをトラッキングしようとしている場合は、同じ名前のjest.fnを宣言して追跡するだけではありません。

あなたは機能をスパイする必要があります。私はSinonJSを使いたいです。

例:

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

expect(spy.calledOnce).to.be.true; 
関連する問題