reactjs - 使用 Enzyme 进行测试时如何在渲染道具中渲染元素
问题描述
我有一个具有这种结构的组件
return <Element>
{(a, b): React.ReactElement => (
...some elements here
)}
</Element>;
当我测试它时,我得到了这个debug
<Element>
[function]
</Element>
我的问题是进入这个函数来检查它返回的元素的通常方法是什么?
我的测试
import React from 'react';
import {createShallow} from '@material-ui/core/test-utils';
import Component from './Component';
let shallow;
function setup() {
const props = {};
const wrapper = shallow(<Component {...props} />);
return {
props,
wrapper,
};
}
beforeAll(() => {
shallow = createShallow({ dive: true });
});
describe('Components', () => {
describe('Element', () => {
it('Should render self', () => {
const {wrapper, props} = setup();
console.log(wrapper.debug())
expect(wrapper.exists()).toBeTruthy();
});
});
});
解决方案
有两种解决方案:
- 使用
wrapper.dive()
orwrapper.shallow()
会导致您ShallowWrapper
渲染顶级组件的子组件。但是,我完全不建议使用shallow
它,因为它有很多问题,而您刚刚经历过一个问题 - 它不会渲染所有内容。 - 使用
mount
而不是,shallow
您将默认呈现您的子函数。
推荐阅读
- python - PyQt5:如何调整主窗口大小以适应 Stackedwidget 或 Stackedlayout
- amazon-web-services - AWS - 是否可以将参数从 AWS lambda 函数传递到 AWS sagemaker notebook
- docker - Docker容器中的Spark不读取Kafka输入-结构化流
- java - 是否可以使 Spring MVC Web 应用程序作为嵌入 Java 和 Tomcat 的“独立可执行文件”运行?
- c# - 如何从 Azure 服务总线主题订阅一次接收 N 条消息
- r - 使用条件在 r 中创建组合列表
- javascript - 将值重新分配给对象属性时出错
- bash - 在网络驱动器组/用户名中获取 acl 而不是 SID
- python - 子字符串的搜索列表:TypeError:“NoneType”类型的参数不可迭代
- angular - TS2532:在测试 Angular 的英雄之旅时,对象可能“未定义”