Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!