2017-02-27 31 views
0
componentWillMount() { 
    // redux store 
    CrudActions.getUsers(); 
} 

如何使用笑話來呼叫我的減速器?我想寫的行爲,而不是調用另一個CrudActions的SETALL功能測試。componentWillMount內的測試功能

到目前爲止,我只能夠調用componentWillMount

test('calls `componentWillMount` before rendering',() => { 
    const onWillMount = jest.fn(); 
    CrudList.prototype.componentWillMount = onWillMount; 
    mount(<CrudList />); 

    expect(onWillMount).toBeCalled(); 
}); 

回答

0

我不是100%肯定你的問題是什麼在這裏,所以我訓釋爲「我怎麼可以卸載我的組件,以便CrudActions.getUsers()叫做?」

安裝可以調用unmount卸載它的成分(我假設你正在使用酶來安裝該組件)後。

test('calls `componentWillUnmount` when unmounted',() => { 
    const onWillUnmount = jest.fn(); 
    CrudList.prototype.componentWillUnmount = onWillUnmount; 
    mount(<CrudList />).unmount(); 

    expect(onWillUnmount).toBeCalled(); 
}); 

所以叫CrudActions.getUsers()只是不假componentWillUnmount,它應該調用的實際功能。