非同期reduxアクションから正しい出力を得ることができません。私はJest、redux-mock-adapter、thunkをツールとして使用しています。非同期ReduxアクションJest
reduxの非同期サンク(https://redux.js.org/docs/recipes/WritingTests.html#async-action-creators)のテストに関するドキュメントによると、テストでは2つのアクションの配列が返されます。しかし、私のテストでは、最初のアクションだけが返され、成功したフェッチで返されるはずの2番目のアクションは返されません。私はここに小さなものを見逃していると思うが、それほど言わないのは面倒だ。
Reduxのアクション
export const getRemoveFileMetrics = cacheKey => dispatch => {
dispatch({ type: IS_FETCHING_DELETE_METRICS });
return axios
.get("GetRemoveFileMetrics", { params: { cacheKey } })
.then(response => dispatch({ type: GET_REMOVE_FILE_METRICS, payload: response.data }))
.catch(err => err);
};
テスト
it("getRemoveFileMetrics() should dispatch GET_REMOVE_FILE_METRICS on successful fetch",() => {
const store = mockStore({});
const cacheKey = "abc123doremi";
const removeFileMetrics = {
cacheKey,
duplicateFileCount: 3,
uniqueFileCount: 12,
};
const expectedActions = [
{
type: MOA.IS_FETCHING_DELETE_METRICS,
},
{
type: MOA.GET_REMOVE_FILE_METRICS,
payload: removeFileMetrics,
}
];
mockRequest.onGet(`/GetRemoveFileMetrics?cacheKey=${cacheKey}`).reply(200, removeFileMetrics);
return store.dispatch(MOA.getRemoveFileMetrics(cacheKey)).then(() => {
const returnedActions = store.getActions();
expect(returnedActions).toEqual(expectedActions);
});
});
出力
Expected value to equal:
[{ "type": "IS_FETCHING_DELETE_METRICS" }, { "payload": { "cacheKey": "abc123doremi", "duplicateFileCount": 3, "uniqueFileCount": 12 }, "type": "GET_REMOVE_FILE_METRICS" }]
Received:
[{ "type": "IS_FETCHING_DELETE_METRICS" }]