From 9e525094a1289a1620c26cdc8b863fc81497713f Mon Sep 17 00:00:00 2001 From: Marco Braak Date: Mon, 25 Nov 2024 11:13:15 +0100 Subject: [PATCH] Add tests for touchStart --- src/test/mouseHandler.test.ts | 71 ++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/src/test/mouseHandler.test.ts b/src/test/mouseHandler.test.ts index ebe17c5b..4300ef43 100644 --- a/src/test/mouseHandler.test.ts +++ b/src/test/mouseHandler.test.ts @@ -5,6 +5,7 @@ interface CreateMouseHandlerParams { element: HTMLElement; getNode?: jest.Mock; onClickButton?: jest.Mock; + onMouseCapture?: jest.Mock; triggerEvent?: jest.Mock; } @@ -12,11 +13,11 @@ const createMouseHandler = ({ element, getNode = jest.fn(), onClickButton = jest.fn(), + onMouseCapture = jest.fn(), triggerEvent = jest.fn(), }: CreateMouseHandlerParams) => { const getMouseDelay = jest.fn(); const onClickTitle = jest.fn(); - const onMouseCapture = jest.fn(); const onMouseDrag = jest.fn(); const onMouseStart = jest.fn(); const onMouseStop = jest.fn(); @@ -194,3 +195,71 @@ describe("handleDblclick", () => { expect(triggerEvent).not.toHaveBeenCalled(); }); }); + +describe("touchStart", () => { + it("handles a touchstart event", () => { + const element = document.createElement("div"); + document.body.append(element); + + const onMouseCapture = jest.fn(); + + createMouseHandler({ element, onMouseCapture }); + + const touch = { + pageX: 0, + pageY: 0, + }; + + const event = new TouchEvent("touchstart", { + bubbles: true, + touches: [touch as Touch], + }); + element.dispatchEvent(event); + + expect(onMouseCapture).toHaveBeenCalledWith({ + originalEvent: event, + pageX: 0, + pageY: 0, + target: undefined, + }); + }); + + it("handles a touchstart event with multiple touches", () => { + const element = document.createElement("div"); + document.body.append(element); + + const onMouseCapture = jest.fn(); + + createMouseHandler({ element, onMouseCapture }); + + const touch = { + pageX: 0, + pageY: 0, + } as Touch; + + const event = new TouchEvent("touchstart", { + bubbles: true, + touches: [touch, touch], + }); + element.dispatchEvent(event); + + expect(onMouseCapture).not.toHaveBeenCalled(); + }); + + it("handles a touchstart event without touches", () => { + const element = document.createElement("div"); + document.body.append(element); + + const onMouseCapture = jest.fn(); + + createMouseHandler({ element, onMouseCapture }); + + const event = new TouchEvent("touchstart", { + bubbles: true, + touches: [], + }); + element.dispatchEvent(event); + + expect(onMouseCapture).not.toHaveBeenCalled(); + }); +});