2017-01-05 46 views
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 
}); 

回答

0

我只是谷歌,我发现这个链接

http://paulsturgess.co.uk/blog/2016/11/06/how-to-write-and-test-a-redux-container-component-for-react/

不知道,如果作品。我平时嘲笑的存储和不使用提供商。 ..

+1

你不需要使用Provider。只需在组件文件的末尾添加一个额外的'export'(即不连接到Redux)。 'export default connect(...)(MyComponent);' 'export {MyComponent};'然后从'...'使用import {MyComponent}导入它;'' – nbkhope

相关问题