Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.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 如何使用Typescript模拟Firebase/Firestrore?_Reactjs_Typescript_Firebase_Jestjs - Fatal编程技术网

Reactjs 如何使用Typescript模拟Firebase/Firestrore?

Reactjs 如何使用Typescript模拟Firebase/Firestrore?,reactjs,typescript,firebase,jestjs,Reactjs,Typescript,Firebase,Jestjs,这是我需要测试的组件 ///AddProject.tsx 从“React”导入React,{useState}; 从“./firebase”导入{firebase}; 从“./helpers”导入{generatePushId}; 从“./上下文”导入{useProjectsValue}; 界面道具{ shouldShow:布尔值 } export const AddProject:React.FC=({shouldShow})=>{ const[show,setShow]=useState(s

这是我需要测试的组件

///AddProject.tsx
从“React”导入React,{useState};
从“./firebase”导入{firebase};
从“./helpers”导入{generatePushId};
从“./上下文”导入{useProjectsValue};
界面道具{
shouldShow:布尔值
}
export const AddProject:React.FC=({shouldShow})=>{
const[show,setShow]=useState(shouldShow)
const[projectName,setProjectName]=useState(“”);
const projectId=generatePushId();
const{projects,setProjects}=useProjectsValue();
const addProject=async()=>{
项目名称&&
等待火力基地
.firestore()
.收集(“项目”)
.添加({
投射的,
名称:projectName,
userId:“userId”,
})
.然后(()=>{
setProjects([…projects]);
setProjectName(“”);
设置显示(假);
});
};
返回(
{show&&(
setProjectName(e.target.value)}
className=“添加项目名称”
数据testid=“项目名称”
type=“text”
占位符=“命名您的项目”
/>
addProject()}
数据testid=“添加项目提交”
>
添加项目
设置显示(假)}
onKeyDown={()=>setShow(false)}
>
取消
)}
+
设置显示(!show)}
onKeyDown={()=>setShow(!show)}
role=“按钮”
tabIndex={0}
>
添加项目
);
};
导出默认项目;
这是我的测试文件

从“React”导入React;
从“@testing library/react”导入{render,cleanup,firevent}”;
从“./components/AddProject”导入AddProject;
从“./上下文”导入{useSelectedProjectValue,useProjectsValue};
从“./firebase”导入{firebase};
jest.mock(“../firebase”,()=>({
火基:{
firestore:jest.fn(()=>({
集合:jest.fn(()=>({
add:jest.fn(()=>Promise.resolve(“我已解决”),
})),
})),
},
}));
jest.mock(“../context”,()=>({
useSelectedProjectValue:jest.fn(),
useProjectsValue:jest.fn(),
}));
const mockedProjectValue=useProjectsValue as jest.MockedFunction<
使用项目值的类型
>;
const mockedSelectedProjectValue=useSelectedProjectValue作为jest.MockedFunction<
useSelectedProjectValue的类型
>;
描述(“

我强烈建议不要嘲笑,因为在幕后有很多细微差别和相互关联的部分。
ERROR: 

node:97958) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'collection' of undefined
(Use `node --trace-warnings ...` to show where the warning was created)
(node:97958) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)