javascript - 为什么酶“安装”包装只是呈现标签,但没有别的?
问题描述
我现在正在学习测试并创建了一个非常简单的示例。在我要测试的组件中,我在一个标签li
内呈现了一些元素。ul
当我将组件包装在mount
包装器中并打印html
到控制台时,我得到了<ul></ul>
我不知道为什么?现在的问题当然是我找不到任何button
我想模拟点击的元素。
它与我如何呈现待办事项列表有关吗?酶不能与 renderFunction 一起使用吗?
这是示例:https ://codesandbox.io/s/v8ljxo5xn5
对应的文件是TodoBody.js
和TodoBody.test.js
谢谢!
解决方案
这是因为 todos 数组是空的,所以 map 没有返回任何内容(可能是 [] - 你需要检查一下)。快速解决方案:
describe("the todobody component", () => {
test("it should call the passed in onDelete func from props", () => {
const todos = ['test'];
const test = { onDelete: () => {} };
jest.spyOn(test, "onDelete");
const wrapper = mount(<TodoBody onDelete={test.onDelete} todos={todos} />);
// this just renders <ul></ul> why??
console.log(wrapper.html());
});
});
const renderTodos = () => {
return todos.map(todo => (
<li key={`${todo}`}>
<p>{todo}</p>
<button onClick={handleDelete} data-test="test-btn-delete">
X
</button>
</li>
));
};
安慰:
<ul><li><p>test</p><button data-test="test-btn-delete">X</button></li></ul>
推荐阅读
- javascript - WebGL - 如何在 Typescript 中正确地将程序参数传递给 gl.attachShader?
- c++ - Why is template name available in derived class (the base class is an instance of the template)?
- c - 我应该为 FreeRTOS 系统上的程序选择什么调度?
- hadoop - Hadoop 3.2.0 中的链映射器
- php - 从不同的页面 PHP 获取 JSON 数据
- excel - 计算字段 - 上面的数字计数
- python - 无法构建 boost python 库(致命错误:pyconfig.h:没有这样的文件或目录)
- java - java中的继承规则
- python - 试图从网站上抓取电子邮件地址
- ssl - 将本地数据发送到网络浏览器...如何?