redux-mock-store - 使用 jest 模拟多个 Axios 调用
问题描述
我async
在 React 中有这个任务。我使用了 Axios 和 redux-thunk。我想用 jest 写一个测试。但是这个redux 文档中的示例使用了 fetch-mock,它模拟了使用 fetch 或 node-fetch 发出的请求。如何实现模拟 Axios 的测试?:
const fetchMeals = () => async (dispatch) => {
dispatch(fetchMealsRequest);
try {
const { data: { categories } } = await axios.get('https://www.themealdb.com/api/json/v1/1/categories.php');
const arrOfPromises = categories.map((cat) => axios.get(`https://www.themealdb.com/api/json/v1/1/filter.php?c=${cat.strCategory}`));
const allMeals = await Promise.all(arrOfPromises);
const meals = categories.reduce((acc, currVal, idx) => {
acc.push({
mealCategory: currVal.strCategory,
categoryThumb: currVal.strCategoryThumb,
categoryDescription: currVal.strCategoryDescription,
meals: allMeals[idx].data.meals,
});
return acc;
}, []);
dispatch(fetchMealsSuccess(meals));
} catch (err) {
dispatch(fetchMealsFailure(err.message));
}
};
解决方案
推荐阅读
- javascript - jQuery 雪插件不会在所有页面上显示相同数量的雪
- r - 当向量的项不匹配时使用 which 和 %in%
- python - np.vectorize 返回不正确的舍入结果
- android - 运行 ReactNative 项目时连接被拒绝错误
- migration - 将 100 多个 repos 迁移到 gitlab 并保持相同的 repo 路径
- oracle - 无法使用 ODBC 连接到一个 Oracle 数据库
- oauth-2.0 - Clio api 与 php 中的 OAuth-2 集成
- html - 使用 :after 元素对按钮的悬停效果 - 当鼠标超出按钮尺寸时动画出来
- java - Javafx mediaViewer/mediaPlayer 抛出 java.lang.reflect.InvocationTargetException 和 java.lang.NullPointerException
- typescript - 如何在不重新渲染组件的情况下同步获取 Stencil 组件参数并进行设置?