Skip to content

Commit

Permalink
feat(test): added drag util and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hirotomoyamada committed Aug 12, 2024
1 parent d0b15bb commit 142fa58
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-rabbits-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@yamada-ui/test": minor
---

Added `drag` util function.
18 changes: 2 additions & 16 deletions packages/components/reorder/tests/reorder.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { a11y, act, render, screen, waitFor } from "@yamada-ui/test"
import { a11y, act, drag, render, screen, waitFor } from "@yamada-ui/test"
import { useState } from "react"
import type { ReorderGenerateItem } from "../src"
import { Reorder, ReorderItem, ReorderTrigger } from "../src"
Expand Down Expand Up @@ -176,21 +176,7 @@ describe("<Reorder />", () => {

const el = screen.getByText("Item 1")

await user.pointer([
{
target: el,
keys: "[MouseLeft>]",
coords: { x: 0, y: 0 },
},
...Array(10)
.fill(0)
.map((_, i) => ({
coords: { x: 0, y: (i + 1) * 100 },
})),
{
keys: "[/MouseLeft]",
},
])
await drag(user)({ target: el, coords: (i) => ({ x: 0, y: i * 100 }) })

await waitFor(() => {
expect(onChange).toHaveBeenCalledWith(["Item 2", "Item 1"])
Expand Down
26 changes: 5 additions & 21 deletions packages/hooks/use-pan-event/tests/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, waitFor } from "@yamada-ui/test"
import { drag, render, waitFor } from "@yamada-ui/test"
import type { FC } from "react"
import { useRef } from "react"
import type { UsePanEventProps } from "../src"
Expand Down Expand Up @@ -40,22 +40,6 @@ describe("usePanEvent", () => {
return <div ref={ref} data-testid="el" />
}

const pointerInput = (target: HTMLElement) => [
{
target,
keys: "[MouseLeft>]",
coords: { x: 0, y: 0 },
},
...Array(10)
.fill(0)
.map((_, i) => ({
coords: { x: (i + 1) * 10, y: (i + 1) * 10 },
})),
{
keys: "[/MouseLeft]",
},
]

test("should start pan session on pointerdown", async () => {
const onSessionStart = vi.fn()

Expand Down Expand Up @@ -90,7 +74,7 @@ describe("usePanEvent", () => {

const el = getByTestId("el")

await user.pointer(pointerInput(el))
await drag(user)({ target: el, coords: (i) => ({ x: i * 10, y: i * 10 }) })

await waitFor(() =>
expect(onStart).toHaveBeenCalledWith(
Expand All @@ -112,7 +96,7 @@ describe("usePanEvent", () => {

const el = getByTestId("el")

await user.pointer(pointerInput(el))
await drag(user)({ target: el, coords: (i) => ({ x: i * 10, y: i * 10 }) })

await waitFor(() =>
expect(onMove).toHaveBeenCalledWith(
Expand All @@ -134,7 +118,7 @@ describe("usePanEvent", () => {

const el = getByTestId("el")

await user.pointer(pointerInput(el))
await drag(user)({ target: el, coords: (i) => ({ x: i * 10, y: i * 10 }) })

await waitFor(() =>
expect(onEnd).toHaveBeenCalledWith(
Expand All @@ -154,7 +138,7 @@ describe("usePanEvent", () => {

const el = getByTestId("el")

await user.pointer(pointerInput(el))
await drag(user)({ target: el, coords: (i) => ({ x: i * 10, y: i * 10 }) })

await waitFor(() =>
expect(onSessionEnd).toHaveBeenCalledWith(
Expand Down
81 changes: 81 additions & 0 deletions packages/test/src/drag.ts
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)
}
}
1 change: 1 addition & 0 deletions packages/test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from "./render"
export * from "./accessibility"
export * from "./focus"
export * from "./mocks"
export * from "./drag"

0 comments on commit 142fa58

Please sign in to comment.