Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

135
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda