Skip to content

Commit

Permalink
Add test for touchend
Browse files Browse the repository at this point in the history
  • Loading branch information
mbraak committed Nov 25, 2024
1 parent 9e52509 commit f16c9d4
Showing 1 changed file with 94 additions and 3 deletions.
97 changes: 94 additions & 3 deletions src/test/mouseHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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,
Expand Down Expand Up @@ -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();
});
});

0 comments on commit f16c9d4

Please sign in to comment.