Javascript 使用React、Enzyme、Jest测试嵌套div中是否存在特定链接

Javascript 使用React、Enzyme、Jest测试嵌套div中是否存在特定链接,javascript,reactjs,unit-testing,jestjs,enzyme,Javascript,Reactjs,Unit Testing,Jestjs,Enzyme,我将一些链接呈现到边栏、标题等中。我正在编写一些测试以确保它们存在、呈现等等。一切正常 我想确保链接以正确的div和正确的位置链接呈现 示例标头组件 我的最终目标是说。。nav item hdr中存在一个链接,其中该链接的标题为X,链接href为Y 我有一些小片段,比如: expect(wrapper.find('.sb').children()).toHaveLength(3) 这计算了.sb类中的儿童数量,但希望深入研究 您应该尝试以下方法: let headerLinkProp = [

我将一些链接呈现到边栏、标题等中。我正在编写一些测试以确保它们存在、呈现等等。一切正常

我想确保链接以正确的div和正确的位置链接呈现

示例标头组件 我的最终目标是说。。
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);
});

亲爱的,这就是我要找的。谢谢
expect(wrapper.find('.sb').children()).toHaveLength(3)
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);
});