我正在尝试将上下文传递给 React 组件,但是因为我正在使用 Enzyme 进行测试,所以我想动态地将组件添加到它的父组件中,以便我可以检查它的状态。测试看起来像这样:
describe('<BeaconConfig />', () => {
it('inherits the config from BeaconConfig', () => {
mount(<BeaconConfig persistent><div id="parent"></div></BeaconConfig>, { attachTo: document.body });
const wrapper = mount(<Beacon/>, { attachTo: document.getElementById('parent') });
expect(wrapper.state('persistent')).to.be(true);
});
});
测试失败是因为组件状态的persistent
属性是,尽管它应该通过上下文继承。Beacon
undefined
BeaconConfig
当我在挂载时尝试Beacon
直接放入 JSX 中时,BeaconConfig
它工作正常,但在这种情况下,Enzyme 不会让我进入Beacon
组件状态,因为它不是根。
当我将它动态添加到其父组件时,React 没有将上下文传播到我的组件是否正常?