Skip to content

Commit

Permalink
feat(teach-frontend): 25-测试拖拽功能
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Aug 9, 2023
1 parent dec29db commit 83f8da7
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
91 changes: 91 additions & 0 deletions apps/teach-frontend/src/composables/tests/drag.spec.ts
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()
})
})
})
21 changes: 21 additions & 0 deletions apps/teach-frontend/src/tests/helper/fireEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,25 @@ export const fireEvent = {
keyDown(eventInitDict?: KeyboardEventInit | undefined) {
window.dispatchEvent(new KeyboardEvent('keydown', eventInitDict))
},

mousedown(
node: HTMLElement | Document,
eventInitDict?: MouseEventInit | undefined,
) {
node.dispatchEvent(new MouseEvent('mousedown', eventInitDict))
},

mousemove(
node: HTMLElement | Document,
eventInitDict?: MouseEventInit | undefined,
) {
node.dispatchEvent(new MouseEvent('mousemove', eventInitDict))
},

mouseup(
node: HTMLElement | Document,
eventInitDict?: MouseEventInit | undefined,
) {
node.dispatchEvent(new MouseEvent('mouseup', eventInitDict))
},
}

1 comment on commit 83f8da7

@vercel
Copy link

@vercel vercel bot commented on 83f8da7 Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.