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

304
Views
Unable to find an element with the placeholder text "Search..." reactjs jest

I have a component that uses async and await.

I am trying to test whether the placeholder for the input renders.

However, I always get the above error.

This is component:

const [Loading, setLoading] = useState(false)
const [name, setName] = useState('');
const [options, setOptions] = useState([])
const [data, setData] = useState(false)

useEffect(() => {
  setLoading(true)
  const newUsers = async() => {
    const response = await fetch('https://jsonplaceholder.typicode.com/users')
      .then((res) => res.json())
      .then((data) => {
        setData(data)
      });

  };
  newUsers();
}, [])

if (!data) return <p className = 'text-center font-bold text-6xl' > Loading... < /p>
if (!Loading) return <p className = 'text-center font-bold text-6xl' > Data loaded < /p>

<input id = "input"
className = 'w-96  h-16 border-1 shadow-lg rounded-lg'
placeholder = "Search..."

onChange = {
  e => handleChange(e.target.value)
}
value = {
  name
}
/>
   

This is the test

test('input appears', async () => {
    await waitFor(() => {
        expect(screen.getByPlaceholderText('Search...')).toBeInTheDocument()
      })
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Looks like you forgot to return the input

const [Loading, setLoading] = useState(false)
const [name, setName] = useState('');
const [options, setOptions] = useState([])
const [data, setData] = useState(false)

useEffect(() => {
  setLoading(true)
  const newUsers = async() => {
    const response = await fetch('https://jsonplaceholder.typicode.com/users')
      .then((res) => res.json())
      .then((data) => {
        setData(data)
      });

  };
  newUsers();
}, [])

if (!data) return <p className = 'text-center font-bold text-6xl' > Loading... < /p>
if (!Loading) return <p className = 'text-center font-bold text-6xl' > Data loaded < /p>

return (    // <---- Return
  <input id = "input"
    className = 'w-96  h-16 border-1 shadow-lg rounded-lg'
    placeholder = "Search..."

    onChange = {
     e => handleChange(e.target.value)
    }
    value = {
     name
    }
  />
);
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!