我正在使用jest和酶对我的reactjs代码进行单元测试,其中我有主要的功能组件"TrashDisplayFiles“,其中有一个方法"getDeletedData",其中我调用了通过从api获取响应来设置TrashFileState的api。
function TrashDisplayFiles(props){
const[TrashFileState,setTrashFileState]=useState([]);
//API CALL
useEffect(()=>{
getDeletedData();
},[]);
const getDeletedData=()=>{
trackPromise(
Axios.get(getUrl(),
{headers:{
Authorization: `Basic ${btoa(getToken())}`
}}).then((response) => {
let FileData=response.data;
setPaginationDefault(response.data.list.pagination)
setTrashFileState(response.data.list.entries.map(d=>{
return {
select:false,
id:d.entry.id,
name:d.entry.name
}}))
}).catch(err=>alert(err))
)
};
如何对此setTrashFileState(response.data.list.entries.map(d=>{return{和错误警报进行单元测试,以便将其包含在覆盖率报告中
此外,在我的jsx中不会通过onsubmit或onclick来模拟此函数,当加载垃圾页面时,会自动触发此api。
发布于 2021-01-20 19:07:54
你不能这样测试React函数的状态,你只能测试组件的行为(我认为这永远是一种更好的方法)。
当然,您可以在稍后的代码中使用此TrashFileState
,可能是在render
中。所以,测试一下使用的结果吧!
https://stackoverflow.com/questions/65770368
复制相似问题