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

347
Views
Cómo verificar el valor de useRef.current.selectionStart en la prueba de broma

Necesito probar los valores de useRef.current.selectionStart y useRef.current.selectionEnd una vez que se hayan cambiado en una interacción onKeyDown con una entrada.

índice.tsx

 import React, { useRef, useEffect } from 'react' type Props { value: string ...aBunchOfProps } const SomeComponent: FC<Props> = ({ value, ...aBunchOfProps }) => { const inputRef = useRef(null) const [caretPosition, setCaretPosition] = useState<CaretPosition | undefined>() useEffect(() => { if (!!caretPosition && !!value) { let newCaretPosition = caretPosition.currentPosition const shouldMoveCaretForward = caretPosition.direction === 1 && value.charAt(caretPosition.currentPosition) === '/' if (shouldMoveCaretForward) { newCaretPosition++ } inputRef.current.selectionStart = newCaretPosition <=== this is the line I want to test inputRef.current.selectionEnd = newCaretPosition <=== this is the line I want to test } }, [caretPosition]) const someFunction = () => { // calls setCaretPosition with new details } return ( ...someAdditionalCode <input ...someAdditionalProps ref={inputRef} value={value} data-testid="input-field" onKeyDown={() => someFuction()} /> ...evenMoreCode ) } export default SomeComponent

índice.prueba.tsx

 describe('SomeComponent tests', () => { it('should move cursor correctly', () => { const { getByTestId } = render(<SomeComonent value="12/3" />) const input = getByTestId('input-field') fireEvent.keyDown(input, { key: '4' }) // expect(useRef.current.selectionStart).toEqual(5) <==== I want something like this // expect(useRef.current.selectionEnd).toEqual(5) <==== I want something like this }) })

Cualquier sugerencia sería útil.

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

0

Había revisado tu código. Puede que no sea posible verificar useRef en el archivo testcase. verifique la imagen que se muestra a continuación [![ingrese la descripción de la imagen aquí y también comparta el enlace del documento, para que pueda ser útil para usted.

enlace del documento: https://testing-library.com/docs/guiding-principles/

ingrese la descripción de la imagen aquí

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!