简体   繁体   English

如何在React的render()中对定义为const的函数进行单元测试?

[英]How to conduct the unit test for a function defined as const in render() in React?

If I have a function defined in render(), how can I add a unit test for it? 如果我在render()中定义了一个函数,如何为其添加单元测试?

export class Foo extends React.Component {
  componentDidMount() {
   ...do something...
  }

  render() { 
   const ConditionalWrapper = ({ condition, wrapper, children }) =>
      condition ? wrapper(children) : children;

   return (
     <ConditionalWrapper condition={...} wrapper={children => <div> {children} </div>}>
        <...>
     </ConditionalWrapper>
   )
  }
}

How can I test the ConditionalWrapper function defined in render() with jest? 我如何用jest测试在render()中定义的ConditionalWrapper函数?

I don't think it is possible. 我认为不可能。 You have to move it outside the render method if you think of unit testing it. 如果要进行单元测试,则必须将其移到render方法之外。 Indirect way is get the snapshots and look for certain key terms that would be created based on the condition. 间接方式是获取快照并查找根据条件创建的某些关键术语。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM