|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { useDrag } from '../drag' |
| 3 | +import { fireEvent } from '@/tests/helper' |
| 4 | + |
| 5 | +describe('useDrag', () => { |
| 6 | + afterEach(() => { |
| 7 | + fireEvent.mouseup(document) |
| 8 | + }) |
| 9 | + it('move left', () => { |
| 10 | + const el = document.createElement('div') |
| 11 | + const onMove = vi.fn() |
| 12 | + |
| 13 | + useDrag({ |
| 14 | + el, |
| 15 | + onMove, |
| 16 | + }) |
| 17 | + |
| 18 | + fireEvent.mousedown(el, { clientX: 10 }) |
| 19 | + fireEvent.mousemove(document, { clientX: 9 }) |
| 20 | + |
| 21 | + expect(onMove).toBeCalledWith(-1) |
| 22 | + }) |
| 23 | + |
| 24 | + it('move right', () => { |
| 25 | + const el = document.createElement('div') |
| 26 | + const onMove = vi.fn() |
| 27 | + |
| 28 | + useDrag({ |
| 29 | + el, |
| 30 | + onMove, |
| 31 | + }) |
| 32 | + |
| 33 | + fireEvent.mousedown(el, { clientX: 10 }) |
| 34 | + fireEvent.mousemove(document, { clientX: 11 }) |
| 35 | + |
| 36 | + expect(onMove).toBeCalledWith(1) |
| 37 | + }) |
| 38 | + |
| 39 | + it('continuous moving', () => { |
| 40 | + const el = document.createElement('div') |
| 41 | + const onMove = vi.fn() |
| 42 | + |
| 43 | + useDrag({ |
| 44 | + el, |
| 45 | + onMove, |
| 46 | + }) |
| 47 | + |
| 48 | + fireEvent.mousedown(el, { clientX: 10 }) |
| 49 | + fireEvent.mousemove(document, { clientX: 11 }) |
| 50 | + fireEvent.mousemove(document, { clientX: 12 }) |
| 51 | + |
| 52 | + expect(onMove).toHaveBeenNthCalledWith(1, 1) |
| 53 | + expect(onMove).toHaveBeenNthCalledWith(2, 1) |
| 54 | + }) |
| 55 | + |
| 56 | + describe('move range', () => { |
| 57 | + it('should not move when past the left bounding', () => { |
| 58 | + const el = document.createElement('div') |
| 59 | + const onMove = vi.fn() |
| 60 | + const leftBounding = 10 |
| 61 | + |
| 62 | + useDrag({ |
| 63 | + el, |
| 64 | + onMove, |
| 65 | + moveRange: [leftBounding, 50], |
| 66 | + }) |
| 67 | + |
| 68 | + fireEvent.mousedown(el, { clientX: leftBounding }) |
| 69 | + fireEvent.mousemove(document, { clientX: leftBounding - 1 }) |
| 70 | + |
| 71 | + expect(onMove).not.toBeCalled() |
| 72 | + }) |
| 73 | + |
| 74 | + it('should not move when past the right bounding', () => { |
| 75 | + const el = document.createElement('div') |
| 76 | + const onMove = vi.fn() |
| 77 | + const rightBounding = 50 |
| 78 | + |
| 79 | + useDrag({ |
| 80 | + el, |
| 81 | + onMove, |
| 82 | + moveRange: [10, rightBounding], |
| 83 | + }) |
| 84 | + |
| 85 | + fireEvent.mousedown(el, { clientX: rightBounding }) |
| 86 | + fireEvent.mousemove(document, { clientX: rightBounding + 1 }) |
| 87 | + |
| 88 | + expect(onMove).not.toBeCalled() |
| 89 | + }) |
| 90 | + }) |
| 91 | +}) |
0 commit comments