reactjs - 反应和依赖注入
问题描述
我正在尝试用 jest 测试一个反应组件,这看起来像:
class UserOnSamePageAlert extends React.Component {
constructor(props) {
super(props);
this.state = {
connected_users: []
};
// subscribe to channel to know if a user is on the same page
this.onSamePageChannel = new OnSamePageSubscription({
current_user: sessionStorage.getItem('current_user'),
url: props.urlToSubscribe,
onUpdate: this.onUserAction,
});
}
... more code here ...
}
还有我的 OnSamePageSubscription 组件:
export default class OnSamePageSubscription extends React.Component {
constructor(props) {
super(props);
... more code here ...
}
... more code here ...
}
这工作正常,但我认为这不是测试目的的好方法。我认为我必须注入 OnSamePageSubscription 对象来进行单元测试。
我的问题是:
- 哪种注入方式是反应的最佳方式(最佳实践或库)
- 有没有一个好的库来模拟对象?
- 或者也许我不必为了模拟而注射?
解决方案
推荐阅读
- react-native - connectycube 视频通话中的安全错误
- php - 这个肥皂请求中缺少什么?
- javascript - JS ROT 13 部分工作,但不是全部工作
- sql-server - SQL Server:如何在单个查询中在 Count 之前执行 TOP
- javascript - 删除文本时AngularJS输入值不更新
- ios - UICollectionViewCell 大小错误
- google-chrome - 使用 OAuth2 在 Chrome 扩展程序中验证非 Google 帐户
- reactjs - 如何在处理函数中链接 useLazyQuery 钩子
- html - 如何正确对齐项目 CSS?
- bash - 从 bash 变量中删除前导换行符