有没有办法开玩笑地解构传递给函数 onClick 的数据属性?
Is there a way to destructure data-attributes passed to a function onClick in jest?
handleClick() 是我要测试的函数:
this.state = {
expanded: {},
}
handleClick = (e) => {
const { dataset } = e.target
this.setState((prevState) => ({
expanded: {
...prevState.expanded,
[dataset.id]: !prevState.expanded[dataset.id],
},
}))}
handleClick 函数和 dataId 作为 prop 传递给 Icon 子组件:
<Icon
icon={
this.state.expanded[this.props.id] === false ? "plus" : "minus"
}
size="sm"
dataId={this.props.id}
onClick={this.handleClick}
/>
按下图标时调用 handleClick() 函数:
const Icon = props => {
const { icon, size, dataId = null, onClick = null } = props
return (
<i
className={`fa fa-${icon} fa-${size}`}
data-id={dataId}
onClick={onClick}
/>
)
}
这是我的 handleClick() 测试用例:
it("should update state when handleClick is invoked", () => {
const mockExpanded = {}
mockExpanded[initialProps.id] = false
wrapper.setState({ expanded: mockExpanded })
const mockEvent = {
target: wrapper.find("Icon").dive().find("i").debug(), //need to pass target value as
an object
}
wrapper.instance().handleClick(mockEvent)
expect(wrapper.state().expanded[initialProps.id]).toBe(true) })
即使在将 mockEvent 作为目标对象传递给 handleClick(e) 之后,测试用例似乎也无法解构 data-id 属性:
TypeError: Cannot read property 'id' of undefined
27 | expanded: {
28 | ...prevState.expanded,
> 29 | [dataset.id]: !prevState.expanded[dataset.id],
| ^
30 | },
31 | }))
32 | }
请建议一种在玩笑测试用例中解构数据集的方法或测试 handleClick(e) 方法的正确方法。
it("should update state when handleClick is invoked", () => {
const mockExpanded = {}
mockExpanded[initialProps.id] = false
wrapper.setState({ expanded: mockExpanded })
const mockEvent = {
target: { dataset: { id: initialProps.id } },
}
wrapper.instance().handleClick(mockEvent)
expect(wrapper.state().expanded[initialProps.id]).toBe(true)
})
handleClick() 是我要测试的函数:
this.state = {
expanded: {},
}
handleClick = (e) => {
const { dataset } = e.target
this.setState((prevState) => ({
expanded: {
...prevState.expanded,
[dataset.id]: !prevState.expanded[dataset.id],
},
}))}
handleClick 函数和 dataId 作为 prop 传递给 Icon 子组件:
<Icon
icon={
this.state.expanded[this.props.id] === false ? "plus" : "minus"
}
size="sm"
dataId={this.props.id}
onClick={this.handleClick}
/>
按下图标时调用 handleClick() 函数:
const Icon = props => {
const { icon, size, dataId = null, onClick = null } = props
return (
<i
className={`fa fa-${icon} fa-${size}`}
data-id={dataId}
onClick={onClick}
/>
)
}
这是我的 handleClick() 测试用例:
it("should update state when handleClick is invoked", () => {
const mockExpanded = {}
mockExpanded[initialProps.id] = false
wrapper.setState({ expanded: mockExpanded })
const mockEvent = {
target: wrapper.find("Icon").dive().find("i").debug(), //need to pass target value as
an object
}
wrapper.instance().handleClick(mockEvent)
expect(wrapper.state().expanded[initialProps.id]).toBe(true) })
即使在将 mockEvent 作为目标对象传递给 handleClick(e) 之后,测试用例似乎也无法解构 data-id 属性:
TypeError: Cannot read property 'id' of undefined
27 | expanded: {
28 | ...prevState.expanded,
> 29 | [dataset.id]: !prevState.expanded[dataset.id],
| ^
30 | },
31 | }))
32 | }
请建议一种在玩笑测试用例中解构数据集的方法或测试 handleClick(e) 方法的正确方法。
it("should update state when handleClick is invoked", () => {
const mockExpanded = {}
mockExpanded[initialProps.id] = false
wrapper.setState({ expanded: mockExpanded })
const mockEvent = {
target: { dataset: { id: initialProps.id } },
}
wrapper.instance().handleClick(mockEvent)
expect(wrapper.state().expanded[initialProps.id]).toBe(true)
})