0
reactjsで私の非同期アクションをテストする際に問題があります。TypeError:非同期アクションをテストしようとすると未定義のプロパティ 'then'を読み取ることができません
export function fillStoryBoard(snippetsLink, channel, uniqueStoryObj, isUniqueStorySearch) {
return function(dispatch){
for (var i = 0; i < snippetsLink.length; i++) {
loadStorySnippet(channel, snippetsLink[i], (i + 1),dispatch);
}
}
}
function loadStorySnippet(channel,snippetsLink,indexValue,dispatch){
return axios.get(ServiceUrls.prototype.getServicesDfltUrl()+snippetsLink)
.then(response => {
dispatch({
type: "FILL_STORYBOARDS",
payload: {
"channelName": channel,
"storiesSnippet": response.data,
"order":indexValue
}
});
}).catch(function(response){
});
}
だから私asncコールがエクスポートされていない機能であり、ループの中に、このasnc関数を呼び出す機能があります: はここに私のコードです。今ここに
は私のテストで:
it('should dispatch type: FILL_STORYBOARDS with what is returned from server as a payload',() => {
nock(ServiceUrls.prototype.getServicesDfltUrl())
.get('/snippet/111')
.reply(200, {"id":1,"headline":"",
"label":"Other",
"imgUrl":"",
"postDate":0});
const expectedActions = [
{ "type": "FILL_STORYBOARDS","payload": { "channel": "",
"storiesSnippet": {"id":1,"headline":"",
"label":"Other","imgUrl":"" +
"",
"postDate":0},
"order":1 } }
]
const store = mockStore();
return store.dispatch(fillStoryBoard(["snippet/111"], "")).then(() => {
expect(store.getActions()[0].type).to.equal(expectedActions[0].type);
})
})
さて問題は、ときに私が取得するテストが実行されます。
TypeError: Cannot read property 'then' of undefined
任意のアイデア私はこの問題を解決することができますか?
おかげで多くのことを。 loadStorySnippet(channel、snippetsLink [i]、(i + 1)、dispatch);のようなループのためのものではないかという質問。どのようにshoudl私はこれを約束で包みますか? –
'loadStorySnippet'は、アキシャスに戻るので、すでに' loadStorySnippet(...) 'を返すので、すでに約束を返します。 –
あなたはこのようにsthを意味します:return loadStorySnippet(channel、snippetsLink [i]、(i + 1)、dispatch); ? –