我正在使用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);
});
});
而且它的工作原理就是找到。但是,如果我将shallow
中的enzyme
方法替换为mount
,则测试将失败(无法找到具有.should-render
类的元素)。
这是预期的行为吗?我虽然shallow
和mount
之间的区别是访问生命周期方法的能力,但是渲染的工作原理相同。
好所以问题是我对mount
的运作方式缺乏了解。
我的Modal
组件具有一个名为show
的状态变量,如果将其设置为false
,该状态变量将防止该元素挂载(我正在包装react-boostrap
模态组件,它具有此行为)。默认情况下,此状态变量为false,并且由于子代在模态主体中呈现,因此未找到任何子代,因为该元素未进行beign安装。