【发布时间】:2019-09-28 01:23:11
【问题描述】:
我想测试我的纯组件,所以我这样做:
MyComp.js
export MyComp = props => {
return (
<Wrapper>Content</Wrapper>
)
}
const MyCompConn = connect()(MyComp);
export default MyCompConn;
<Wrapper>:
export Wrapper = ({children}) => {
return (
<div>{children}</div>
)
}
const WrapperConn = connect()(Wrapper);
export default WrapperConn;
MyComp.test.js
import React from 'react';
import renderer from 'react-test-renderer';
import { MyComp } from '../../MyComp';
describe('With Snapshot Testing', () => {
it('renders!"', () => {
const component = renderer.create(<Login />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
});
现在,当我运行 yarn test 时,我得到了这个错误:
Invariant Violation: Could not find "store" in either the context or props of "Connect(AppWrapper)". Either wrap the root component in a <Provider> or explicitly pass "store" as a prop to "Connect(AppWrapper)"
发生这种情况是因为<Wrapper> 连接在我的<MyComp> 组件中,但我不确定如何测试只是后者,即使它包装在连接的组件上。 p>
【问题讨论】:
标签: javascript reactjs unit-testing redux jestjs