-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dec29db
commit 83f8da7
Showing
2 changed files
with
112 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,91 @@ | ||
import { afterEach, describe, expect, it, vi } from 'vitest' | ||
import { useDrag } from '../drag' | ||
import { fireEvent } from '@/tests/helper' | ||
|
||
describe('useDrag', () => { | ||
afterEach(() => { | ||
fireEvent.mouseup(document) | ||
}) | ||
it('move left', () => { | ||
const el = document.createElement('div') | ||
const onMove = vi.fn() | ||
|
||
useDrag({ | ||
el, | ||
onMove, | ||
}) | ||
|
||
fireEvent.mousedown(el, { clientX: 10 }) | ||
fireEvent.mousemove(document, { clientX: 9 }) | ||
|
||
expect(onMove).toBeCalledWith(-1) | ||
}) | ||
|
||
it('move right', () => { | ||
const el = document.createElement('div') | ||
const onMove = vi.fn() | ||
|
||
useDrag({ | ||
el, | ||
onMove, | ||
}) | ||
|
||
fireEvent.mousedown(el, { clientX: 10 }) | ||
fireEvent.mousemove(document, { clientX: 11 }) | ||
|
||
expect(onMove).toBeCalledWith(1) | ||
}) | ||
|
||
it('continuous moving', () => { | ||
const el = document.createElement('div') | ||
const onMove = vi.fn() | ||
|
||
useDrag({ | ||
el, | ||
onMove, | ||
}) | ||
|
||
fireEvent.mousedown(el, { clientX: 10 }) | ||
fireEvent.mousemove(document, { clientX: 11 }) | ||
fireEvent.mousemove(document, { clientX: 12 }) | ||
|
||
expect(onMove).toHaveBeenNthCalledWith(1, 1) | ||
expect(onMove).toHaveBeenNthCalledWith(2, 1) | ||
}) | ||
|
||
describe('move range', () => { | ||
it('should not move when past the left bounding', () => { | ||
const el = document.createElement('div') | ||
const onMove = vi.fn() | ||
const leftBounding = 10 | ||
|
||
useDrag({ | ||
el, | ||
onMove, | ||
moveRange: [leftBounding, 50], | ||
}) | ||
|
||
fireEvent.mousedown(el, { clientX: leftBounding }) | ||
fireEvent.mousemove(document, { clientX: leftBounding - 1 }) | ||
|
||
expect(onMove).not.toBeCalled() | ||
}) | ||
|
||
it('should not move when past the right bounding', () => { | ||
const el = document.createElement('div') | ||
const onMove = vi.fn() | ||
const rightBounding = 50 | ||
|
||
useDrag({ | ||
el, | ||
onMove, | ||
moveRange: [10, rightBounding], | ||
}) | ||
|
||
fireEvent.mousedown(el, { clientX: rightBounding }) | ||
fireEvent.mousemove(document, { clientX: rightBounding + 1 }) | ||
|
||
expect(onMove).not.toBeCalled() | ||
}) | ||
}) | ||
}) |
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
83f8da7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
vue3-dida – ./
dida.cuixueshe.com
vue3-dida-git-main-cuixueshe.vercel.app
vue3-dida-cuixueshe.vercel.app
vue3-dida-eta.vercel.app