简体   繁体   English

如何在React无状态功能组件中测试道具?

[英]How do I test props in a React stateless functional component?

With Jest and Enzyme, I am trying to test that my Logo component receives the right props ( id , height , width , active , disableTransparency ). 使用Jest和Enzyme,我试图测试我的Logo组件是否接收正确的道具( idheightwidthactivedisableTransparency )。

I have tried creating an instance ( wrapper.instance() ), but this returns null. 我尝试创建一个实例( wrapper.instance() ),但是此方法返回null。 I have also tried wrapper.getElement().props , and this returns the props for the components rendered. 我还尝试了wrapper.getElement().props ,这返回了呈现组件的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>
);

Have you tried wrapper.props() ? 您是否尝试过wrapper.props() This is how I typically get at a component's props when using Enzyme. 这就是我通常在使用酶时获得组件道具的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确使用React.StatelessFunctionalComponent <Props> 在无状态功能组件上? - How to properly use React.StatelessFunctionalComponent<Props> on a stateless functional component? 如何在无状态功能组件中访问 props.children? - How to access props.children in a stateless functional component in react? 将道具传递给无状态功能组件 - React Pass Props to a Stateless Functional Component React - 如何确定组件是否无状态/功能? - React - how to determine if component is stateless/functional? 反应:如何访问 class 组件中的功能组件的道具 - React: How can I acces to props of a functional component in a class component 如何使用 TypeScript 为无状态的功能性 React 组件指定(可选)默认道具? - How to specify (optional) default props with TypeScript for stateless, functional React components? 在React上,如何在功能/无状态组件上调用组件安装上的函数? - On React, how can I call a function on component mount on a functional/stateless component? 当我尝试与无状态功能组件进行条件渲染时,出现“预期的意外令牌” - “Unexpected token, expected ,” when I try to do conditional rendering in react with stateless functional component 如何在功能组件的 getInitialprops 中访问特定页面的道具? - How do I access props for a particular page in getInitialprops for a functional component? 如何在 React 功能组件中正确更新 props - How to update props correctly in React functional component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM