Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

308
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda