我是使用Jest进行测试的新手,需要模拟click事件。我已经能够使用酶css选择器选择元素并触发事件,但是测试仍然失败,表明我尚未运行该功能。
Test:
it('test',() => {
const e = { target: {id:'X'} }
const click = jest.fn();
let wrapper = shallow(<GameBoard onClick={click}/>);
let test = wrapper.find("#gameBoard").childAt(0)
test.simulate('click',e)
expect(click).toHaveBeenCalledTimes(1)
})
Component :
import React, {Component} from 'react'
class GameBoard extends Component {
handleClick = (e) => {
this.props.turn
if (e.target.innerText === ''){
this.props.addToTotalMoves()
e.target.innerText = this.props.turn
this.props.updateActiveBoardArray(e.target.id)
this.props.changeTurn()
}
}
render(){
return (
<div id ='gameBoard'>
<div
id = '0'
className = 'boardTile'
onClick = {this.handleClick}
></div>
答案 0 :(得分:1)
查看GameBoard
的{{1}}的来源,我们可以看到它没有调用handleClick
。
相反,它会调用
this.props.onClick()
因此,您必须在测试中通过那些 this.props.updateActiveBoardArray(e.target.id)
this.props.changeTurn()
并检查它们是否被称为:
props