react + redux,的情况下,大家怎么测试 high-ordered component?
資深大佬 : yazoox 6
一般来说,都有 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)