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

334
Views
Valor de retorno simulado para useRef pasado como argumento

Tengo la siguiente función que toma React.useRef() como argumento.

Tengo problemas para probarlo donde estoy tratando de obtener el useRef para mantener valores y poder removeChild() .

Cuando ejecuto la prueba, recibo el siguiente error.

TypeError: useRef.current.removeChild no es una función

Esto es porque estoy pasando un objeto en lugar de una función de gancho useRef durante la prueba.

¿Cómo puedo pasar un useRef simulado y poder pasar el valor y realizar la operación removeChild durante la prueba?

Esta es la implementación.

 export const postData = ( useRef, // this useRef is coming from the calling component which is actually React.useRef() token, id ) => { useRef.current.children[0].value = token; useRef.current.children[1].value = id; [...useRef.current.children].forEach((child) => { if (!child.value) { useRef.current.removeChild(child); } }); };

Esta es la prueba que falla con el error anterior.

 it('should work', () => { const formRef = { current: { action: '', submit: jest.fn(), children: [ { value: '', }, { value: '', }, ], }, }; postData(formRef, 'mock_token', 'mock_id'); });

Como se mencionó, falla debido a pasar un objeto en lugar de useRef durante la prueba.

Intenté la siguiente prueba en su lugar para abordar esto.

La siguiente prueba arroja este error.

TypeError: no se puede leer la propiedad 'hijos' de undefined

 import React, { useRef } from 'react'; jest.mock('react', () => { const originReact = jest.requireActual('react'); return { ...originReact, useRef: jest.fn(), }; }); it('should work', () => { const formRef = { current: { action: '', submit: jest.fn(), children: [ { value: '', }, { value: '', }, ], }, }; useRef.mockReturnValueOnce(formRef); postData(useRef, 'mock_token', 'mock_id'); });
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!