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

165
Views
Cómo escribir un caso de prueba de unidad usando JEST si el texto se muestra condicionalmente con useState

muestra.js

 const Sample = () => { const [show, setShow] = useState(false); const onShow = () => { setShow(!show); } return( <div> <button onClick={onShow}>{show ? 'Hide Text' : 'Show text'}</button> {show && <label>Welcome!!</label>} </div> ); }; export default Sample;

Muestra.prueba.js

 import Sample from '../components/sample'; import React from "react"; test('render Sample text matcher', () => { render(<Sample />); const titleElement = screen.getByText(/Welcome!!/) expect(titleElement).toBeInTheDocument; })

Lanza el siguiente error. ¿Cómo puedo escribir testcase si el elemento se representa con valor de estado?

 TestingLibraryElementError: Unable to find an element with the text: /Welcome!!/. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Como solución alternativa, justo después de renderizar, puede intentar hacer clic en el botón que activa su pantalla:

 document.querySelector('button').click();
about 4 years ago · Juan Pablo Isaza Report

0

Primero debe hacer clic en su botón. De lo contrario show es falso y el texto no se muestra.

Puedes usar:

 const button = screen.getByRole('button'); fireEvent.click(button); // import this from react-testing-library
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!