首页 > 解决方案 > 反应和依赖注入

问题描述

我正在尝试用 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 对象来进行单元测试。

我的问题是:

标签: reactjsunit-testingjestjschai

解决方案


推荐阅读