Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

231
Vistas
How Can I test a react component based on props value

I have this component that is responsible of showing an update message

import React from 'react';
import { Typography } from '@material-ui/core';
import { DateTime } from 'luxon';

const LastUpdatedTime = ({ recordedOn }) => {
  if (!recordedOn) {
    return <></>;
  }
  const date = DateTime.fromMillis(recordedOn).toLocaleString();
  return (
    <>
      <Typography data-testid="caption" style={{ color: '#757575' }} variant="caption">
        {`Last recorded on`}
      </Typography>
      <Typography data-testid="date" style={{ color: '#757575' }} variant="body2">
        {`${date}`}
      </Typography>
    </>
  );
};

export default LastUpdatedTime;

I want to test this component based on props value which is recordedOn and my tests so far are like this

import React from 'react';
import { render } from '@testing-library/react';
import LastUpdatedTime from './index'

describe('<LastUpdatedTime />' ,()=>{

    it('should render the date message',()=>{
        const { getByTestId} = render(<LastUpdatedTime recordedOn={1645109616314} />)
        const captionEl = getByTestId("caption")
        expect(captionEl.textContent).toBe('Last recorded on')
    })
    it('should render the date provided from the component',()=>{
        const {getByTestId} = render(<LastUpdatedTime recordedOn={1645109616314} />)
        const dateEl = getByTestId("date")
        expect(dateEl.textContent).toBeTruthy()
    })
    it('should render nothing if there was no recordedOn time provided' ,()=>{
        const {getByTestId} =render(<LastUpdatedTime recordedOn={1645109616314} />)
        const dateEl = getByTestId("date")
        expect(dateEl).toBeNull()
    })
})

My problem is in the third test should render nothing if there was no recordedOn time provided

How to do that?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda