我将一些链接渲染到侧边栏、标题等。我正在编写一些测试以确保它们存在、被渲染等等。一切正常。
我想确保链接在具有正确位置链接的正确 div 中呈现。
示例 header 组件
<div className="pull-right" id="navbarsExampleDefault">
<ul className="navbar-nav mr-auto">
{props.links.map((link, i) => {
return (
<li key={`HEADER_${i}`} className="nav-item hdr">
<NavLink exact className='nav-link' activeClassName='nav-link active' to={`${link.location}`}>
{link.title}
</NavLink>
</li>
)
})}
</ul>
</div>
此对象作为 Prop 传入以进行迭代:
let headerLinkProp = [
{ title: "Dashboard", location: "/"},
{ title: "Budget", location: "/editBudget"},
{ title: "Virtual Accounts", location: "/viewAccounts"}]
我的最终目标是最终说.. nav-item hdr
中存在一个链接,其中该链接的标题为 X
,链接 href 为 Y
。
我有一些小片段,比如:
expect(wrapper.find('.sb').children()).toHaveLength(3)
计算 .sb
类中子级的数量,但希望能更深入地研究。
最佳答案
你应该尝试这样的事情:
let headerLinkProp = [
{ title: "Dashboard", location: "/"},
{ title: "Budget", location: "/editBudget"},
{ title: "Virtual Accounts", location: "/viewAccounts"}]
expect(wrapper.find('.sb').children()).toHaveLength(3);
wrapper.find('.nav-link').forEach((node, index) => {
expect(node.text()).to.equal(headerLinkProp[index].title);
expect(node.prop('to')).to.equal(headerLinkProp[index].location);
});
关于javascript - 使用 React、Enzyme、Jest 测试嵌套 div 中是否存在特定链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48193981/