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

282
Vistas
Jest test react hook method to get better code coverage

I have a component, that sets state based on the onClick method as follows:

const [active, setActive] = React.useState(0);

<button type="button" onClick={() => {setActive(() => index); inner.fc(index)}} key={inner.id}>{inner.title}</button>

The test itself is:

it('Should test setActive index of button', (): void => {
    const setIndex = jest.fn(() => 1);
    let tree = create(<button type="button" onClick={setIndex} key='1'>1 button</button>);
    const button = tree.root.findByType('button')
    button.props.onClick()
    expect(setIndex()).toBe(1);
});

This passes but when I look at the coverage report, it flags the setActive method as not covered? How do I test that React.useState method correctly?

about 4 years ago · Juan Pablo Isaza
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