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

320
Views
React Testing Library jest.fn() toHaveBeenCalled() no funciona

Componente:

 const MyComp = ({ handler }) => { return <button onClick={handler}>Test</button> };

Prueba:

 it('Calls the handler', async () => { const handler = jest.fn(); render(<MyComp handler={handler} />); const button = screen.getByRole('button', { name: /Test/i }); await fireEvent(button, new MouseEvent('click')); expect(handler).toHaveBeenCalled(); });

Número esperado de llamadas: >= 1 Número de llamadas recibidas: 0

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

0

Intenta usar userEvent.click , así

 it('Calls the handler', async () => { const handler = jest.fn(); render(<MyComp handler={handler} />); const button = screen.getByRole('button', { name: /Test/i }); await userEvent.click(button); expect(handler).toHaveBeenCalled(); });
about 4 years ago · Juan Pablo Isaza Report

0

Tres opciones:

  • Haga que el evento burbujee, como se muestra en el ejemplo (no devuelve una promesa, no se necesita await ):

     fireEvent(button, new MouseEvent("click", { bubbles: true }));
  • Use el método de conveniencia , que agrega propiedades de eventos predeterminadas, incluido el burbujeo (del mismo modo):

     fireEvent.click(button);
  • Use userEvent (devuelve una promesa , a partir de v14):

     await userEvent.click(button);
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!