我正在使用 enzyme mount 测试一个 react 组件(因为我正在测试它的生命周期方法)。我的问题是我的组件有一个 redux 连接的子组件,这给了我错误:
Invariant Violation: Could not find "store" in either the context or props of "Connect(Popup)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(Popup)
我该如何解决这个问题?
谢谢!
乌里
最佳答案
如错误所述,您需要为您的组件提供商店。
要么通过 props 传递:
const wrapper = mount(<PopupContainer store={store} />)
或者将其包装在 <Provider>
中:
const wrapper = mount(
<Provider store={store}>
<PopupContainer />
</Provider>
)
现在,如果您正在使用 Jest 并且不希望包装的组件干扰您的测试,您可以模拟它:
jest.mock('./Popup', () => 'Popup');
关于reactjs - 使用 enzyme 安装测试具有连接的子组件的 react 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45810856/