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

120
Visualizações
How to test button click on jest/enzyme without props

I want to test is the button clicked and some actions happens but I fail the test and receive 0 number of calls when clicked. Im not passing onclick function from props. I'm using materialUi Button

const LoginFunction = () => {

  const handleClick = () => {
    console.log('some action here')
  };

  return (
    <Button
        data-test="btn"
        className='btn'
        onClick={() => {
            handleClick();
        }}
    >
       Some text
    </Button>
 )};

I wrote test like this

describe('Test Login component', () => {

  it('should trigger click event', () => {
    const mockCallBack = jest.fn();
    const wrapper = shallow(<LoginFunction />);

    wrapper.simulate('click');
    expect(mockCallBack.mock.calls.length).toBe(1);
   }); 
 });

Tried this way too

 describe('Test Login component', () => {

  it('should trigger click event', () => {
    const mockCallBack = jest.fn();
    const wrapper = shallow(<LoginFunction />);

    wrapper.find('.btn').simulate('click');
    expect(mockCallBack.mock.calls.length).toBe(1);
   }); 
 });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

**Also in the main component you can directly call the handler in onclick like this** 

     <Button
            data-test="btn"
            className='btn'
            onClick={handleClick}
        >
           Some text
        </Button>

describe('Test Login component', () => {

  it('should trigger click event', () => {
  const wrapper=mount( <LoginFunction />);
    const result=wrapper.find('button').simulate('click')
   expect(result).toBe('some action here')
 });
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