Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 测试从本地JSON文件获取一些数据的异步操作创建者_Reactjs_Typescript_Testing_Redux_Jestjs - Fatal编程技术网

Reactjs 测试从本地JSON文件获取一些数据的异步操作创建者

Reactjs 测试从本地JSON文件获取一些数据的异步操作创建者,reactjs,typescript,testing,redux,jestjs,Reactjs,Typescript,Testing,Redux,Jestjs,我试图使用jest测试一个异步操作创建者,该创建者获取一些数据。我已经浏览了redux模拟商店文档,但实际上我没有掌握应该做什么。以下是行动: import { Dispatch } from 'redux'; export interface Task { id: string; title: string; categoryTitle?: string; error?: null; } export interface Category { name: string,

我试图使用jest测试一个异步操作创建者,该创建者获取一些数据。我已经浏览了redux模拟商店文档,但实际上我没有掌握应该做什么。以下是行动:

import { Dispatch } from 'redux';

export interface Task {
  id: string;
  title: string;
  categoryTitle?: string;
  error?: null;
}

export interface Category {
  name: string,
  tasks: Task[]
}

export const getCategories = () => {
  return async (dispatch: Dispatch) => {
    const response = await axios.get<Category[]>(url);

    dispatch<GetCategoriesAction>({
      type: ActionTypes.getCategories,
      payload: response.data
    });
  };
};

从'redux'导入{Dispatch};
导出接口任务{
id:字符串;
标题:字符串;
categoryTitle?:字符串;
错误?:空;
}
导出接口类别{
名称:string,
任务:任务[]
}
导出常量getCategories=()=>{
返回异步(分派:分派)=>{
const response=wait axios.get(url);
派遣({
类型:ActionTypes.getCategories,
有效载荷:response.data
});
};
};

我如何测试它?有人能指导我完成这些步骤吗?

在这里您不需要
redux mock store
。 本质上,您只需模拟axios(有大量关于如何实现的信息),并在测试调用
getCategories()(dispatch)
中使用
jest.fn()的实例作为dispatch。
然后,只需测试针对不同场景(如“axios Successed”或“axios failed”)调用参数的频率和使用参数的频率

redux的好处在于它将事物解耦

之后,您可以通过调用一行操作和
expect
它们返回的内容来测试减缩器


redux的好处在于它将这些层彼此分离,不需要一次测试所有内容。

谢谢,但您能给出一个代码示例说明如何做到这一点吗?我只是所有这些东西的新手,我希望能看到一个编写的代码示例。