javascript - 如何在 React 无状态功能组件中测试道具?
问题描述
使用 Jest 和 Enzyme,我正在尝试测试我的Logo组件是否接收到正确的道具 ( id
, height
, width
, active
, disableTransparency
)。
我曾尝试创建一个实例 ( wrapper.instance()
),但这会返回 null。我也试过wrapper.getElement().props
了,这会返回渲染组件的道具。
const Logo = ({id, height, width, active, disableTransparency}) => (
<Svg x='0px' y='0px' height={height} width={width} viewBox='0 0 1000 1000'>
{(active) && <Gradient id={id}/>}
{(disableTransparency) && <Underlay/>}
<Overlay id={id} active={active}/>
</Svg>
);
解决方案
你试过wrapper.props()
吗?这就是我在使用 Enzyme 时通常获得组件道具的方式。
推荐阅读
- java - Android 通过蓝牙从 HC-06 发布/接收数据
- python - python pandas中的SAS VARLEN等价物
- android - React native android apk安装错误
- hadoop - Ranger 未安装在 Kerberos Hadoop 集群中
- core - 如何设置位置和大小图像变量 Stimulsoft Core
- python - 将按钮添加到 ListView 项的右侧
- reactjs - 如何使 react-native-tab-view 的 TabBar 变小?
- android - 标签显示错误的标题
- java - Spring 状态机操作如何工作
- angular - 为什么使用 Angular CLI 构建的 Angular 应用程序会在 github 上发出安全警告,我该如何解决?