0

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));
  }
};

4

0 回答 0