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

254
Visualizações
Simulate click on an element when using element.eventlistener in jest + enzyme set up

I have a component lets say <MyComponent>. I have a <button>Click me</button> in the component which has a event listener on click added in useEffect

useEffect(() => {
    const btn = document.getElementById('button');
    if (btn) {
      btn.addEventListener('click', onButtonClicked, false);
    }
    return function cleanupListener() {
      if (btn) btn.removeEventListener('click', onButtonClicked);
    };
  }, []);

I need to simulate click on that button in test case using jest + enzyme. Went through one of the answer which had a solution like this but it didn't work

beforeEach(() => {
  const map = {};
  const namedByRefComponent = Wrapper.find("button");
  const namedByRefDomNode = namedByRefComponent.getDOMNode();
  const namedByRefDomNode = ReactDOM.findDOMNode(namedByRefComponent);
  namedByRefDomNode.addEventListener = jest
    .fn()
    .mockImplementation((event, cb) => {
      map[event] = cb;
    });
});

Can someone help me out with new solution or complete the above solution because I am not able to figure it out

Thanks in advance

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