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

363
Views
¿Por qué no se procesan los componentes secundarios cuando se prueba con React Testing Library?

Estoy escribiendo pruebas unitarias de UI usando Jest/Testing Library.

Tengo un componente DashboardLayout que devuelve

 return ( <DashboardLayoutRoot sx={{ marginTop: '30px' }}> <DashboardNavbar onSidebarMobileOpen={(): void => setIsSidebarMobileOpen(true)} /> <DashboardSidebar onMobileClose={(): void => setIsSidebarMobileOpen(false)} openMobile={isSidebarMobileOpen} /> <DashboardLayoutWrapper> <DashboardLayoutContainer> <DashboardLayoutContent>{children}</DashboardLayoutContent> </DashboardLayoutContainer> </DashboardLayoutWrapper> </DashboardLayoutRoot> );

DashboardNavBar:

 <DashboardNavbarRoot {...other}> <Toolbar sx={{ minHeight: 64 }}> <Hidden lgUp> <IconButton color="inherit" onClick={onSidebarMobileOpen}> <MenuIcon fontSize="small" data-testid="sidebar-icon" /> </IconButton> </Hidden>

Como puede ver, configuré testid en MenuIcon.

Sin embargo, al renderizar en prueba, getByTestId falla.

 // skip setup above render( <Provider store={store}> <DashboardLayout /> </Provider>, ); const sidebarIcon = screen.getByTestId('sidebar-icon'); expect(sidebarIcon).toBeVisible();

Configuré todo correctamente, como enrutador simulado, proveedor, etc.

¿Por qué está pasando esto?

¿Debería representar DashboardNavbar solo? (aunque no estoy seguro de si esto sería una buena prueba de unidad de interfaz de usuario)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Es posible que deba simular el componente secundario y probar el componente simulado

 // skip setup above jest.mock('./MenuIcon', () => ({ MenuIcon : () => 'mocked menu icon' })); const container = render( <Provider store={store}> <DashboardLayout /> </Provider>, ); expect(container).toMatchInlineSnapshot(` <div> mocked menu icon </div> `);
about 4 years ago · Juan Pablo Isaza Report
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!