|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file reorderModel.test.ts |
| 5 | + * @input reorderModel.ts and the UIKit reorder values (motion/uikit/reorder.ts) |
| 6 | + * @output Unit proofs of the reorder model against the UIKit lab measurements |
| 7 | + * (iOS 26.2 simulator, a table of 44 pt rows with rows 6 and 9 at 88 pt): |
| 8 | + * the proposed slots of a measured drag, the movable range, the displaced |
| 9 | + * rows' offsets, the slot's place, the drop's spring durations, the slides' |
| 10 | + * shortening while autoscrolling and the autoscroll steps |
| 11 | + * @position Tests for List/reorderModel.ts |
| 12 | + */ |
| 13 | + |
| 14 | +import {describe, expect, it} from 'vitest'; |
| 15 | +import {reorder} from '../../motion/uikit/reorder'; |
| 16 | +import { |
| 17 | + autoscrollStep, |
| 18 | + clampTop, |
| 19 | + movableRange, |
| 20 | + moveIndex, |
| 21 | + offsetsFor, |
| 22 | + proposedSlot, |
| 23 | + settleSpring, |
| 24 | + slideSpring, |
| 25 | + slotTop, |
| 26 | + type ReorderGeometry, |
| 27 | + type ReorderRow, |
| 28 | +} from '../reorderModel'; |
| 29 | + |
| 30 | +function table( |
| 31 | + heights: ReadonlyArray<number>, |
| 32 | + {gap = 0, fixed = []}: {gap?: number; fixed?: ReadonlyArray<number>} = {}, |
| 33 | +): ReorderGeometry { |
| 34 | + let top = 0; |
| 35 | + const rows: ReorderRow[] = heights.map((height, index) => { |
| 36 | + const row = {top, height, isFixed: fixed.includes(index)}; |
| 37 | + top += height + gap; |
| 38 | + return row; |
| 39 | + }); |
| 40 | + return {rows, gap}; |
| 41 | +} |
| 42 | + |
| 43 | +/** The measured table: 40 rows of 44 pt, rows 6 and 9 at 88 pt. */ |
| 44 | +const MEASURED = table( |
| 45 | + Array.from({length: 40}, (_, index) => |
| 46 | + index === 6 || index === 9 ? 88 : 44, |
| 47 | + ), |
| 48 | +); |
| 49 | + |
| 50 | +describe('proposedSlot', () => { |
| 51 | + it('reproduces the measured drag of row 3 down over the 88 pt row and back', () => { |
| 52 | + // Each proposal of the UIKit lab drag (table.proposed), with the dragged |
| 53 | + // row's centre (list coordinates) on the frame before it. Down, the slot |
| 54 | + // moves once the centre is past the boundary; up, once it is back on it. |
| 55 | + const measured: ReadonlyArray<[center: number, slot: number]> = [ |
| 56 | + [178, 4], |
| 57 | + [221, 5], |
| 58 | + [309, 6], |
| 59 | + [354, 7], |
| 60 | + [398, 8], |
| 61 | + [396, 7], |
| 62 | + [352, 6], |
| 63 | + [306.8, 5], |
| 64 | + ]; |
| 65 | + for (const [center, slot] of measured) { |
| 66 | + expect(proposedSlot(MEASURED, 3, center), `centre ${center}`).toBe(slot); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + it('swaps a 44 pt neighbour after half a row and the 88 pt row after its height', () => { |
| 71 | + // Row 3's centre starts at 3 × 44 + 22 = 154. |
| 72 | + expect(proposedSlot(MEASURED, 3, 154)).toBe(3); |
| 73 | + expect(proposedSlot(MEASURED, 3, 154 + 22)).toBe(3); |
| 74 | + expect(proposedSlot(MEASURED, 3, 154 + 22.5)).toBe(4); |
| 75 | + expect(proposedSlot(MEASURED, 3, 154 - 22)).toBe(2); |
| 76 | + // From slot 5 (centre past 220) to slot 6 takes the 88 pt row's height. |
| 77 | + expect(proposedSlot(MEASURED, 3, 221)).toBe(5); |
| 78 | + expect(proposedSlot(MEASURED, 3, 307)).toBe(5); |
| 79 | + expect(proposedSlot(MEASURED, 3, 309)).toBe(6); |
| 80 | + }); |
| 81 | + |
| 82 | + it('does not move a tall row whose centre starts on a short neighbour boundary', () => { |
| 83 | + const geometry = table([44, 88, 44, 44]); |
| 84 | + // Row 1's centre (44 + 44) is the next row's boundary without it. |
| 85 | + expect(proposedSlot(geometry, 1, 88)).toBe(1); |
| 86 | + expect(proposedSlot(geometry, 1, 89)).toBe(2); |
| 87 | + }); |
| 88 | + |
| 89 | + it('puts the boundary in the middle of the gap between rows', () => { |
| 90 | + const geometry = table([40, 40, 40], {gap: 4}); |
| 91 | + // Without row 0, row 1 sits at 0..40; its boundary is at 42. |
| 92 | + expect(proposedSlot(geometry, 0, 42)).toBe(0); |
| 93 | + expect(proposedSlot(geometry, 0, 42.5)).toBe(1); |
| 94 | + }); |
| 95 | + |
| 96 | + it('stops at a fixed row', () => { |
| 97 | + const geometry = table([44, 44, 44, 44, 44], {fixed: [3]}); |
| 98 | + expect(proposedSlot(geometry, 0, 1000)).toBe(2); |
| 99 | + expect(proposedSlot(geometry, 4, -1000)).toBe(4); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('movableRange', () => { |
| 104 | + it('is bounded by the nearest fixed rows', () => { |
| 105 | + const {rows} = table([44, 44, 44, 44, 44, 44], {fixed: [1, 4]}); |
| 106 | + expect(movableRange(rows, 2)).toEqual({start: 2, end: 3}); |
| 107 | + expect(movableRange(rows, 0)).toEqual({start: 0, end: 0}); |
| 108 | + expect(movableRange(rows, 5)).toEqual({start: 5, end: 5}); |
| 109 | + expect(movableRange(table([44, 44, 44]).rows, 1)).toEqual({ |
| 110 | + start: 0, |
| 111 | + end: 2, |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe('offsetsFor and slotTop', () => { |
| 117 | + it('moves the rows between the place and the slot by the dragged row', () => { |
| 118 | + expect(offsetsFor(MEASURED, 3, 5).slice(0, 8)).toEqual([ |
| 119 | + 0, 0, 0, 0, -44, -44, 0, 0, |
| 120 | + ]); |
| 121 | + expect(offsetsFor(MEASURED, 6, 4).slice(0, 8)).toEqual([ |
| 122 | + 0, 0, 0, 0, 88, 88, 0, 0, |
| 123 | + ]); |
| 124 | + const gapped = table([40, 40, 40], {gap: 4}); |
| 125 | + expect(offsetsFor(gapped, 0, 2)).toEqual([0, -44, -44]); |
| 126 | + }); |
| 127 | + |
| 128 | + it('places the dragged row where the displaced rows leave room', () => { |
| 129 | + // Row 3 to slot 6 (past the 88 pt row): 3 × 44 + 44 + 88 … row 6's |
| 130 | + // bottom (352) minus its own 44. |
| 131 | + expect(slotTop(MEASURED, 3, 6)).toBe(308); |
| 132 | + expect(slotTop(MEASURED, 3, 1)).toBe(44); |
| 133 | + expect(slotTop(MEASURED, 3, 3)).toBe(132); |
| 134 | + const gapped = table([40, 40, 40], {gap: 4}); |
| 135 | + expect(slotTop(gapped, 0, 2)).toBe(88); |
| 136 | + expect(slotTop(gapped, 2, 0)).toBe(0); |
| 137 | + }); |
| 138 | + |
| 139 | + it('orders the rows after a move', () => { |
| 140 | + expect(moveIndex(5, 1, 3)).toEqual([0, 2, 3, 1, 4]); |
| 141 | + expect(moveIndex(5, 4, 0)).toEqual([4, 0, 1, 2, 3]); |
| 142 | + }); |
| 143 | +}); |
| 144 | + |
| 145 | +describe('settleSpring', () => { |
| 146 | + it('scales the measured drop durations by the distance up to half the row', () => { |
| 147 | + // 44 pt row released 12 pt from its slot: 0.1636 s. |
| 148 | + expect(settleSpring(12, 44).duration).toBeCloseTo(0.1636, 4); |
| 149 | + // 88 pt row released 12 / 22 / 33 / 43 pt away. |
| 150 | + const measured: ReadonlyArray<[number, number]> = [ |
| 151 | + [12, 0.0818], |
| 152 | + [22, 0.15], |
| 153 | + [33, 0.225], |
| 154 | + [43, 0.2932], |
| 155 | + ]; |
| 156 | + for (const [distance, duration] of measured) { |
| 157 | + expect(settleSpring(distance, 88).duration).toBeCloseTo(duration, 3); |
| 158 | + expect(settleSpring(-distance, 88).duration).toBeCloseTo(duration, 3); |
| 159 | + } |
| 160 | + expect(settleSpring(200, 44)).toEqual({ |
| 161 | + duration: reorder.settle.maxDuration, |
| 162 | + bounce: 0, |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it('never makes a spring shorter than a frame', () => { |
| 167 | + expect(settleSpring(0, 44).duration).toBeCloseTo(1 / 60, 6); |
| 168 | + }); |
| 169 | +}); |
| 170 | + |
| 171 | +describe('slideSpring', () => { |
| 172 | + it('is the table spring at rest and shortens with the autoscroll step', () => { |
| 173 | + expect(slideSpring()).toEqual({duration: 0.3, bounce: 0}); |
| 174 | + // Measured: 0.24 / 0.21 / 0.18 / 0.03 s at 2 / 3 / 4 / 9 pt per tick. |
| 175 | + expect(slideSpring(2).duration).toBeCloseTo(0.24, 6); |
| 176 | + expect(slideSpring(3).duration).toBeCloseTo(0.21, 6); |
| 177 | + expect(slideSpring(4).duration).toBeCloseTo(0.18, 6); |
| 178 | + expect(slideSpring(9).duration).toBeCloseTo(0.03, 6); |
| 179 | + expect(slideSpring(10).duration).toBeCloseTo(1 / 60, 6); |
| 180 | + }); |
| 181 | +}); |
| 182 | + |
| 183 | +describe('autoscrollStep', () => { |
| 184 | + it('follows the measured steps by edge depth', () => { |
| 185 | + expect(autoscrollStep(-12)).toBe(0); |
| 186 | + expect(autoscrollStep(-reorder.autoscroll.startInset - 0.5)).toBe(0); |
| 187 | + expect(autoscrollStep(-reorder.autoscroll.startInset)).toBe(2); |
| 188 | + expect(autoscrollStep(-3)).toBe(2); |
| 189 | + expect(autoscrollStep(6)).toBe(2); |
| 190 | + expect(autoscrollStep(8)).toBe(3); |
| 191 | + expect(autoscrollStep(10)).toBe(3); |
| 192 | + expect(autoscrollStep(12)).toBe(4); |
| 193 | + expect(autoscrollStep(16)).toBe(6); |
| 194 | + expect(autoscrollStep(20)).toBe(8); |
| 195 | + expect(autoscrollStep(22)).toBe(10); |
| 196 | + expect(autoscrollStep(40)).toBe(10); |
| 197 | + }); |
| 198 | + |
| 199 | + it('interpolates whole points between samples', () => { |
| 200 | + expect(autoscrollStep(14)).toBe(5); |
| 201 | + expect(autoscrollStep(21)).toBe(9); |
| 202 | + for (let depth = -8; depth <= 30; depth += 0.25) { |
| 203 | + expect(Number.isInteger(autoscrollStep(depth))).toBe(true); |
| 204 | + } |
| 205 | + }); |
| 206 | +}); |
| 207 | + |
| 208 | +describe('clampTop', () => { |
| 209 | + it('keeps the centre between the edges', () => { |
| 210 | + // The measured top clamp: a 44 pt row on a table starting at 62 stops at |
| 211 | + // a top of 40 (its centre on the edge). |
| 212 | + expect(clampTop(0, 44, 62, 800)).toBe(40); |
| 213 | + expect(clampTop(100, 44, 62, 800)).toBe(100); |
| 214 | + expect(clampTop(900, 44, 62, 800)).toBe(778); |
| 215 | + }); |
| 216 | +}); |
0 commit comments