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

212
Views
mock ontouchstart event in window object jest

I'm trying to mock ontouchstart event in window object to make some tests, but i can't find a proper way to do it

export const main = () =>
  !!('ontouchstart' in window || navigator.maxTouchPoints);

I try to do

 it('123', () => {
    const spyWindowOpen = jest.spyOn(window, 'ontouchstart');
    spyWindowOpen.mockImplementation(jest.fn());
  });

but ontouchstart does not seem exist on window object in my compilation tests

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It's ok i do that :

describe('support ontouchstart', () => {
  it('return true when window support ontouchstart event', () => {
    // eslint-disable-next-line no-global-assign
    window = {
      ontouchstart: jest.fn(),
    };

    expect(!!('ontouchstart' in window)).toBe(true);
  });
});
})
about 4 years ago · Juan Pablo Isaza Report

0

Please make sure to reset to window in original position back like below.

it('should render', () => {
const original = window.ontouchstart;
window.ontouchstart = jest.fn();   

// rest of your code like 
// expect(!!('ontouchstart' in window)).toBe(true);

window.ontouchstart = original;

});

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!