componentDidMount是否应该在Enzyme中以浅层渲染运行?

编程入门 行业动态 更新时间:2024-10-24 19:21:33
本文介绍了componentDidMount是否应该在Enzyme中以浅层渲染运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

根据我的理解以及到目前为止我在各种答案中所读的内容,并非所有生命周期方法都应该在浅层渲染中运行。特别是 componentDidMount

但是,我注意到当我这样做

beforeEach(function(){ fakeComponentDidMount = sinon.stub(Component.prototype,'componentDidMount'); fakeComponentDidMount.callsFake(function(){} ); 包装=浅(<组件{... props} />); }); afterEach(function(){ fakeComponentDidMount.restore(); }); 它(称为 componentDidMount,函数(){ Expect(fakeComponentDidMount.call).to.equal(true); });

测试通过。

那么,我在这里做错了还是我理解错了?

以供参考

解决方案

是的,它在酶3.0 中。

github/airbnb/enzyme/blob/master/CHANGELOG.md#300

LifeCycleExperimental 以前是您必须在 shallow 上手动设置为true的选项,因为它现在已经稳定了,所以现在默认启用。 / p>

这比想要测试生命周期时不得不求助于 mount 好得多。

现在绝对没有借口不使用 shallow 进行单元测试:)...除了需要时, o测试裁判:(。

From my understanding and from what I have read so far in various answers, not all lifecycle methods are supposed to be run with shallow rendering. Especially componentDidMount

However, I notice that when I do

beforeEach(function () { fakeComponentDidMount = sinon.stub(Component.prototype, 'componentDidMount'); fakeComponentDidMount.callsFake(function () {}); wrapper = shallow(<Component {...props} />); }); afterEach(function () { fakeComponentDidMount.restore(); }); it('calls componentDidMount', function () { expect(fakeComponentDidMount.called).to.equal(true); });

the test passes.

So, am I doing something wrong here or have I understood something wrong?

For reference

解决方案

Yes it is in enzyme 3.0.

github/airbnb/enzyme/blob/master/CHANGELOG.md#300

LifeCycleExperimental which was previously an option that you had to manually set to true on shallow is now enabled by default because it is now stable.

This is much nicer than having to resort to mount when wanting to test lifecycles.

There is absolutely no excuse to not use shallow for unit tests now :)... Well apart from when you need to test refs :(.

更多推荐

componentDidMount是否应该在Enzyme中以浅层渲染运行?

本文发布于:2023-11-14 02:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586029.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中以   浅层   componentDidMount   Enzyme

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!