非同期Reduxアクションを非常にうまくテストしていますが、非常に単純な状況で完全に困惑しています。ここに私の関数である:私は私の状況401ケースをテストする場合フェッチ・モックを使って非同期Reduxアクションをテストし、jsonに変換する
//actions.js
export const fetchInfo = (credentials) => {
return (dispatch) => {
dispatch(asyncRequest())
const fetchConfig = {
method: 'POST',
body: JSON.stringify({
email: credentials.email,
token: credentials.token
})
}
return fetch('api/info', fetchConfig)
.then(response => {
if (response.status === 200) {
return response.json()
.then((json) => {
console.log(json)
dispatch(receiveInfoSuccess(json))
} else if (response.status === 401) {
dispatch(push('/signin'))
},
error => {
dispatch(receiveInfoFailure(error))
})
}
}
、すべてが正常ですが、いくつかの理由のために私はresponse.json()
過ぎて実行するテストを取得することはできません。いつも時間がTimeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
と外
it('should use fetchInfo to dispatch relevant action creators', (done) => {
const credentials = {"email": "[email protected]", "token": "djbsdJ"}
const fetchReturnVal = {
status: 200,
headers: {"Content-Type": "application/json"},
body: {
"id": "kjbsdf"
}
}
fetchMock.post('api/info', fetchReturnVal)
const expectedActions = [
{type: 'ASYNC_REQUEST'},
{type: 'RECEIVE_INFO_SUCCESS',
payload: {
id: 'kjbsdf'
}
}
]
function mockDispatch(action) {
const expectedAction = expectedActions.shift()
expect(action).toEqual(expectedAction)
if (!expectedActions.length) {
done()
}
}
actions.fetchLogin(credentials)(mockDispatch)
})
テストとコードが.then
ブロックに実行されることはありません。ここに私のテストがあります。
response.json
を解決するにはどうすればよいですか?同様