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

451
Views
cómo hacer una prueba unitaria de componentes de representación condicional de reacción

Empecé a aprender pruebas unitarias en React. Estoy implementando una llamada API simple que representa el nombre de la persona almacenado dentro del estado de datos.

 import React,{useState,useEffect} from 'react' import {Props} from '../../Props/Person.props' import {REACT_APP_CDA_TOKEN,REACT_APP_SPACE_ID,query} from '../../Constants/Query/Person' export const NameComp = () => { const [data,setData]=useState<Props | null>(null); useEffect(()=>{ window.fetch( `https://graphql.contentful.com/content/v1/spaces/${REACT_APP_SPACE_ID}`, { method:"POST", headers:{ 'content-type':'application/json', Authorization:`Bearer ${REACT_APP_CDA_TOKEN}` }, body:JSON.stringify({query}), }).then((response)=>response.json()) .then((json)=>setData(json.data)) },[]) return ( !data ? <div>Loading...</div>: <div> <p className='name'>{data.person.name}</p> </div> ) }

Estoy probando la unidad con el siguiente código.

 import React from 'react' import {shallow} from 'enzyme'; import { NameComp } from '../components/NameComp/NameComp'; describe('Renders Name Component', () => { let container:any; beforeEach(() => { container= shallow(<NameComp/>) }) it('renders a div',() => { expect(container.find('div').length).toEqual(1); }) it('renders a paragraph for name',() => { expect(container.contains('p')).toEqual(true) }) })

Pero el caso final está fallando porque el div de carga se procesa cuando los datos son nulos. Cómo hacer pruebas de tal escenario.

Además, ¿cuál es la mejor manera de aprender pruebas unitarias en React?

about 4 years ago · Juan Pablo Isaza
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!