2017-11-28 29 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;