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

134
Visualizações
React testing library - how to populate values in a state to test the component?

My component ->

export default()=>{
const [list, setList] = useState([])

 const handleAddToList = () => {
//makes an api call and sets the list state.
setList(response);
}
return (

<div>
  <Button onClick={hanldeAddToList}>add to list</Button>
{
list.map(row=><span>{row.name} </span>)
}
</div>
)

}

Problem =>

how to test this component using React testing library with mock list data, the problem is "list" is a state here and I cant test it using react testing library because it doesn't focus on the implementation and therefore I cant populate list state.

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

0

Like @matt-morgan said, you have to mock your api call. I would suggest using Mock Service Worker.

React Testing Library allows you to write tests against your UI in exactly the same was as a user does. Your tests will consist of clicking on buttons and filling in fields and clicking on links. If your component makes async calls for data, you have to mock those calls so that you UI has data to work with.

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