微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在玩笑中测试多个获取请求

如何解决在玩笑中测试多个获取请求

我找不到任何关于如何在 useEffect 中测试多个同步提取请求的文章或文档。我正在获取数据,然后在第二次获取开始之前,我在按钮上设置了“加载数据”工具提示。一旦第二次获取完成,工具提示要么变成“无法使用此功能”,要么根本没有工具提示。我不知道如何进入测试的那些状态,其中 jest 在 fetch 调用之前或之后。

问题是我的测试没有看到开始时的“加载数据”工具提示,即使它在我手动测试时出现。我很难理解何时等待第二次呼叫,何时实际发生以及何时完成...

只是为了澄清 getAccountInfo一个获取请求函数,它返回一个具有 accountInfo 属性的对象。

错误

TestingLibraryElementError: Unable to find an element with the text: /loading data/。这可能是因为文本被多个元素分解了。在这种情况下,您可以为您的文本匹配器提供一个函数,使您的匹配器更加灵活。

这是我的测试

import React from 'react';
import * as utils from '../utils';
import { render,screen,fireEvent,waitFor } from '@testing-library/react';
import fetch from 'jest-fetch-mock';

const getAccountInfoSpy = jest.spyOn(utils,'getAccountInfo');
const testId = 'ABCDEF';

describe('AccountInfo',() => {
  beforeAll(() => fetch.enableMocks());
  afterall(() => fetch.disableMocks());
  beforeEach(() => jest.clearallMocks());
  
  it('displays "loading data" tooltip on hover' () => {
    fetch.mockResponse(
      JSON.stringify({
        orgId: testId,name: 'Adam',}),{ status: 200 },);

    const { getByRole,findByText } = render(
      <AccountInfo />
    );

    const importButton = getByRole('button',{ name: 'Import' });
    fireEvent.mouseEnter(importButton);

    expect(await findByText(/loading data/)).toBeInTheDocument();

    await waitFor(() =>
      expect(getAccountInfoSpy).toHaveBeenCalledWith(testId),);

    expect(findByText(/cannot use this feature/).toBeInTheDocument();
  });
});

这是 AccountInfo 组件

const AccountInfo = () => {
  const { customerId } = useCustomerContext();
  const [isLoading,setIsLoading] = useState(true);
  const [accountInfo,setAccountInfo] = useState({});
  const [isAccountInfoLoading,setIsAccountInfoLoading] = useState(false);
  
  useEffect(() => {
    const fetchAccountData = async () => {
      const rawRes = await fetch(`/customer/${customerId}`);
      const res = await rawRes.json();
      setIsLoading(false);
      
      if (res.orgId) {
        setIsAccountInfoLoading(true);
        try {
          const accountInfoRes = await getAccountInfo(res.orgId);
          setAccountInfo(accountInfoRes.accountInfo);
        } catch (error) {
          console.log('error',error);
        } finally {
          setIsAccountInfoLoading(false);
        }
      }
    }
    fetchAccountData();
  });
  
  function renderTooltip() {
    if (isAccountInfoLoading) {
      return 'loading data';
    } else {
      return !accountInfo.active
        ? 'cannot use this feature'
        : undefined;
    }
  }
  
  return (
    <>
      <Button tooltip={renderTooltip()}>
        Import
      </Button>
    </>
  );
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?