如何测试是否使用jest调用blur()
?
这是我的功能:
blurOnEnter (event) {
if (event.keyCode === 13 && !event.shiftKey) event.target.blur()
}
这就是我测试它的方式,但我不知道如何处理我的期望陈述:
it('blurOnEnter() should blur target input field', () => {
const blur = jest.fn()
wrapper = shallow(<Component />)
wrapper.instance().blurOnEnter({
keyCode: 13,
target: { blur }
})
expect(blur.calls.length).toBe(1) // TypeError: Cannot read property 'length' of undefined
})
为什么我会收到TypeError Cannot read property 'length' of undefined
答案 0 :(得分:0)
怎么样:
it('blurOnEnter() should blur target input field', () => {
const blur = jest.fn()
wrapper = shallow(<Component />)
wrapper.instance().blurOnEnter({
keyCode: 13,
target: { "blur": blur } // see here!
})
expect(blur.mock.calls.length).toBe(1) // and here
})
根据(http://facebook.github.io/jest/docs/en/mock-functions.html#content)的jest模拟文档,jest模拟具有mock属性,而原始代码没有使用它。