Skip to content

Commit

Permalink
fix: removed ts-ignore from the drag utils playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
salvatorecriscioneweb committed Dec 22, 2023
1 parent da28dfb commit 6b01f3d
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions docs/utils/playwrightDrag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
declare global {
interface Window {
createTouchEvent: (x: number, y: number, offset?: number) => Touch;
PlayWrightStartTouch: (
selector: string,
position: {
x: number;
y: number;
},
) => void;
PlayWrightMoveTouch: (selector: string, y: number, steps: number) => void;
PlayWrightEndTouch: (selector: string) => void;
}
}

export const createTouchEvent = () => {
window.createTouchEvent = (x: number, y: number, offset = -281) => {
return new Touch({
clientX: x,
clientY: y,
screenX: x,
screenY: offset + y,
target: window,
identifier: 0,
});
};
};

export const PlayWrightStartTouch = () => {
window.PlayWrightStartTouch = (
selector: string,
position: {
x: number;
y: number;
},
) => {
const element = document.querySelector(selector);
if (!element) {
throw Error("Selector not found");
}
// @ts-ignore
const t = window.createTouchEvent(0, 0);
const event = new TouchEvent("touchstart", {
touches: [t],
changedTouches: [t],
cancelable: true,
bubbles: true,
});
element.dispatchEvent(event);
};
};

export const PlayWrightMoveTouch = () => {
window.PlayWrightMoveTouch = (selector: string, y: number, steps: number) => {
const element = document.querySelector(selector);
if (!element) {
throw Error("Selector not found");
}

const minorStep = y / steps;
// Create the steps
const touches = Array(steps)
.fill(0)
.map((t: any, index: number) => {
// @ts-ignore
return window.createTouchEvent(0, minorStep * index);
});
touches.forEach((t) => {
const event = new TouchEvent("touchmove", {
touches: [t],
changedTouches: [t],
cancelable: true,
bubbles: true,
});
element.dispatchEvent(event);
});
};
};

export const PlayWrightEndTouch = () => {
window.PlayWrightEndTouch = (selector: string) => {
const element = document.querySelector(selector);
if (!element) {
throw Error("Selector not found");
}
const t = window.createTouchEvent(0, 0);
const event = new TouchEvent("touchend", {
touches: [t],
changedTouches: [t],
cancelable: true,
bubbles: true,
});
element.dispatchEvent(event);
};
};

0 comments on commit 6b01f3d

Please sign in to comment.