首页 > 解决方案 > 如何在 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>
);

标签: javascriptreactjsreact-nativeecmascript-6jestjs

解决方案


你试过wrapper.props()吗?这就是我在使用 Enzyme 时通常获得组件道具的方式。


推荐阅读