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

133
Vistas
How to test containers/divs in react-testing-library

For example:

I want to test if my custom CSS logo will render. I am trying to avoid using 'data-testid' attribute (because is supposedly not recommended).

Logo component:

 const Logo = ({ size }) => (
  <div className={styles.logoContainer}>
    <span className={size === 'small' ? `${styles.oSmall}` : `${styles.oBig}`} />
    <span className={size === 'small' ? `${styles.lSmall}` : `${styles.lBig}`} />
    <span className={size === 'small' ? `${styles.xSmall}` : `${styles.xBig}`} />
  </div>
);

Part of another component when 'Logo' component is rendered (I want to test it here if the logo component will render in this component):

 <Container>
   <a title="anchor title" href="http://www.olx.pl" className="rel">
      <Logo color="turquoise" size="small" />
   </a>
 </Container>

Test:

it('should render logo', () => {
   render(<ComponentWithLogo />);
   expect(
     screen.getByRole('link', { name: /anchor title/i })
    ).toBeInTheDocument();
});

I guess that this is not best way to test if a logo will render, because just change the child of this anchor tag and the test will still pass. Would it be better to use data-testid (to a wrapping container) in this case? I've noticed that this problem occurs often (mostly when I want to test if a component container has been rendered). Is it really bad to use data-testid in such cases?

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