|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file longPressDetector.test.ts |
| 5 | + * @input vitest, longPressDetector |
| 6 | + * @output Tests the long-press detector with no React: a hand start, a touch |
| 7 | + * press on the events' clock, a second finger, a late timer, a start handled |
| 8 | + * late, the movement tolerance, disabled, the latest options, and a touch |
| 9 | + * with no time |
| 10 | + * @position Unit coverage for longPressDetector.ts; useLongPress.test.tsx |
| 11 | + * covers the hook that wraps it |
| 12 | + */ |
| 13 | + |
| 14 | +import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; |
| 15 | +import {longPressDetector, type LongPressTouchEvent} from './longPressDetector'; |
| 16 | + |
| 17 | +// A window whose clock reads `at` now and moves with the fake timers. |
| 18 | +function windowAt(at: number) { |
| 19 | + const offset = at - Date.now(); |
| 20 | + return {performance: {now: () => Date.now() + offset}}; |
| 21 | +} |
| 22 | + |
| 23 | +// A window whose clock reads only what the test sets: a busy page. |
| 24 | +function busyWindow(at: number) { |
| 25 | + const view = {at, performance: {now: () => view.at}}; |
| 26 | + return view; |
| 27 | +} |
| 28 | + |
| 29 | +function touch( |
| 30 | + view: unknown, |
| 31 | + timeStamp: number, |
| 32 | + points: {clientX: number; clientY: number}[] = [], |
| 33 | +): LongPressTouchEvent { |
| 34 | + return {touches: points, timeStamp, view}; |
| 35 | +} |
| 36 | + |
| 37 | +describe('longPressDetector', () => { |
| 38 | + beforeEach(() => { |
| 39 | + vi.useFakeTimers(); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + vi.useRealTimers(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('fires at the start point after the delay when started by hand, and not after a cancel', () => { |
| 47 | + const onLongPress = vi.fn(); |
| 48 | + const detector = longPressDetector({onLongPress}); |
| 49 | + detector.start({x: 3, y: 4}); |
| 50 | + vi.advanceTimersByTime(499); |
| 51 | + expect(onLongPress).not.toHaveBeenCalled(); |
| 52 | + vi.advanceTimersByTime(1); |
| 53 | + expect(onLongPress).toHaveBeenCalledWith({x: 3, y: 4}); |
| 54 | + detector.start({x: 0, y: 0}); |
| 55 | + detector.cancel(); |
| 56 | + vi.advanceTimersByTime(1000); |
| 57 | + expect(onLongPress).toHaveBeenCalledTimes(1); |
| 58 | + }); |
| 59 | + |
| 60 | + it('reports a touch press first when its lift is stamped past the deadline, with no timer run', () => { |
| 61 | + const page = windowAt(1000); |
| 62 | + const onLongPress = vi.fn(); |
| 63 | + const detector = longPressDetector({onLongPress}); |
| 64 | + detector.touchStart(touch(page, 1000, [{clientX: 10, clientY: 20}])); |
| 65 | + detector.touchEnd(touch(page, 1800)); |
| 66 | + expect(onLongPress).toHaveBeenCalledWith({x: 10, y: 20}); |
| 67 | + expect(detector.isPending()).toBe(false); |
| 68 | + }); |
| 69 | + |
| 70 | + it('ends the press on a second finger, and reports it first when that touch is stamped past the deadline', () => { |
| 71 | + const finger = {clientX: 5, clientY: 6}; |
| 72 | + const early = vi.fn(); |
| 73 | + const before = longPressDetector({onLongPress: early}); |
| 74 | + const page = windowAt(1000); |
| 75 | + before.touchStart(touch(page, 1000, [finger])); |
| 76 | + before.touchStart(touch(page, 1200, [finger, finger])); |
| 77 | + expect(before.isPending()).toBe(false); |
| 78 | + vi.advanceTimersByTime(1000); |
| 79 | + expect(early).not.toHaveBeenCalled(); |
| 80 | + |
| 81 | + const late = vi.fn(); |
| 82 | + const after = longPressDetector({onLongPress: late}); |
| 83 | + const busy = busyWindow(1000); |
| 84 | + after.touchStart(touch(busy, 1000, [finger])); |
| 85 | + after.touchStart(touch(busy, 1600, [finger, finger])); |
| 86 | + expect(late).toHaveBeenCalledWith({x: 5, y: 6}); |
| 87 | + expect(after.isPending()).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + it('lets a lift stamped before the deadline cancel the press when its timer runs late', () => { |
| 91 | + const onLongPress = vi.fn(); |
| 92 | + const detector = longPressDetector({onLongPress}); |
| 93 | + const busy = busyWindow(1000); |
| 94 | + detector.touchStart(touch(busy, 1000, [{clientX: 0, clientY: 0}])); |
| 95 | + // The page is busy past the deadline, so the timer runs late and yields |
| 96 | + // once to the queued lift. |
| 97 | + busy.at = 1700; |
| 98 | + vi.advanceTimersByTime(500); |
| 99 | + expect(onLongPress).not.toHaveBeenCalled(); |
| 100 | + detector.touchEnd(touch(busy, 1400)); |
| 101 | + vi.advanceTimersByTime(1000); |
| 102 | + expect(onLongPress).not.toHaveBeenCalled(); |
| 103 | + |
| 104 | + detector.touchStart(touch(busy, 1700, [{clientX: 0, clientY: 0}])); |
| 105 | + busy.at = 2400; |
| 106 | + vi.advanceTimersByTime(500); |
| 107 | + expect(onLongPress).not.toHaveBeenCalled(); |
| 108 | + vi.advanceTimersByTime(1); |
| 109 | + expect(onLongPress).toHaveBeenCalledTimes(1); |
| 110 | + }); |
| 111 | + |
| 112 | + it('waits only for the rest of the hold when its start is handled late', () => { |
| 113 | + const onLongPress = vi.fn(); |
| 114 | + const detector = longPressDetector({onLongPress}); |
| 115 | + detector.touchStart( |
| 116 | + touch(windowAt(1300), 1000, [{clientX: 0, clientY: 0}]), |
| 117 | + ); |
| 118 | + vi.advanceTimersByTime(199); |
| 119 | + expect(onLongPress).not.toHaveBeenCalled(); |
| 120 | + vi.advanceTimersByTime(1); |
| 121 | + expect(onLongPress).toHaveBeenCalledTimes(1); |
| 122 | + }); |
| 123 | + |
| 124 | + it('drops the press past the movement tolerance, or past a per-press tolerance', () => { |
| 125 | + const onLongPress = vi.fn(); |
| 126 | + const detector = longPressDetector({onLongPress}); |
| 127 | + detector.start({x: 0, y: 0}); |
| 128 | + expect(detector.moveTo({x: 10, y: 0})).toBe(true); |
| 129 | + expect(detector.moveTo({x: 11, y: 0})).toBe(false); |
| 130 | + detector.start({x: 0, y: 0}, {moveCancelPx: 13}); |
| 131 | + expect(detector.moveTo({x: 13, y: 0})).toBe(true); |
| 132 | + vi.advanceTimersByTime(500); |
| 133 | + expect(onLongPress).toHaveBeenCalledTimes(1); |
| 134 | + }); |
| 135 | + |
| 136 | + it('starts nothing while disabled, and reads the latest options at each start and fire', () => { |
| 137 | + const first = vi.fn(); |
| 138 | + const latest = vi.fn(); |
| 139 | + const detector = longPressDetector({onLongPress: first, disabled: true}); |
| 140 | + detector.start({x: 0, y: 0}); |
| 141 | + expect(detector.isPending()).toBe(false); |
| 142 | + detector.setOptions({onLongPress: first, delayMs: 200}); |
| 143 | + detector.start({x: 1, y: 1}); |
| 144 | + detector.setOptions({onLongPress: latest, delayMs: 200}); |
| 145 | + vi.advanceTimersByTime(200); |
| 146 | + expect(first).not.toHaveBeenCalled(); |
| 147 | + expect(latest).toHaveBeenCalledWith({x: 1, y: 1}); |
| 148 | + }); |
| 149 | + |
| 150 | + it('waits the full delay for a touch that carries no time (a stamp of 0)', () => { |
| 151 | + const onLongPress = vi.fn(); |
| 152 | + const detector = longPressDetector({onLongPress}); |
| 153 | + detector.touchStart(touch(windowAt(10_000), 0, [{clientX: 0, clientY: 0}])); |
| 154 | + vi.advanceTimersByTime(499); |
| 155 | + expect(onLongPress).not.toHaveBeenCalled(); |
| 156 | + vi.advanceTimersByTime(1); |
| 157 | + expect(onLongPress).toHaveBeenCalledTimes(1); |
| 158 | + }); |
| 159 | +}); |
0 commit comments