|
| 1 | +import { createElement, type ReactNode } from 'react' |
| 2 | +import { act, create } from 'react-test-renderer' |
| 3 | +import Animated, { useAnimatedRef, useSharedValue } from 'react-native-reanimated' |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +type DragEvent = { absoluteY: number; translationY: number } |
| 7 | +type GestureCallbacks = Record<string, (event: DragEvent, success: boolean) => void> |
| 8 | +const probe = vi.hoisted( |
| 9 | + (): { |
| 10 | + gestures: GestureCallbacks[] |
| 11 | + shared: { value: unknown }[] |
| 12 | + frameActive: ReturnType<typeof vi.fn> |
| 13 | + deferJS: boolean |
| 14 | + jsQueue: (() => void)[] |
| 15 | + frame: ((frame: { timeSincePreviousFrame: number }) => void) | null |
| 16 | + scroll: ReturnType<typeof vi.fn> |
| 17 | + viewport: { pageY: number; height: number } | null |
| 18 | + } => ({ |
| 19 | + gestures: [], |
| 20 | + shared: [], |
| 21 | + frameActive: vi.fn(), |
| 22 | + deferJS: false, |
| 23 | + jsQueue: [], |
| 24 | + frame: null, |
| 25 | + scroll: vi.fn(), |
| 26 | + viewport: null |
| 27 | + }) |
| 28 | +) |
| 29 | + |
| 30 | +vi.mock('react-native', async () => { |
| 31 | + const React = await import('react') |
| 32 | + return { |
| 33 | + View: ({ children, ...props }: { children?: ReactNode }) => |
| 34 | + React.createElement('View', props, children), |
| 35 | + StyleSheet: { create: (styles: unknown) => styles, hairlineWidth: 1 } |
| 36 | + } |
| 37 | +}) |
| 38 | +vi.mock('react-native-gesture-handler', async () => { |
| 39 | + const React = await import('react') |
| 40 | + return { |
| 41 | + GestureDetector: ({ children }: { children: ReactNode }) => |
| 42 | + React.createElement('GestureDetector', null, children), |
| 43 | + Gesture: { |
| 44 | + Pan: () => { |
| 45 | + const callbacks: GestureCallbacks = {} |
| 46 | + probe.gestures.push(callbacks) |
| 47 | + const gesture = { |
| 48 | + activateAfterLongPress: () => gesture, |
| 49 | + shouldCancelWhenOutside: () => gesture, |
| 50 | + onStart: (fn: GestureCallbacks[string]) => { |
| 51 | + callbacks.start = fn |
| 52 | + return gesture |
| 53 | + }, |
| 54 | + onUpdate: (fn: GestureCallbacks[string]) => { |
| 55 | + callbacks.update = fn |
| 56 | + return gesture |
| 57 | + }, |
| 58 | + onFinalize: (fn: GestureCallbacks[string]) => { |
| 59 | + callbacks.finalize = fn |
| 60 | + return gesture |
| 61 | + } |
| 62 | + } |
| 63 | + return gesture |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +}) |
| 68 | +vi.mock('react-native-reanimated', async () => { |
| 69 | + const React = await import('react') |
| 70 | + return { |
| 71 | + default: { |
| 72 | + View: ({ children, ...props }: { children?: ReactNode }) => |
| 73 | + React.createElement('AnimatedView', props, children) |
| 74 | + }, |
| 75 | + useSharedValue: (value: unknown) => { |
| 76 | + const held = React.useRef({ value }) |
| 77 | + if (!probe.shared.includes(held.current)) { |
| 78 | + probe.shared.push(held.current) |
| 79 | + } |
| 80 | + return held.current |
| 81 | + }, |
| 82 | + useAnimatedRef: () => React.useRef(Object.assign(() => null, { current: null })).current, |
| 83 | + useAnimatedStyle: (fn: () => unknown) => fn(), |
| 84 | + useFrameCallback: (fn: (frame: { timeSincePreviousFrame: number }) => void) => { |
| 85 | + probe.frame = fn |
| 86 | + return { setActive: probe.frameActive } |
| 87 | + }, |
| 88 | + withSpring: (value: number) => value, |
| 89 | + runOnJS: |
| 90 | + (fn: (...args: unknown[]) => unknown) => |
| 91 | + (...args: unknown[]) => { |
| 92 | + if (probe.deferJS) { |
| 93 | + probe.jsQueue.push(() => fn(...args)) |
| 94 | + } else { |
| 95 | + fn(...args) |
| 96 | + } |
| 97 | + }, |
| 98 | + runOnUI: (fn: unknown) => fn, |
| 99 | + measure: () => probe.viewport, |
| 100 | + scrollTo: probe.scroll |
| 101 | + } |
| 102 | +}) |
| 103 | +vi.mock('lucide-react-native', () => ({ GripVertical: () => null })) |
| 104 | +vi.mock('../platform/haptics', () => ({ triggerMediumImpact: vi.fn(), triggerSelection: vi.fn() })) |
| 105 | + |
| 106 | +import { DragReorderList } from './DragReorderList' |
| 107 | + |
| 108 | +beforeEach(() => { |
| 109 | + probe.gestures.length = 0 |
| 110 | + probe.shared.length = 0 |
| 111 | + probe.frameActive.mockClear() |
| 112 | + probe.deferJS = false |
| 113 | + probe.jsQueue.length = 0 |
| 114 | + probe.frame = null |
| 115 | + probe.scroll.mockClear() |
| 116 | + probe.viewport = null |
| 117 | +}) |
| 118 | + |
| 119 | +async function mounted() { |
| 120 | + const onReorder = vi.fn() |
| 121 | + const onActive = vi.fn() |
| 122 | + let root: ReturnType<typeof create> |
| 123 | + const initialIds = ['a', 'b'] |
| 124 | + function Harness({ ids = initialIds }: { ids?: string[] }) { |
| 125 | + const scrollRef = useAnimatedRef<Animated.ScrollView>() |
| 126 | + const scrollOffsetY = useSharedValue(0) |
| 127 | + const scrollContentHeight = useSharedValue(144) |
| 128 | + return createElement(DragReorderList<{ id: string }>, { |
| 129 | + items: ids.map((id) => ({ id })), |
| 130 | + itemKey: (item) => item.id, |
| 131 | + rowHeight: 72, |
| 132 | + renderRow: (item) => createElement('Text', null, item.id), |
| 133 | + onReorder, |
| 134 | + onDragActiveChange: (active: boolean) => onActive(active), |
| 135 | + scrollRef, |
| 136 | + scrollOffsetY, |
| 137 | + scrollContentHeight |
| 138 | + }) |
| 139 | + } |
| 140 | + await act(async () => { |
| 141 | + root = create(createElement(Harness)) |
| 142 | + }) |
| 143 | + return { |
| 144 | + root: root!, |
| 145 | + onReorder, |
| 146 | + onActive, |
| 147 | + gesture: probe.gestures[0]!, |
| 148 | + updateItems: async (ids: string[]) => { |
| 149 | + await act(async () => root!.update(createElement(Harness, { ids }))) |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +function positions() { |
| 155 | + return probe.shared.find( |
| 156 | + (held) => held.value && typeof held.value === 'object' && 'a' in held.value && 'b' in held.value |
| 157 | + )!.value |
| 158 | +} |
| 159 | + |
| 160 | +describe('drag completion and cancellation', () => { |
| 161 | + it('restores original positions and writes nothing when an active drag is cancelled', async () => { |
| 162 | + const fixture = await mounted() |
| 163 | + await act(async () => { |
| 164 | + fixture.gesture.start!({ absoluteY: 100, translationY: 0 }, true) |
| 165 | + fixture.gesture.update!({ absoluteY: 180, translationY: 80 }, true) |
| 166 | + }) |
| 167 | + expect(positions()).toEqual({ a: 1, b: 0 }) |
| 168 | + await act(async () => fixture.gesture.finalize!({ absoluteY: 180, translationY: 80 }, false)) |
| 169 | + expect(positions()).toEqual({ a: 0, b: 1 }) |
| 170 | + expect(fixture.onReorder).not.toHaveBeenCalled() |
| 171 | + expect(fixture.onActive.mock.calls).toEqual([[true], [false]]) |
| 172 | + expect(probe.frameActive).toHaveBeenLastCalledWith(false) |
| 173 | + await act(async () => fixture.root.unmount()) |
| 174 | + }) |
| 175 | + |
| 176 | + it('commits the moved order once on a successful drop', async () => { |
| 177 | + const fixture = await mounted() |
| 178 | + await act(async () => { |
| 179 | + fixture.gesture.start!({ absoluteY: 100, translationY: 0 }, true) |
| 180 | + fixture.gesture.update!({ absoluteY: 180, translationY: 80 }, true) |
| 181 | + fixture.gesture.finalize!({ absoluteY: 180, translationY: 80 }, true) |
| 182 | + }) |
| 183 | + expect(fixture.onReorder.mock.calls).toEqual([[['b', 'a']]]) |
| 184 | + await act(async () => fixture.root.unmount()) |
| 185 | + }) |
| 186 | + |
| 187 | + it('does not write after a failed activation or an unchanged drop', async () => { |
| 188 | + const fixture = await mounted() |
| 189 | + await act(async () => { |
| 190 | + fixture.gesture.finalize!({ absoluteY: 100, translationY: 0 }, false) |
| 191 | + fixture.gesture.start!({ absoluteY: 100, translationY: 0 }, true) |
| 192 | + fixture.gesture.finalize!({ absoluteY: 100, translationY: 0 }, true) |
| 193 | + }) |
| 194 | + expect(fixture.onReorder).not.toHaveBeenCalled() |
| 195 | + await act(async () => fixture.root.unmount()) |
| 196 | + }) |
| 197 | +}) |
| 198 | + |
| 199 | +describe('catalog changes during a drag', () => { |
| 200 | + it('releases the scroll lock when the held row disappears and ignores its late cancellation', async () => { |
| 201 | + const fixture = await mounted() |
| 202 | + await act(async () => { |
| 203 | + fixture.gesture.start!({ absoluteY: 100, translationY: 0 }, true) |
| 204 | + fixture.gesture.update!({ absoluteY: 180, translationY: 80 }, true) |
| 205 | + }) |
| 206 | + await fixture.updateItems(['b', 'c']) |
| 207 | + expect(fixture.onActive.mock.calls).toEqual([[true], [false]]) |
| 208 | + expect(probe.frameActive).toHaveBeenLastCalledWith(false) |
| 209 | + await act(async () => fixture.gesture.finalize!({ absoluteY: 180, translationY: 80 }, false)) |
| 210 | + expect( |
| 211 | + probe.shared.some( |
| 212 | + (held) => |
| 213 | + held.value && |
| 214 | + typeof held.value === 'object' && |
| 215 | + 'b' in held.value && |
| 216 | + 'c' in held.value && |
| 217 | + !('a' in held.value) |
| 218 | + ) |
| 219 | + ).toBe(true) |
| 220 | + expect(fixture.onReorder).not.toHaveBeenCalled() |
| 221 | + await act(async () => fixture.root.unmount()) |
| 222 | + }) |
| 223 | + |
| 224 | + it.each([{ ids: ['a', 'b', 'c'] }, { ids: ['b', 'a'] }])( |
| 225 | + 'does not commit a stale drag after the catalog becomes $ids', |
| 226 | + async ({ ids }) => { |
| 227 | + const fixture = await mounted() |
| 228 | + await act(async () => { |
| 229 | + fixture.gesture.start!({ absoluteY: 100, translationY: 0 }, true) |
| 230 | + fixture.gesture.update!({ absoluteY: 180, translationY: 80 }, true) |
| 231 | + }) |
| 232 | + await fixture.updateItems(ids) |
| 233 | + await act(async () => fixture.gesture.finalize!({ absoluteY: 180, translationY: 80 }, true)) |
| 234 | + expect(fixture.onReorder).not.toHaveBeenCalled() |
| 235 | + expect(fixture.onActive.mock.calls).toEqual([[true], [false]]) |
| 236 | + await act(async () => fixture.root.unmount()) |
| 237 | + } |
| 238 | + ) |
| 239 | + |
| 240 | + it('releases its owner when unmounted while a row is held', async () => { |
| 241 | + const fixture = await mounted() |
| 242 | + await act(async () => fixture.gesture.start!({ absoluteY: 100, translationY: 0 }, true)) |
| 243 | + await act(async () => fixture.root.unmount()) |
| 244 | + expect(fixture.onActive.mock.calls).toEqual([[true], [false]]) |
| 245 | + expect(probe.frameActive).toHaveBeenLastCalledWith(false) |
| 246 | + expect(fixture.onReorder).not.toHaveBeenCalled() |
| 247 | + }) |
| 248 | + |
| 249 | + it('keeps a drag when only row metadata refreshes', async () => { |
| 250 | + const fixture = await mounted() |
| 251 | + await act(async () => { |
| 252 | + fixture.gesture.start!({ absoluteY: 100, translationY: 0 }, true) |
| 253 | + fixture.gesture.update!({ absoluteY: 180, translationY: 80 }, true) |
| 254 | + }) |
| 255 | + await fixture.updateItems(['a', 'b']) |
| 256 | + expect(fixture.onActive.mock.calls).toEqual([[true]]) |
| 257 | + probe.deferJS = true |
| 258 | + await act(async () => fixture.gesture.finalize!({ absoluteY: 180, translationY: 80 }, true)) |
| 259 | + await fixture.updateItems(['a', 'b']) |
| 260 | + await act(async () => { |
| 261 | + probe.jsQueue.splice(0).forEach((fn) => fn()) |
| 262 | + }) |
| 263 | + expect(fixture.onReorder.mock.calls).toEqual([[['b', 'a']]]) |
| 264 | + await act(async () => fixture.root.unmount()) |
| 265 | + }) |
| 266 | +}) |
| 267 | + |
| 268 | +describe('late gesture callbacks', () => { |
| 269 | + it('ignores movement delivered after the row catalog changes', async () => { |
| 270 | + const fixture = await mounted() |
| 271 | + await act(async () => fixture.gesture.start!({ absoluteY: 100, translationY: 0 }, true)) |
| 272 | + await fixture.updateItems(['a', 'b', 'c']) |
| 273 | + await act(async () => fixture.gesture.update!({ absoluteY: 180, translationY: 80 }, true)) |
| 274 | + expect(positions()).toEqual({ a: 0, b: 1, c: 2 }) |
| 275 | + expect(fixture.onReorder).not.toHaveBeenCalled() |
| 276 | + await act(async () => fixture.root.unmount()) |
| 277 | + }) |
| 278 | + |
| 279 | + it.each([ |
| 280 | + ['a', 'b', 'c'], |
| 281 | + ['b', 'a'] |
| 282 | + ])('does not save a completed drop queued before catalog %j replaces it', async (...ids) => { |
| 283 | + const fixture = await mounted() |
| 284 | + await act(async () => { |
| 285 | + fixture.gesture.start!({ absoluteY: 100, translationY: 0 }, true) |
| 286 | + fixture.gesture.update!({ absoluteY: 180, translationY: 80 }, true) |
| 287 | + probe.deferJS = true |
| 288 | + fixture.gesture.finalize!({ absoluteY: 180, translationY: 80 }, true) |
| 289 | + }) |
| 290 | + await fixture.updateItems(ids) |
| 291 | + await act(async () => { |
| 292 | + probe.jsQueue.splice(0).forEach((fn) => fn()) |
| 293 | + }) |
| 294 | + expect(fixture.onReorder).not.toHaveBeenCalled() |
| 295 | + await act(async () => fixture.root.unmount()) |
| 296 | + }) |
| 297 | + |
| 298 | + it('does not revive a queued drop when the original row order returns', async () => { |
| 299 | + const fixture = await mounted() |
| 300 | + await act(async () => { |
| 301 | + fixture.gesture.start!({ absoluteY: 100, translationY: 0 }, true) |
| 302 | + fixture.gesture.update!({ absoluteY: 180, translationY: 80 }, true) |
| 303 | + probe.deferJS = true |
| 304 | + fixture.gesture.finalize!({ absoluteY: 180, translationY: 80 }, true) |
| 305 | + }) |
| 306 | + await fixture.updateItems(['b', 'a']) |
| 307 | + await fixture.updateItems(['a', 'b']) |
| 308 | + await act(async () => { |
| 309 | + probe.jsQueue.splice(0).forEach((fn) => fn()) |
| 310 | + }) |
| 311 | + expect(fixture.onReorder).not.toHaveBeenCalled() |
| 312 | + expect(positions()).toEqual({ a: 0, b: 1 }) |
| 313 | + await act(async () => fixture.root.unmount()) |
| 314 | + }) |
| 315 | + |
| 316 | + it('does not save a completed drop after unmount', async () => { |
| 317 | + const fixture = await mounted() |
| 318 | + await act(async () => { |
| 319 | + fixture.gesture.start!({ absoluteY: 100, translationY: 0 }, true) |
| 320 | + fixture.gesture.update!({ absoluteY: 180, translationY: 80 }, true) |
| 321 | + probe.deferJS = true |
| 322 | + fixture.gesture.finalize!({ absoluteY: 180, translationY: 80 }, true) |
| 323 | + }) |
| 324 | + await act(async () => fixture.root.unmount()) |
| 325 | + await act(async () => { |
| 326 | + probe.jsQueue.splice(0).forEach((fn) => fn()) |
| 327 | + }) |
| 328 | + expect(fixture.onReorder).not.toHaveBeenCalled() |
| 329 | + }) |
| 330 | +}) |
| 331 | + |
| 332 | +it('stops scrolling at the viewport edge when rows are replaced', async () => { |
| 333 | + const fixture = await mounted() |
| 334 | + probe.viewport = { pageY: 0, height: 100 } |
| 335 | + await act(async () => { |
| 336 | + fixture.gesture.start!({ absoluteY: 90, translationY: 0 }, true) |
| 337 | + probe.frame!({ timeSincePreviousFrame: 16 }) |
| 338 | + }) |
| 339 | + expect(probe.scroll).toHaveBeenCalledTimes(1) |
| 340 | + await fixture.updateItems(['b', 'c']) |
| 341 | + await act(async () => probe.frame!({ timeSincePreviousFrame: 16 })) |
| 342 | + expect(probe.scroll).toHaveBeenCalledTimes(1) |
| 343 | + expect(probe.frameActive).toHaveBeenLastCalledWith(false) |
| 344 | + await act(async () => fixture.root.unmount()) |
| 345 | +}) |
0 commit comments