reactjs - 如何从 react-fetching-library 模拟 useQuery?
问题描述
您好,我如何模拟 useQuery?我有负责调用 api 的容器组件和简单的 ui 组件来为用户显示数据。我收到当前错误
console.error node_modules/@testing-library/react/dist/act-compat.js:52
Error: Error: connect ECONNREFUSED 127.0.0.1:80
容器
import React from 'react';
import { screen, waitForElement, getByText } from '@testing-library/react';
import { useQuery } from 'react-fetching-library';
import { render } from 'tests';
import tags from 'api/mocks/tags-response.json';
import { TrendingTagsContainer } from './TrendingTagsContainer';
jest.mock('react-fetching-library');
describe('TrendingTagsContainer component', () => {
test('should render component with correct title and description', async () => {
const action = jest.fn();
const useQuery = jest.fn(action());
useQuery.mockReturnValue({ loading: false, error: false, query: () => true, payload: { tags } });
console.log(useQuery());
const { getByText } = render(<TrendingTagsContainer />);
await 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
推荐阅读
- android - Toast 在 Android API 28 中显示不到一秒钟
- android - 如何将 GitHub 存储库下载到我当前正在处理的 Andriod Studio 项目中?
- python - 使用 pandas 查找具有给定范围的列的长度
- sql - SQL 帮助 - 在当前记录时间戳之前查找顺序
- image - 如何在 react-native 中成功缓存最常用的数据(图像和文本)
- r - 在闪亮的 R 中显示来自网络的图像
- python - OSError: [Errno 1] 不允许操作
- go - Go中使用递归和并发的第N个斐波那契数
- python - 在使用 keras Lambda 时,如何处理“TypeError: Object arrays are not supported”?
- python - Python 给了我索引号而不是列表中的输入。