javascript - 如何使用 jest 和酵素对子功能进行单元测试?
问题描述
我有一个看起来像这样的组件:
const TestComponent = ({ mobile }) => (
<>
<div>
Your estimate
{!mobile && (
<InfoButton>
{target => (
<Tooltip
target={target}
>
My tooltip text
</Tooltip>
)}
</InfoButton>
)}
</div>
</>
);
Tooltip
我正在尝试使用 jest 和酶为此添加一个单元测试,并在我尝试寻找被渲染时遇到问题。我相信这是因为它是由函数渲染的,所以当我开玩笑调试时,这就是我所看到的
<InfoButton className="infoButton">
[function]
</InfoButton>
而且我无法.find()
或.dive()
更深入地实际寻找该Tooltip
组件。在使用 jest 进行测试时,如何让该功能实际呈现内容?
更新:添加我的测试
it("should render info button with tooltip", () => {
const wrapper = renderSubject(); // My render fn which randers the parent compoennt
const testComponent = wrapper
.find("TestComponent")
.first()
.dive();
expect(testComponent.find("InfoButton").exists()).toBe(true);
console.log(testComponent.find("InfoButton").debug());
});
解决方案
推荐阅读
- javascript - 获取嵌入式 Tableau 仪表板的窗口高度
- php - 如何通过 PHP 的缩短 URL 找到一系列重定向后的最后一个(最终)URL
- postgresql - postgres中具有Hamming差异的唯一约束
- html - NVDA 不读取 Firefox 导航中的链接或标题属性,或屏幕阅读器文本
- database - 是否可以在 Oracle Express Edition 中使用保存点和回滚?
- c# - 不支持关键字:尝试打开 SQL 连接时出现“提供者”
- php - php访问变量
- javascript - 将元素添加到数组的末尾
- javascript - ReactJS:尝试打印我的数组对象的索引 - 如果我删除元素,它应该动态变化
- c++ - 无法让 `std::enable_if` 为无作用域枚举工作