-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: removed ts-ignore from the drag utils playwright
- Loading branch information
1 parent
da28dfb
commit 6b01f3d
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
}; |