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