-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test): added
drag
util and updated tests
- Loading branch information
1 parent
d0b15bb
commit 142fa58
Showing
5 changed files
with
94 additions
and
37 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,5 @@ | ||
--- | ||
"@yamada-ui/test": minor | ||
--- | ||
|
||
Added `drag` util function. |
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
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
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,81 @@ | ||
import type { UserEvent } from "@testing-library/user-event" | ||
import { runIfFunc } from "@yamada-ui/utils" | ||
import { sleep } from "./utils" | ||
|
||
const DEFAULT_COUNT = 11 | ||
|
||
export type PointerCoords = { | ||
x?: number | ||
y?: number | ||
clientX?: number | ||
clientY?: number | ||
offsetX?: number | ||
offsetY?: number | ||
pageX?: number | ||
pageY?: number | ||
screenX?: number | ||
screenY?: number | ||
} | ||
|
||
export type PointerInput = { | ||
target?: HTMLElement | ||
keys?: string | ||
coords?: PointerCoords | ||
node?: Node | ||
offset?: number | ||
} | ||
|
||
export type DragOptions = { | ||
target?: HTMLElement | ||
count?: number | ||
interval?: number | ||
keys?: string | ((index: number) => string | undefined) | ||
coords?: PointerCoords | ((index: number) => PointerCoords | undefined) | ||
} | ||
|
||
const defaultKeys = (i: number) => { | ||
const isStart = i === 0 | ||
const isEnd = i === DEFAULT_COUNT - 1 | ||
|
||
if (isStart) { | ||
return "[MouseLeft>]" | ||
} else if (isEnd) { | ||
return "[/MouseLeft]" | ||
} | ||
|
||
return undefined | ||
} | ||
|
||
const defaultCoords = (i: number) => { | ||
const isEnd = i === DEFAULT_COUNT - 1 | ||
|
||
return !isEnd ? { x: 0, y: i * 100 } : undefined | ||
} | ||
|
||
export const drag = | ||
(user: UserEvent) => | ||
async ({ | ||
target, | ||
count = DEFAULT_COUNT, | ||
interval = 50, | ||
keys: _keys = defaultKeys, | ||
coords: _coords = defaultCoords, | ||
}: DragOptions) => { | ||
const timeline = [...Array(count).keys()] | ||
|
||
for await (const i of timeline) { | ||
const isStart = i === 0 | ||
const keys = runIfFunc(_keys, i) | ||
const coords = runIfFunc(_coords, i) | ||
|
||
const input: PointerInput = {} | ||
|
||
if (isStart) input.target = target | ||
if (keys) input.keys = keys | ||
if (coords) input.coords = coords | ||
|
||
await user.pointer(input) | ||
|
||
await sleep(interval) | ||
} | ||
} |
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