Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!