【问题标题】:Enzyme: Test children rendering failling with mount but passes with shallow酶:测试儿童渲染失败,但通过浅
【发布时间】:2020-03-13 16:15:51
【问题描述】:

我正在使用 Enzyme + Jest 来测试一些 React 组件。

我有以下测试:

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

它的工作原理只是找到。但是,如果我将enzyme 中的shallow 方法替换为mount,则测试失败(找不到具有.should-render 类的元素)。

这是预期的行为吗?我认为shallowmount 之间的区别在于访问生命周期方法的能力,但渲染工作相同。

【问题讨论】:

    标签: reactjs testing jestjs enzyme


    【解决方案1】:

    好的。所以问题是我对mount 的工作原理缺乏了解。

    我的Modal 组件有一个名为show 的状态变量,如果它设置为false(我正在包装具有这种行为的react-boostrap 模态组件),它会阻止安装元素。默认情况下,此状态变量为 false,并且由于子元素正在模式的主体中呈现,因此未找到子元素,因为该元素未安装。

    【讨论】:

      猜你喜欢
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      相关资源
      最近更新 更多