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

399
Views
How to write jest test case for addEventListener and DOM for vanilla javascript

Here is app.js file, I believe, I have to use spyon and jest.fn but not sure how to do that. Also I'm not sure how to show expected dom because it is throwing this error "Support for the experimental syntax 'jsx' isn't currently enabled" as of now, for below code, I'm getting this error

enter image description here

 button.addEventListener("click", display);
    
    function display() {
        displayTable.style.display = "block"
        obj.move.push(input.value);
        let bulldozerMove = obj.move[obj.move.length - 1];
        let index = obj.initPos[obj.initPos.length - 1];
        let direction = obj.initDir[obj.initDir.length - 1];
        solution(
            grid,
            index,
            direction,
            bulldozerMove
        );
        let newRow = displayTable.insertRow(-1);
        key.forEach((val) => {
            let insertData = obj[val];
            for (let i = 0; i < 1; i++) {
                let newCell = document.createElement("td");
                newCell.style.border = 'solid 1px black';
                newCell.appendChild(document.createTextNode(insertData[insertData.length - 1]));
                newRow.appendChild(newCell);
            }
            tableBody.appendChild(newRow);
        })
        displayTable.appendChild(tableBody);
        document.body.appendChild(displayTable);
    }

Here is app.test.js file

    describe('addEventListener', () => {
test('call callback function', () => {
  const display = jest.fn();
  button.addEventListener('click', display);
  expect(display).toHaveBeenCalled(1);
});

})

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