javascript - 在自定义钩子(Hook)中测试 fetch.catch

标签 javascript typescript jestjs jest-fetch-mock react-hooks-testing-library

我有这个自定义钩子(Hook):

import React from 'react';
import { useMessageError } from 'components/Message/UseMessage';

export interface Country {
  code: string;
  name: string;
}

export default function useCountry(): Array<Country> {
  const [countries, setCountries] = React.useState<Country[]>([]);
  const { showErrorMessage } = useMessageError();

  React.useEffect(() => {
    fetch('/api/countries', {
      method: 'GET',
    })
      .then(data => data.json())
      .then(function(data) {
        // ..
      })
      .catch(() => showErrorMessage());
  }, []);

  return countries;
}

如果会有无效响应,我想测试捕获错误。这样,由于showErrorMessage(),应该会出现错误消息.我有这个测试:

const showErrorMessage = jest.fn();

jest.mock('components/Message/UseMessage', () => ({
  useMessageError: () => ({
    showErrorMessage: showErrorMessage,
  }),
}));

import useCountry from 'components/Country/useCountry';
import { renderHook } from '@testing-library/react-hooks';
import { enableFetchMocks } from 'jest-fetch-mock';
enableFetchMocks();

describe('The useCountry hook', () => {
  it('should show error message', async () => {
    jest.spyOn(global, 'fetch').mockImplementation(() =>
      Promise.resolve({
        json: () => Promise.reject(),
      } as Response),
    );

    const { result, waitForNextUpdate } = renderHook(() => useCountry());
    await waitForNextUpdate();

    expect(fetch).toHaveBeenCalled();
    expect(showErrorMessage).toHaveBeenCalled();
    expect(result.current).toEqual([]);
  });
});


但是,我收到了一个错误:

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error



我在这里做错了什么?我认为它与 await waitForNextUpdate(); 有某种关系。 ,但我真的不确定以及如何管理它。

最佳答案

waitForNextUpdate()等待下一次更新,但你的钩子(Hook)不会触发它,因为它只调用 showErrorMessage() .看看this sandbox
作为一个简单的解决方案,可以添加一些触发更新的内容:

  React.useEffect(() => {
    fetch('/api/countries', {
      method: 'GET',
    })
      .then(data => data.json())
      .then(function(data) {
        // ..
      })
      .catch(() => { 
        showErrorMessage();
        // trigger update in any suitable way, for example:
        setCountries([]); 
      });
  }, []);
但以某种方式重构它可能会更好。例如,您可以对错误使用单独的钩子(Hook)和状态:
export default function useCountry(): Array<Country> {
  const [countries, setCountries] = React.useState<Country[]>([]);
  const [error, setError] = React.useState(null);
  const { showErrorMessage } = useMessageError();

  React.useEffect(() => {
    fetch('/api/countries', {
      method: 'GET',
    })
      .then(data => data.json())
      .then(function(data) {
        // ..
      })
      .catch(() => setError(true));
  }, []);
  
  React.useEffect(() => {
    if (error) {
      showErrorMessage()
    }
  }, [error]);

  return countries;
}

关于javascript - 在自定义钩子(Hook)中测试 fetch.catch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60917322/

相关文章:

Angular4/子元素不触发父(点击)事件

typescript - 如何将任何字符串选项添加到 typescript 文字字符串枚举?

angular - RxJS:将多个 http 请求组合成单个、扁平的可观察数组

javascript - 显示来自 fetch API 调用 Node/React 的图像

javascript - 使用 fs 模块了解 Node JS 生成器

javascript - bootstrap-select 下拉菜单被另一个下拉菜单隐藏

reactjs - 当我在 react 中对此 componentDidMount 运行测试时,显然有几行没有被覆盖?

javascript - 嵌套 JS 同一个提供者的两个实例

javascript - react 和 Jest : Cannot find module from test file

javascript - 如何在javascript闭包调用中生成动态字符串