From f16c9d4fda478548348264ae7059663ece240c16 Mon Sep 17 00:00:00 2001 From: Marco Braak Date: Mon, 25 Nov 2024 14:23:14 +0100 Subject: [PATCH] Add test for touchend --- src/test/mouseHandler.test.ts | 97 +++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 3 deletions(-) diff --git a/src/test/mouseHandler.test.ts b/src/test/mouseHandler.test.ts index 4300ef43..d8bbdf6d 100644 --- a/src/test/mouseHandler.test.ts +++ b/src/test/mouseHandler.test.ts @@ -6,6 +6,9 @@ interface CreateMouseHandlerParams { getNode?: jest.Mock; onClickButton?: jest.Mock; onMouseCapture?: jest.Mock; + onMouseDrag?: jest.Mock; + onMouseStart?: jest.Mock; + onMouseStop?: jest.Mock; triggerEvent?: jest.Mock; } @@ -14,13 +17,13 @@ const createMouseHandler = ({ getNode = jest.fn(), onClickButton = jest.fn(), onMouseCapture = jest.fn(), + onMouseDrag = jest.fn(), + onMouseStart = jest.fn(), + onMouseStop = jest.fn(), triggerEvent = jest.fn(), }: CreateMouseHandlerParams) => { const getMouseDelay = jest.fn(); const onClickTitle = jest.fn(); - const onMouseDrag = jest.fn(); - const onMouseStart = jest.fn(); - const onMouseStop = jest.fn(); return new MouseHandler({ element, @@ -263,3 +266,91 @@ describe("touchStart", () => { expect(onMouseCapture).not.toHaveBeenCalled(); }); }); + +describe("touchEnd", () => { + it("handles a touchend event after a touchstart and a touchmove event", () => { + const element = document.createElement("div"); + document.body.append(element); + + const onMouseCapture = jest.fn(() => true); + const onMouseStart = jest.fn(() => true); + const onMouseStop = jest.fn(); + + createMouseHandler({ + element, + onMouseCapture, + onMouseStart, + onMouseStop, + }); + + const touch = { + pageX: 0, + pageY: 0, + }; + + const touchStartEvent = new TouchEvent("touchstart", { + bubbles: true, + touches: [touch as Touch], + }); + element.dispatchEvent(touchStartEvent); + + const touchMoveEvent = new TouchEvent("touchmove", { + bubbles: true, + touches: [touch as Touch], + }); + element.dispatchEvent(touchMoveEvent); + + const touchEndEvent = new TouchEvent("touchend", { + bubbles: true, + touches: [touch as Touch], + }); + element.dispatchEvent(touchEndEvent); + + expect(onMouseStop).toHaveBeenCalledWith({ + originalEvent: touchEndEvent, + pageX: 0, + pageY: 0, + }); + }); + + it("handles a touchend with multiple touches", () => { + const element = document.createElement("div"); + document.body.append(element); + + const onMouseCapture = jest.fn(() => true); + const onMouseStart = jest.fn(() => true); + const onMouseStop = jest.fn(); + + createMouseHandler({ + element, + onMouseCapture, + onMouseStart, + onMouseStop, + }); + + const touch = { + pageX: 0, + pageY: 0, + }; + + const touchStartEvent = new TouchEvent("touchstart", { + bubbles: true, + touches: [touch as Touch], + }); + element.dispatchEvent(touchStartEvent); + + const touchMoveEvent = new TouchEvent("touchmove", { + bubbles: true, + touches: [touch as Touch], + }); + element.dispatchEvent(touchMoveEvent); + + const touchEndEvent = new TouchEvent("touchend", { + bubbles: true, + touches: [], + }); + element.dispatchEvent(touchEndEvent); + + expect(onMouseStop).not.toHaveBeenCalled(); + }); +});