componentDidMount是否应该在Enzyme中以浅层渲染运行? [英] Is componentDidMount supposed to run with shallow rendering in Enzyme?

查看:45
本文介绍了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 中。



https://github.com/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.

https://github.com/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中以浅层渲染运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆