Home > OS >  mock ontouchstart event in window object jest
mock ontouchstart event in window object jest

Time:06-06

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

CodePudding user response:

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);
  });
});
})
  • Related