V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yazoox
V2EX  ›  React

react + redux,的情况下,大家怎么测试 high-ordered component?

  •  
  •   yazoox · 2020-11-04 15:59:00 +08:00 · 1393 次点击
    这是一个创建于 1259 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一般来说,都有 pure component 和 high ordered component

    前者就是纯组件,接收 props,然后根据 props 画出来。基本没有计算,即使有,也只需要 props 就可以完成。(这个测试,我们通常是用 testlibrary/react 来测试画出来的 dom )

    但通常,pure component 外面会有一层 hoc, 即用 redux 的方法 connect 把 mapStateToProps & mapDispatchToProps 绑定的那个组件。

    我的问题是,这个 hoc 怎么测试?

    mapStateToProps & mapDispatchToProps 比较好测试,只是函数,export 后,就可以测试了。

    但是这个 hoc 本身,不知道怎么测试。因为这个组件在传递给内部的 pure components 的 props 时,还是有一些内部的函数的,主要是这些函数,不知道怎么测试。

    举个例子: 我怎么测试这个 getName 函数?

    class mycomp extend React.Component {
    
      function getName() {
        //根据 props 来计算
        this.props.
      }
    
      render() {
        return (
          <div>
            <wrapped-component
              name={this.getName()}
            />
          </div>
        );
      }
    }
    
    export default connect(mapStateToProps, mapDispatchToProps)(mycomp);
    
    

    谢谢!

    1 条回复    2020-11-05 10:32:56 +08:00
    azcvcza
        1
    azcvcza  
       2020-11-05 10:32:56 +08:00
    也许可以去 stackoverflow 找找 ‘how to test higher order component in react'
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5060 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:46 · PVG 17:46 · LAX 02:46 · JFK 05:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.