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

123
Visualizações
Jest test function call inside useEffect

I have the following test:

  it("should call runValidation when listen_to is set", async () => {
    const updatedProps = {
      item: {
        ...props.item,
        attributes: {
          ...props.item.attributes,
        },
      },
      validation: {
        ...props.validation,
        setValue: jest.fn(),
        runValidation: jest.fn(),
        listenFieldValues: jest.fn(),
      },
    };

    const spyOnValidation = jest.spyOn(
      updatedProps.validation,
      "runValidation"
    );

    render(<Average item={updatedProps.item} validation={props.validation} />);

    expect(spyOnValidation).toHaveBeenCalled();
  });

and this is the UI related to it:

export const Average = ({ item, validation }: ComponentProps): JSX.Element => {
  const { attributes, name } = item;

  const hide_cents = attributes?.hide_cents;
  const [price, setPrice] = useState<number>(Number(item.value || 0));

  useEffect(() => {
    if (attributes?.listen_to) {
      console.log("validation: ", validation);
      setupListenData({ item, validation, callback: setPrice });
      validation.runValidation();
    }
  }, [item, attributes, validation]);
...

I am trying to test that validation.runValidation() is being called inside useEffect but the test is failing: number of calls 0

I have also tried to use waitFor but the test passes regardless of how many times haveBeenCalledTimes(3) or haveBeenCalledTimes(100)

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

0

Have you tried to create a mock function within this test, then assign the mock function to updatedProps.validation.runValidation, then you just expect the mock function is called?

  it("should call runValidation when listen_to is set", () => {
    const mockRunValidation = jest.fn();
    const updatedProps = {
      item: {
        ...props.item,
        attributes: {
          ...props.item.attributes,
        },
      },
      validation: {
        ...props.validation,
        setValue: jest.fn(),
        runValidation: mockRunValidation,
        listenFieldValues: jest.fn(),
      },
    };
    render(<Average item={updatedProps.item} validation={props.validation} />);

    expect(mockRunValidation).toHaveBeenCalled();
  });
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