1
这是有点随访到Nested components testing with Enzyme inside of React & Redux的...测试嵌套反应的组分与酶
我有需要测试道具和方法的组分。然而,该组件需要在提供商为了工作呈现:
const renderComponent = (props) => {
<Provider store={ createStore(reducer, initialState) }>
<ChildComponent { ...props }/>
</Provider>
};
我的问题是,我现在如何测试此组件上的方法?这不起作用:
it('can call its own methods',()=> {
const wrapper = mount(renderComponent(defaultProps)).find('ChildComponent');
wrapper.instance().call('someMethod'); // returns Error: ReactWrapper::instance() can only be called on the root
});
你不需要使用Provider。只需在组件文件的末尾添加一个额外的'export'(即不连接到Redux)。 'export default connect(...)(MyComponent);' 'export {MyComponent};'然后从'...'使用import {MyComponent}导入它;'' – nbkhope