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

243
Visualizações
How to test inner functions defined on Stateless Components in React with Enzyme

I'm using enzyme to test my react components. I have a stateless component that has an inner function. How can i call and test that inner function?

Here is my component:

const Btn = (props) => {
  const types = ['link', 'plainLink', 'primary', 'secondary', 'danger', 'info'];

  const handleClick = (event) => {
    event.preventDefault();
    props.onClick(event);
  };

  return (
    <button onClick={handleClick} className={classes}>
      <span>{props.children}</span>
    </button>
  );
};

I've tried the following, but get an error saying: TypeError: undefined is not a constructor

const btnComp = shallow(<Btn />);
btnComp.instance().handleClick();
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I usually test this functionality by setting a sinon.spy for the event:

const click = sinon.spy();
const btnComp = shallow(<Btn onClick={click} />);

btnComp.find('button').simulate('click');

expect(click.called).to.equal(true);

Now, you know that the inner function did indeed invoke the props.onClick event, which is the most important bit of its work.

over 4 years ago · Santiago Trujillo 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