javascript - React/Enzyme 测试 HTML 标签的值(value)?

标签 javascript html reactjs unit-testing enzyme

我想测试我的标签组件是否正确解析主体对象中的数据。

这是我的标签:

<label id="userInfo"><b>Logged in as: </b>{principal.emailAddress}, <b>Role: </b>{userRole}</label>

这是我的测试,目前不起作用:

describe("Testing User Info displaying correctly", () =>{
  it("when provided principal data, it should correctly combine the username and role", () => {
    expect(wrapper.find('[data-test-id="userInfo"]').text()).to.equal("Logged in as: jdoe@foo.bar, Role: Subscriber");
    });
});

如何检查标签的值(或更准确地说,标签之间的文本)是否正确显示?

当前测试失败并显示以下消息:

"Method “text” is only meant to be run on a single node. 0 found instead."

最佳答案

有了这个reference ,expect语句应该像下面这样:

expect(wrapper.find('[data-test-id="userInfo"]').text().equals("Logged in as: jdoe@foo.bar, Role: Subscriber")).to.equal(true);

关于javascript - React/Enzyme 测试 HTML 标签的值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53387323/

相关文章:

JavaScript 打字机退格效果

javascript - 如果有一定百分比,则更改左侧百分比

javascript - 每次在 jQuery 中单击按钮(子)时,如何删除选定的 <tr>(父)div?

reactjs - Typescript 找不到导入类型 Action

reactjs - 在 React 中测试 mapbox-gl 时如何修复 'window.URL.createObjectURL is not a function'?

javascript - 水平滑出菜单在 Firefox 中不起作用

javascript - 我的 javascript 只适用于第一个 div?

javascript - Masonry:div 布局有问题

html - 设计 css 类名 : Content based vs Function based

javascript - 在 React 中将数据传递给父级