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

98
Views
reaccionar biblioteca de prueba toHaveBeenCalled 0 burlándose de un simple accesorio de clic

Intento simular un accesorio llamado actionClicked pero mi prueba falló, tengo un componente simple como el siguiente

 const ButtonAction: React.FC = ({ actionClicked }) => { const handleClick = (action) => () => actionClicked(action) return ( <div data-testid="some-action" onClick={handleClick('something')} > <Icon /> </div> ) }

esta es mi prueba, button.spec.tsx

 import { render, screen } from '@testing-library/react' import userEvent from '@testing-library/user-event' import React from 'react' import ButtonAction from './ButtonAction' test('render ButtonAction', () => { const actionClicked = jest.fn() render(<ButtonAction actionClicked={actionClicked} />) userEvent.click(screen.getByTestId('some-action')) expect(actionClicked).toHaveBeenCalled() //failed here })

No estoy seguro de por qué no se ha llamado a actionClicked .

 Expected number of calls: >= 1 Received number of calls: 0

¿Alguna idea?

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

0

Este comportamiento se produce porque userEvent.click devuelve una promesa.

Primero debe convertir su prueba en async y luego await userEvent.click :

 test("render ButtonAction", async () => { const actionClicked = jest.fn(); render(<ButtonAction actionClicked={actionClicked} />); await userEvent.click(screen.getByTestId("some-action")); expect(actionClicked).toHaveBeenCalledTimes(1); });
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!