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

104
Visualizações
Click event not firing in JSDOM

I have a small app created in Vanilla JavaScript. To test the app, I am using Jest and JSDOM. I have a button in the app which deletes a table in row. The test looks like:

it('Table deletes corresponding row when Delete is clicked', () => {
    const tableCells = document.querySelectorAll('table tbody tr td');
    expect(tableCells[1].innerHTML).toBe('Some value');
    const firstRowDeleteButton = document.querySelectorAll('table tbody tr button')[1];
    firstRowDeleteButton.click();
    expect(tableCells[1].innerHTML).toBe('Another value'); // row value should change after original row is deleted.
});

Above test fails as Jest receives the same 'Some value' after at the end of test too. So the delete button is not clicking (be assured that delete functionality is working fine as I tested it in the actual app). I even tried simulating click event like this:

const evt = document.createEvent("HTMLEvents");
evt.initEvent("input", false, true);
element.dispatchEvent(evt);

But this didn't work either. So how can I trigger the click event in JSDOM?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Yes, that is a JSDOM limitations, you should use something like https://testing-library.com/docs/ecosystem-user-event

...
userEvent.click(firstRowDeleteButton)
...

NOTE: examples on this page includes React but user-event works fine apart

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