【发布时间】:2020-08-12 01:04:58
【问题描述】:
我正在尝试测试一个菜单栏,它使用history.push 处理到某些页面的路由。我正在使用 Primereact Menubar 组件。
class MenubarComponent extends React.Component {
constructor() {
super();
this.state = {
items: [
{
label: "Home",
icon: "pi pi-home",
command: () => (this.props.history.push("/"))
},
{
label: "About",
icon: "pi pi-info",
command: () => (this.props.history.push("/about"))
}
]
}
}
render() {
return (
<Menubar model={this.state.items}/>
)
}
}
export default withRouter(MenubarComponent)
如何验证当我点击菜单栏按钮时,它会将我带到正确的页面?
describe('MenubarComponent', () => {
it('should navigate on menuitem click', () => {
const menubarItemsMock = jest.fn();
const item = {
label: "Home",
icon: "pi pi-home",
command: () => (this.props.history.push("/"))
}
const wrapper = shallow(<MenubarComponent/>)
//??
})
})
【问题讨论】:
-
您需要在 menuBar 中捕获具有 onClick attr` 作为道具的元素并触发它,然后检查 MenubarComponent.props.history 是否具有您期望的字符串
-
我不太确定该怎么做。
标签: reactjs unit-testing jestjs react-router-dom primereact