gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:23:28 25 4
gpt4 key购买 nike

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

但是,我注意到当我这样做的时候

  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);
});

测试通过。

那么,我是做错了什么还是我理解错了什么?

For reference

最佳答案

是的,它在 enzyme 3.0 中。

https://github.com/airbnb/enzyme/blob/master/CHANGELOG.md#300

LifeCycleExperimental以前您必须在 shallow 上手动设置为 true 的选项现在默认启用,因为它现在很稳定。

这比在想要测试生命周期时必须诉诸 mount 要好得多。

现在绝对没有理由不使用 shallow 进行单元测试 :)... 除了需要测试 refs 的时候 :(。

关于javascript - componentDidMount 是否应该在 Enzyme 中以浅层渲染运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47309585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com