如何在React Fetching Library中模拟上下文?

时间:2020-08-22 07:32:02

标签: reactjs typescript unit-testing jestjs react-testing-library

大家好,我在模拟react fetching library function useQuery以通过容器组件中的api读取数据的测试方面遇到问题。现在我有一个错误,无法在dom中找到模拟标签。在控制台中,它仍然输出,只有加载程序可见。 我不知道如何等待加载指示灯消失。

我有以下错误:

Unable to find an element with the 
text: #1. This could be because the text is broken up by multiple elements. 
In this case, you can provide a function for your text matcher to make your matcher more flexible.

这是容器组件测试

import React from 'react';
import { getByTestId } from '@testing-library/react';
import { ClientContextProvider } from 'react-fetching-library';

import { render, act } from 'tests';
import tags from 'api/mocks/tags-response.json';

import { TrendingTagsContainer } from './TrendingTagsContainer';

describe('TrendingTagsContainer component', () => {
  const client = {
    query: async () => ({
      error: false,
      status: 200,
      payload: tags,
    }),
    suspenseCache: false,
  };
  test('should render component with correct title and description', async () => {
    jest.useFakeTimers();
    const { getByText, container } = render(
      <ClientContextProvider client={client}>
        <TrendingTagsContainer />
      </ClientContextProvider>,
    );
    expect(getByTestId(container, 'loader')).toBeInTheDocument();
    act(() => {
      jest.runAllTimers();
    });
    expect(getByText('#1')).toBeInTheDocument();
  });
});

0 个答案:

没有答案
相关问题