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

137
Visualizações
React Testing with API call - how to effectively render the component with results of GET request?

I am trying to mock my React functional component and test if the results of an API call are being rendered properly. I can see on the page that everything is working, but I've been asked to write a test for "business logic".

The page gets an array of objects from the api, then maps over them to generate Post-Previews.

The API call happens in the first render (in useEffect), and the first six results are visible. There are six elements with the class name "Post-Preview" on the page at that point.

My first question is: What does "business logic" mean in the context of a React App? My second question is: How can I make a test which actually renders the page in response to the API call? The dummyData here is an array of six objects, copied directly from the real API.

test("Renders the page", async () => {
    act(() => {
        mockedAxios.get.mockResolvedValue({ data: dummyData });
        ReactDOM.render(<App />, container);
    });
    expect(container.innerHTML).toContain("climate");     // This test passes

});

test("Renders the results of the API call", async () => {
    mockedAxios.get.mockResolvedValue({ data: dummyData });
    act(() => {
        ReactDOM.render(<App />, container);
    });
    const postPreview = container.getElementsByClassName("Post-Preview");
    console.log(postPreview);                             // Returns empty HTMLCollection
});
about 4 years ago · Juan Pablo Isaza
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