Files
headlessui/packages/@headlessui-vue/src/test-utils/fake-pointer.ts
T
Jordan Pittman 2e941f85dd Ignore mouse move/leave events when the cursor hasn’t moved (#2069)
* Ignore mouse move/leave events when the cursor hasn’t moved

A mouse enter / leave event where the cursor hasn’t moved happen only because of:
- Scrolling
- The container moved

* Fix linting errors

* Update changelog

* wip

* Fix tests

* fix linting error

* Tweak tests to bypass tracked pointer checks

* Fixup

* Add stuff

* Fix build script

* fix stuff

* wip
2022-12-07 13:50:57 -05:00

53 lines
1.2 KiB
TypeScript

export class FakePointer {
private x: number = 0
private y: number = 0
constructor(private width: number, private height: number) {
this.width = width
this.height = height
}
get options() {
return {
screenX: this.x,
screenY: this.y,
}
}
randomize() {
this.x = Math.floor(Math.random() * this.width)
this.y = Math.floor(Math.random() * this.height)
}
advance(amount: number = 1) {
this.x += amount
if (this.x >= this.width) {
this.x %= this.width
this.y++
}
if (this.y >= this.height) {
this.y %= this.height
}
}
/**
* JSDOM does not support pointer events.
* Because of this when we try to set the pointer position it returns undefined so our checks fail.
*
* This runs the callback with the TEST_IGNORE_TRACKED_POINTER environment variable set to 1 so we bypass the checks.
*/
bypassingTrackingChecks(callback: () => void) {
let original = process.env.TEST_BYPASS_TRACKED_POINTER
process.env.TEST_BYPASS_TRACKED_POINTER = '1'
callback()
process.env.TEST_BYPASS_TRACKED_POINTER = original
}
}
/**
* A global pointer for use in pointer and mouse event checks
*/
export let pointer = new FakePointer(1920, 1080)