• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

321
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.

almost 3 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í

almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error