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

446
Views
¿Cómo probar TextInput en react-native con @testing-library/reat-native?

Estoy tratando de probar un TextInput verificando su valor después de ingresar una cadena, pero no funciona. Cualquier ayuda sería apreciada.

Aquí está el código:

 import React from 'react' import { TextInput } from 'react-native' import { fireEvent, render } from "@testing-library/react-native" test('<TextInput/>, () => { const { getByTestId } = render( <TextInput testID="input" /> ); const input = getByTestId("input"); fireEvent.changeText(input, "123"); expect(input.value).toBe("123"); })

La prueba falla con el mensaje:

 Expected: "123" Received: undefined
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Creo que su ejemplo no funciona porque no está controlando la entrada. Intente agregar un value y una función onChangeText . Algo como:

 function Example() { const [value, setValue] = useState('') return <TextInput value={value} onChangeText={setValue} testID="input" /> } test('Should apply the value when changing text', () => { const { getByTestId } = render(<Exampler />); const input = getByTestId('input'); fireEvent.changeText(input, '123'); expect(input.props.value).toBe('123'); });

Además, debe verificar input.props.value en lugar de input.value Espero que ayude.

about 4 years ago · Santiago Trujillo Report

0

Voy a sugerir algunas cosas para ayudarlo a llegar a la solución,

  • ¿Estás seguro de que TextInput tiene un accesorio llamado testID ? Utiliza getByTestId posteriormente, que necesita un valor de data-testid en el elemento. Así que por favor asegúrese de esto primero.
  • Puede intentar encontrar el elemento de otra manera. Probablemente use getByPlaceholderText o similar.

Una vez que pueda obtener el elemento correctamente, y después de fireEvent , el valor ciertamente debería actualizarse y la aserción tendría éxito.

about 4 years ago · Santiago Trujillo 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!