yazoox
V2EX  ›  React

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

  •  
  •   yazoox · Nov 4, 2020 · 1917 views
    This topic created in 2020 days ago, the information mentioned may be changed or developed.

    一般来说,都有 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 replies    2020-11-05 10:32:56 +08:00
    azcvcza
        1
    azcvcza  
       Nov 5, 2020
    也许可以去 stackoverflow 找找 ‘how to test higher order component in react'
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5630 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 03:51 · PVG 11:51 · LAX 20:51 · JFK 23:51
    ♥ Do have faith in what you're doing.