Reactjs 如何从react抓取库模拟useQuery?

Reactjs 如何从react抓取库模拟useQuery?,reactjs,jestjs,react-testing-library,Reactjs,Jestjs,React Testing Library,你好,我怎样才能模拟useQuery?我有负责调用api的容器组件,以及为用户显示数据的简单ui组件。我收到当前错误 console.error node_modules/@testing-library/react/dist/act-compat.js:52 Error: Error: connect ECONNREFUSED 127.0.0.1:80 容器 从“React”导入React; 从'@testing library/react'导入{screen,waitForElement,

你好,我怎样才能模拟useQuery?我有负责调用api的容器组件,以及为用户显示数据的简单ui组件。我收到当前错误

console.error node_modules/@testing-library/react/dist/act-compat.js:52
Error: Error: connect ECONNREFUSED 127.0.0.1:80
容器

从“React”导入React;
从'@testing library/react'导入{screen,waitForElement,getByText};
从“react fetching library”导入{useQuery};
从“测试”导入{render};
从“api/mocks/tags response.json”导入标记;
从“/TrendingTagsContainer”导入{TrendingTagsContainer};
mock('react-fetching-library');
描述('TrendingTagsContainer组件',()=>{
test('应该用正确的标题和描述呈现组件',async()=>{
const action=jest.fn();
const useQuery=jest.fn(action());
mockReturnValue({加载:false,错误:false,查询:()=>true,负载:{tags}});
log(useQuery());
const{getByText}=render();
等待waitForElement(()=>screen.getByText(“#Testing react”);
expect(screen.getByText(“#Testing react”).toBeInTheDocument();
});
});

我认为您可以简单地模拟
react fetching library
模块,如下所示:

import { useQuery } from 'react-fetching-library';

jest.mock('react-fetching-library');

// Everything looks the same I guess


谢谢,但是此解决方案仍然尝试获取外部api
Error:Error:connect econnrefered127.0.0.1:80 at Object。dispatchError
我不确定是否会多次调用useQuery,因此请尝试一直模拟,而不是仅使用MockImplementation一次。它位于文件顶部,它通过调用api帮助解决了错误。谢谢是的,我没有拒绝连接的错误,现在它只是关于一些测试包装问题,非常感谢。