|
| 1 | +import { SwipeDirection } from './index' |
| 2 | +import { useSetup } from '../../.test' |
| 3 | +import { useSwipe } from '.' |
| 4 | +import each from 'jest-each' |
| 5 | + |
| 6 | +describe('useSwipe', () => { |
| 7 | + const target = document.createElement('div') |
| 8 | + target.id = 'target' |
| 9 | + document.body.appendChild(target) |
| 10 | + |
| 11 | + const mockTouchEventInit = (x: number, y: number): TouchEventInit => ({ |
| 12 | + touches: [{ |
| 13 | + clientX: x, |
| 14 | + clientY: y, |
| 15 | + altitudeAngle: 0, |
| 16 | + azimuthAngle: 0, |
| 17 | + force: 0, |
| 18 | + identifier: 0, |
| 19 | + pageX: 0, |
| 20 | + pageY: 0, |
| 21 | + radiusX: 0, |
| 22 | + radiusY: 0, |
| 23 | + rotationAngle: 0, |
| 24 | + screenX: 0, |
| 25 | + screenY: 0, |
| 26 | + target, |
| 27 | + touchType: 'direct', |
| 28 | + }], |
| 29 | + }) |
| 30 | + |
| 31 | + const mockTouchStart = (x: number, y: number) => new TouchEvent('touchstart', mockTouchEventInit(x, y)) |
| 32 | + const mockTouchMove = (x: number, y: number) => new TouchEvent('touchmove', mockTouchEventInit(x, y)) |
| 33 | + const mockTouchEnd = (x: number, y: number) => new TouchEvent('touchend', mockTouchEventInit(x, y)) |
| 34 | + |
| 35 | + const mockTouchEvents = (target: EventTarget, coords: Array<number[]>) => { |
| 36 | + coords.forEach(([x, y], i) => { |
| 37 | + if (i === 0) target.dispatchEvent(mockTouchStart(x, y)) |
| 38 | + else if (i === coords.length - 1) target.dispatchEvent(mockTouchEnd(x, y)) |
| 39 | + else |
| 40 | + target.dispatchEvent(mockTouchMove(x, y)) |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + let onSwipe: jest.Mock |
| 45 | + let onSwipeEnd: jest.Mock |
| 46 | + const threshold = 30 |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + onSwipe = jest.fn((e: TouchEvent) => {}) |
| 50 | + onSwipeEnd = jest.fn((e: TouchEvent, direction: SwipeDirection) => {}) |
| 51 | + }) |
| 52 | + |
| 53 | + it('threshold not exceeded', () => { |
| 54 | + useSetup(() => { |
| 55 | + useSwipe(target, { threshold, onSwipe, onSwipeEnd }) |
| 56 | + |
| 57 | + mockTouchEvents(target, [[0, 0], [threshold - 1, 0], [threshold - 1, 0]]) |
| 58 | + |
| 59 | + expect(onSwipe.mock.calls.length).toBe(0) |
| 60 | + expect(onSwipeEnd.mock.calls.length).toBe(0) |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + it('threshold exceeded', () => { |
| 65 | + useSetup(() => { |
| 66 | + useSwipe(target, { threshold, onSwipe, onSwipeEnd }) |
| 67 | + |
| 68 | + mockTouchEvents(target, [[0, 0], [threshold / 2, 0], [threshold, 0], [threshold, 0]]) |
| 69 | + |
| 70 | + expect(onSwipe.mock.calls.length).toBe(1) |
| 71 | + expect(onSwipeEnd.mock.calls.length).toBe(1) |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + it('reactivity', () => { |
| 76 | + useSetup(() => { |
| 77 | + const { isSwiping, direction, lengthX, lengthY } = useSwipe(target, { threshold, onSwipe, onSwipeEnd }) |
| 78 | + |
| 79 | + target.dispatchEvent(mockTouchStart(0, 0)) |
| 80 | + expect(isSwiping.value).toBeFalsy() |
| 81 | + expect(direction.value).toBeNull() |
| 82 | + expect(lengthX.value).toBe(0) |
| 83 | + expect(lengthY.value).toBe(0) |
| 84 | + |
| 85 | + target.dispatchEvent(mockTouchMove(threshold, 5)) |
| 86 | + expect(isSwiping.value).toBeTruthy() |
| 87 | + expect(direction.value).toBe(SwipeDirection.RIGHT) |
| 88 | + expect(lengthX.value).toBe(-threshold) |
| 89 | + expect(lengthY.value).toBe(-5) |
| 90 | + |
| 91 | + target.dispatchEvent(mockTouchEnd(threshold, 5)) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + each([ |
| 96 | + [SwipeDirection.UP, [[0, 2 * threshold], [0, threshold], [0, threshold]]], |
| 97 | + [SwipeDirection.DOWN, [[0, 0], [0, threshold], [0, threshold]]], |
| 98 | + [SwipeDirection.LEFT, [[2 * threshold, 0], [threshold, 0], [threshold, 0]]], |
| 99 | + [SwipeDirection.RIGHT, [[0, 0], [threshold, 0], [threshold, 0]]], |
| 100 | + ]).it('swipe %s', (expected, coords) => { |
| 101 | + useSetup(() => { |
| 102 | + const { direction } = useSwipe(target, { threshold, onSwipe, onSwipeEnd }) |
| 103 | + |
| 104 | + mockTouchEvents(target, [[0, 2 * threshold], [0, threshold], [0, threshold]]) |
| 105 | + |
| 106 | + expect(direction.value).toBe(SwipeDirection.UP) |
| 107 | + expect(onSwipeEnd.mock.calls[0][1]).toBe(SwipeDirection.UP) |
| 108 | + }) |
| 109 | + }) |
| 110 | +}) |
0 commit comments