使用 React、Enzyme、Jest 测试嵌套 div 中是否存在特定 link
Testing that a specific link exists within nested divs using React, Enzyme, Jest
我将一些 link 渲染到边栏中,header 等。我正在编写一些测试以确保它们存在、被渲染等等。一切正常。
我想确保 link 在正确的 div 中以正确的位置呈现 link。
示例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>
这个 object 正在作为道具传入以迭代:
let headerLinkProp = [
{ title: "Dashboard", location: "/"},
{ title: "Budget", location: "/editBudget"},
{ title: "Virtual Accounts", location: "/viewAccounts"}]
我的最终目标是最终说.. 在 nav-item hdr
中存在一个 link,其中 link 的标题为 X
和一个 link Y
的 href。
我有像这样的小片段:
expect(wrapper.find('.sb').children()).toHaveLength(3)
即算children内的人数.sb
class,但希望能dive更深。
你应该尝试这样的事情:
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);
});
我将一些 link 渲染到边栏中,header 等。我正在编写一些测试以确保它们存在、被渲染等等。一切正常。
我想确保 link 在正确的 div 中以正确的位置呈现 link。
示例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>
这个 object 正在作为道具传入以迭代:
let headerLinkProp = [
{ title: "Dashboard", location: "/"},
{ title: "Budget", location: "/editBudget"},
{ title: "Virtual Accounts", location: "/viewAccounts"}]
我的最终目标是最终说.. 在 nav-item hdr
中存在一个 link,其中 link 的标题为 X
和一个 link Y
的 href。
我有像这样的小片段:
expect(wrapper.find('.sb').children()).toHaveLength(3)
即算children内的人数.sb
class,但希望能dive更深。
你应该尝试这样的事情:
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);
});