【发布时间】:2017-05-21 21:01:51
【问题描述】:
当快照测试(开玩笑快照)连接到 redux 存储的组件时,除了连接的组件外,我还可以导出实际组件
// User.js
/* ... */
export class User extends React.Component {/* ... */}
/* ... */
export default connect(mapStateToProps)(User);
在测试文件中,我可以导入实际的组件(不是连接的版本)并对其进行快照测试。
// User.spec.js
import { User } from './User';
/* ... toMatchSnapshot() testing */
但是当一个连接的组件嵌套在另一个连接的组件中时,我遇到了问题。例如,假设User 组件嵌套在另一个连接组件中 -
// Wrapper.js
import User from './User'; // importing the connected version
/* ... */
export class Wrapper extends React.Component {
/* ... */
render() {
return (
<div>
/* ... */
<User />
</div>
);
}
}
export default connect(mapStateToProps)(Wrapper);
在 Wrapper 上运行快照测试时,与在 User 上所做的方式相同,出现以下错误:
Invariant Violation: Could not find "store" in either the context or props of "Connect(User)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(User)".`
快照时有什么方法可以进行浅渲染吗?还是我做错了什么?
【问题讨论】: