简体   繁体   English

酶:测试子级渲染失败但通过浅层

[英]Enzyme: Test children rendering failling with mount but passes with shallow

I'm using Enzyme + Jest to test some React components.我正在使用 Enzyme + Jest 来测试一些 React 组件。

I have the following test:我有以下测试:

describe('<Modal />', () => {
  let wrapper;

  it('should render children props', () => {
    wrapper = shallow(<Modal />);
    wrapper.setProps({
      children: <div className='should-render'>This should be rendered</div>
    });
    expect(wrapper.find('.should-render').length).toEqual(1);
  });
});

And it works just find.它的工作原理只是找到。 But, if I replace the shallow method from enzyme with mount the test fails (can't find a element with .should-render class).但是,如果我用mount替换enzymeshallow方法,则测试失败(找不到具有.should-render类的元素)。

Is this the expected behavior?这是预期的行为吗? I though that the difference between shallow and mount was the ability to access lifecycle methods but render worked the same.我认为shallowmount之间的区别在于能够访问生命周期方法,但渲染的工作方式相同。

Ok.好的。 So the problem was my lack of understanding of how mount works.所以问题是我对mount工作原理缺乏了解。

My Modal component has a state variable called show that prevents the element from mounting if it's set to false (I'm wrapping react-boostrap modal component, that has this beahavior).我的Modal组件有一个名为show的状态变量,如果它被设置为false它会阻止元素安装(我正在包装具有这种行为的react-boostrap模态组件)。 By default, this state variable it's false and since the children are being rendered in the body of the modal, no children were found because the element wasn't beign mounted.默认情况下,这个状态变量是假的,因为子元素在模态的主体中呈现,所以没有找到子元素,因为元素没有被安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM