如何单元测试React Component shouldComponentUpdate方法

如何单元测试React Component shouldComponentUpdate方法,第1张

如何单元测试React Component shouldComponentUpdate方法

我可能只会直接调用shouldComponentUpdate。

就像是

const comp = shallow(<Comp {...props} />)const shouldUpdate = comp.instance().shouldComponentUpdate(nextProps, nextState)expect(shouldUpdate).toBe(true/false)

尝试通过确定组件是否实际渲染/未渲染来进行测试可能比其价值更大。我什至不知道你会怎么用酶来做到这一点。您不能真正脱离渲染的输出,因为除非渲染的输出与以前相同,否则您可能不会从shouldComponentUpdate返回false。因此,仅靠输出就不能确定渲染是否发生。

通过直接调用它进行测试对我来说似乎还不错。只要您相信React将正确使用您的shouldComponentUpdate返回值(如果没有,我们就会遇到更大的问题),那是安全的。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4932622.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-13
下一篇2022-11-12

发表评论

登录后才能评论

评论列表(0条)

    保存