reactjs - 为什么文本在反应中不匹配?
问题描述
我正在尝试写test case
. functional component
我只是写如果有必填字段会显示。但是required text
我的测试没有通过为什么
it("get text of required field", () => {
wrapper.setProps({
error: {
olmIdError: true
}
});
console.log(wrapper.find(FormHelperText).text());
expect(wrapper.find(FormHelperText).text()).toEqual("Required..!!");
});
这是我的代码
https://codesandbox.io/s/43k6lw60x
零件
<Input
error={error.olmIdError || apiError}
id="olm-id"
type="text"
value={olmId}
classes={{
root: classes.inputRoot,
focused: classes.focusedLabel,
underline: classes.underlineInput
}}
placeholder="Enter OLM ID"
onChange={handleChange("olmId")}
/>
{error.olmIdError ? (
<FormHelperText error={error.olmIdError} id="weight-helper-text">
{MESSAGES.REQUIRED}
</FormHelperText>
) : null}
出错
Expected value to equal:
"Required..!!"
Received:
"<WithStyles(WithFormControlContext(FormHelperText)) />"
44 | }
解决方案
如果您将断言更改为,则测试通过
expect(wrapper.find(FormHelperText).children().text()).toEqual("Required..!!");
.text()返回当前树的渲染文本。输出有点奇怪,因为当前树是FormHelperText
浅渲染的。调用.children()
first 导致树只是Required..!!
文本。
推荐阅读
- xml - 如何将 xml 作为请求正文传递并在 amazone api 网关中获取 xml 作为响应正文以进行 post 方法
- python - 如何从列中删除“月”并将剩余值除以 12?
- c# - 在 Windows 窗体中显示 MySqlConnection 的状态
- javascript - WebExtensions API - 创建一个没有标题栏标题和按钮的“弹出窗口”
- elasticsearch - 在两个索引上搜索和聚合
- python - Python函数更新未作为参数传递的变量
- apache-camel - 如何使用骆驼文件组件查看隐藏文件?
- python - 使用 For 循环编辑列表中的后续值
- python-3.x - 有没有更好的方法在 python 中存储解压缩的值
- javascript - 使用fileSaver下载的xlsx文件打开时无效