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

289
Views
Broma: simular la pulsación de una tecla no funciona en el gancho React

Tengo una función simple de disparo de gancho en la tecla:

 useEffect(() => { const handleUp = (event: KeyboardEvent) => { const { key: releasedKey } = event if (key === releasedKey) { if (onKeyUp) { onKeyUp() } } } window.addEventListener('keyup', handleUp) return () => { window.removeEventListener('keyup', handleUp) } }, [key, onKeyUp]) }

Quiero probarlo usando Enzyme:

 import { shallow } from 'enzyme' import React from 'react' import useKeyUp from 'utils/hooks/useKeyUp' const TestComponent = ({key, callback}) => { useKeyUp(key, callback) return ( <div> test </div> ) } test('executes handler on Enter key up', () => { const callback = jest.fn() const testComponent = shallow( <TestComponent key={'Enter'} callback={callback} />) testComponent.simulate('keypress', { charCode: 13 }) expect(callback).toHaveBeenCalledTimes(1)

Pero no se ha llamado a la función de devolución de llamada. Sé que el gancho está funcionando porque lo probé manualmente. ¿Cómo puedo simular correctamente el evento?

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!