Skip to content

Commit

Permalink
Add tests for touchStart
Browse files Browse the repository at this point in the history
  • Loading branch information
mbraak committed Nov 25, 2024
1 parent e3890c7 commit 9e52509
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion src/test/mouseHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ interface CreateMouseHandlerParams {
element: HTMLElement;
getNode?: jest.Mock;
onClickButton?: jest.Mock;
onMouseCapture?: jest.Mock;
triggerEvent?: jest.Mock;
}

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

0 comments on commit 9e52509

Please sign in to comment.