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

439
Views
How to test TextInput in react-native with @testing-library/reat-native?

I'm trying to test a TextInput by checking its value after entering a string, but it's not working. Any help would be appreciated.

Here's the code:

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");
})

The test fails with the message:

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

0

I think your example is not working because you are not controlling the input. Try adding a value and an onChangeText function. Something like:

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');
});

Also, you need to check input.props.value instead of input.value Hope it helps.

about 4 years ago · Santiago Trujillo Report

0

I'm going to suggest a few things to assist you to get to the solution,

  • Are you sure TextInput has a prop called testID? You use getByTestId subsequently, which needs a data-testid value on the element. So please make sure of this first.
  • You can try finding the element in another way. Probably use getByPlaceholderText or similar.

Once you can get the element properly, and after the fireEvent, the value should certainly be updated, and assertion would succeed.

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!