Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 当componentDidMount中存在函数调用时,酶装载失败_Reactjs_Mocha.js_Sinon_Enzyme - Fatal编程技术网

Reactjs 当componentDidMount中存在函数调用时,酶装载失败

Reactjs 当componentDidMount中存在函数调用时,酶装载失败,reactjs,mocha.js,sinon,enzyme,Reactjs,Mocha.js,Sinon,Enzyme,我有一个react组件,其中有一个单击功能,如下所示: clickTwitter(e) { e.stopPropagation(); let storyId = this.refs.storyToTweet.getAttribute('id'); if (storyId != undefined) { let storyIdValue = this.props.storyId; this.postOnTwitter(storyId

我有一个react组件,其中有一个单击功能,如下所示:

    clickTwitter(e) {

    e.stopPropagation();
    let storyId = this.refs.storyToTweet.getAttribute('id');
    if (storyId != undefined) {
        let storyIdValue = this.props.storyId;
        this.postOnTwitter(storyIdValue);
    }
}
在componentDidMount中,我有以下内容:

    componentDidMount(){
    this.fbInit();
}

fbInit(){
    return facebookInitializer= function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src = `https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.12&appId=${FACEBOOK.appId}&autoLogAppEvents=1`;
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk');

}
我有以下测试用例:

        it('should call the clickTwitter method once', () => {
        // creates the spy for the clickTwitter method
        let clickTwitterSpy = sinon.spy(StoryMedia.prototype, 'clickTwitter');
        let props={

        }
        // wraps the component by using the enzyme mount method
        const wrapper = mount(<StoryMedia/>);

        // simulates a user clicking the twitter button
        wrapper.find('.shareTwitterDummy').simulate('click');
        // asserts that the clickTwitter method was called once when the button was clicked
        assert.strictEqual(StoryMedia.prototype.clickTwitter.calledOnce, true);
        // restores the method to its original state
        clickTwitterSpy.restore();
    });
如果我删除this.fbInit()函数;从componentDidMount开始,一切正常。
我是否可以告诉测试忽略this.fbInit()的值?或者欢迎任何其他解决方案

我认为
var js,fjs=d.getElementsByTagName[0]
fjs是
未定义的

我怎样才能避免它?我不知道你想要什么
fjs
TypeError: Cannot read property 'parentNode' of undefined....