Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

92
Visualizações
How to test a React asynchronous custom hook that returns a boolean when an API call is resolved/rejected

I have a custom React hook as follows:

export function useValidPolicyReference({
  token,
  tenant,
  policyReference,
}: UseValidPolicyReferenceProps): Record<string, boolean> {
  const [isValid, setIsValid] = React.useState<boolean>(true);

  React.useEffect(() => {
    async function fetchOnMount(): Promise<void> {
      try {
        await getPolicyByReference({
          policyReference: policyReference as string,
          tenant,
          token,
        });
      } catch (error) {
        setIsValid(false);
      }
    }
    if (tenant !== '' && policyReference !== null && token !== '') {
      fetchOnMount();
    }
  }, [policyReference, tenant, token]);

  return {
    isValidPolicyReference: isValid,
  };

I want to test that isValidPolicyReference will return true when getPolicyByReference does not throw and false when it does.

My test for the true case is as follows:

  test('should be truthy if the policy reference is valid', () => {
    (getPolicyByReference as jest.Mock).mockImplementationOnce(() =>
      Promise.resolve(true)
    );
    const {
      result: { current },
      waitForNextUpdate,
    } = render();

    expect(current.isValidPolicyReference).toBeTruthy();

    waitForNextUpdate();

    expect(getPolicyByReference).toHaveBeenCalledWith({
      token,
      tenant,
      policyReference,
    });
    expect(current.isValidPolicyReference).toBeTruthy();
  });

Which passes but the test for the false case does not:

  test('should be falsy if the policy reference is invalid', () => {
    (getPolicyByReference as jest.Mock).mockImplementationOnce(() =>
      Promise.reject(new Error('policy reference error'))
    );
    const {
      result: { current },
      waitForNextUpdate,
    } = render();

    expect(current.isValidPolicyReference).toBeTruthy();

    waitForNextUpdate();

    expect(current.isValidPolicyReference).toBeFalsy();
  });

It fails on two counts:

  1. isValidPolicyReference never returns as false
  2. There is also a console.error logged at this line setIsValid(false); where Warning: Can't perform a React state update on an unmounted component.

I am not sure how to get getPolicyByReference to throw in Jest so that the hook will return the false value.

What am I not doing correctly? Should I be wrapping my test cases in async await? Should I mock axios.get instead of getPolicyByReference?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is also a console.error logged at this line setIsValid(false); where Warning: Can't perform a React state update on an unmounted component.

I think the issue is you are trying to test the hook alone with jest, when hooks can only be called from whithin components.

The react testing library has really nice helpers to simplify testing hooks, this article is quite informative on the matter.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda