|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +import fs from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | +import {fileURLToPath} from 'node:url'; |
| 6 | +import {describe, expect, it} from 'vitest'; |
| 7 | +import * as core from '../index'; |
| 8 | +import {resolveSnapPoint} from './resolveSnapPoint'; |
| 9 | + |
| 10 | +const here = path.dirname(fileURLToPath(import.meta.url)); |
| 11 | + |
| 12 | +const SHEET = { |
| 13 | + flickThreshold: 1008, |
| 14 | + tolerance: 1, |
| 15 | + projection: {seconds: 0.1, minSpeed: 250}, |
| 16 | +} as const; |
| 17 | + |
| 18 | +describe('resolveSnapPoint', () => { |
| 19 | + it('lands a slow release on the snap point nearest where it was let go', () => { |
| 20 | + expect( |
| 21 | + resolveSnapPoint({ |
| 22 | + points: [0, 300, 640], |
| 23 | + position: 140, |
| 24 | + velocity: 0, |
| 25 | + flickThreshold: 48, |
| 26 | + }), |
| 27 | + ).toEqual({index: 0, projected: 140}); |
| 28 | + expect( |
| 29 | + resolveSnapPoint({ |
| 30 | + points: [0, 300, 640], |
| 31 | + position: 160, |
| 32 | + velocity: 20, |
| 33 | + flickThreshold: 48, |
| 34 | + }).index, |
| 35 | + ).toBe(1); |
| 36 | + }); |
| 37 | + |
| 38 | + it('projects nothing below the projection speed, and judges a faster slow release where it coasts to', () => { |
| 39 | + // 240 px/s is below 250: judged at 140, nearer 0. |
| 40 | + expect( |
| 41 | + resolveSnapPoint({ |
| 42 | + ...SHEET, |
| 43 | + points: [0, 300, 640], |
| 44 | + position: 140, |
| 45 | + velocity: 240, |
| 46 | + }), |
| 47 | + ).toEqual({index: 0, projected: 140}); |
| 48 | + // 600 px/s projects 0.1 s ahead: 140 + 60 = 200, nearer 300. |
| 49 | + const projected = resolveSnapPoint({ |
| 50 | + ...SHEET, |
| 51 | + points: [0, 300, 640], |
| 52 | + position: 140, |
| 53 | + velocity: 600, |
| 54 | + }); |
| 55 | + expect(projected.index).toBe(1); |
| 56 | + expect(projected.projected).toBeCloseTo(200, 6); |
| 57 | + }); |
| 58 | + |
| 59 | + it('takes a flick at least to the next snap point beyond, or to its projection if that is farther', () => { |
| 60 | + // At 300 moving 1200 px/s: projected 420 (nearer 300), but a flick goes on. |
| 61 | + const flick = resolveSnapPoint({ |
| 62 | + ...SHEET, |
| 63 | + points: [0, 300, 640], |
| 64 | + position: 300, |
| 65 | + velocity: 1200, |
| 66 | + }); |
| 67 | + expect(flick.index).toBe(2); |
| 68 | + expect(flick.projected).toBeCloseTo(420, 6); |
| 69 | + // Without projection, from just past the first snap point. |
| 70 | + expect( |
| 71 | + resolveSnapPoint({ |
| 72 | + points: [0, 300], |
| 73 | + position: 2, |
| 74 | + velocity: 60, |
| 75 | + flickThreshold: 48, |
| 76 | + }).index, |
| 77 | + ).toBe(1); |
| 78 | + // Moving down the axis, to the next snap point below. |
| 79 | + expect( |
| 80 | + resolveSnapPoint({ |
| 81 | + ...SHEET, |
| 82 | + points: [0, 300, 640], |
| 83 | + position: 620, |
| 84 | + velocity: -1100, |
| 85 | + }).index, |
| 86 | + ).toBe(1); |
| 87 | + }); |
| 88 | + |
| 89 | + it('counts a snap point within the tolerance as reached, so a flick goes on to the one after it', () => { |
| 90 | + expect( |
| 91 | + resolveSnapPoint({ |
| 92 | + points: [0, 300, 640], |
| 93 | + position: 300.5, |
| 94 | + velocity: 2000, |
| 95 | + flickThreshold: 1008, |
| 96 | + tolerance: 1, |
| 97 | + }).index, |
| 98 | + ).toBe(2); |
| 99 | + // Past the last snap point there is nothing beyond: the nearest stays. |
| 100 | + expect( |
| 101 | + resolveSnapPoint({ |
| 102 | + points: [0, 300], |
| 103 | + position: 310, |
| 104 | + velocity: 2000, |
| 105 | + flickThreshold: 1008, |
| 106 | + tolerance: 1, |
| 107 | + }).index, |
| 108 | + ).toBe(1); |
| 109 | + }); |
| 110 | + |
| 111 | + it('cancels a release moving back toward where the drag began, whatever its travel or speed', () => { |
| 112 | + // Dragged from 0 to 280, released slowly moving back up: back to 0. |
| 113 | + expect( |
| 114 | + resolveSnapPoint({ |
| 115 | + ...SHEET, |
| 116 | + points: [0, 300, 640], |
| 117 | + position: 280, |
| 118 | + velocity: -10, |
| 119 | + start: 0, |
| 120 | + }).index, |
| 121 | + ).toBe(0); |
| 122 | + // A flick back toward the start cancels too. |
| 123 | + expect( |
| 124 | + resolveSnapPoint({ |
| 125 | + ...SHEET, |
| 126 | + points: [0, 300, 640], |
| 127 | + position: 500, |
| 128 | + velocity: -1500, |
| 129 | + start: 300, |
| 130 | + }).index, |
| 131 | + ).toBe(1); |
| 132 | + // Moving away from the start does not cancel. |
| 133 | + expect( |
| 134 | + resolveSnapPoint({ |
| 135 | + ...SHEET, |
| 136 | + points: [0, 300, 640], |
| 137 | + position: 280, |
| 138 | + velocity: 10, |
| 139 | + start: 0, |
| 140 | + }).index, |
| 141 | + ).toBe(1); |
| 142 | + }); |
| 143 | + |
| 144 | + it('sends a tie the way a projected release moves, and otherwise to the earlier snap point', () => { |
| 145 | + const at = (velocity: number, projection?: typeof SHEET.projection) => |
| 146 | + resolveSnapPoint({ |
| 147 | + points: [0, 300], |
| 148 | + // Released where its projection lands on the midpoint. |
| 149 | + position: 150 - velocity * 0.1 * (projection == null ? 0 : 1), |
| 150 | + velocity, |
| 151 | + flickThreshold: 1008, |
| 152 | + projection, |
| 153 | + }).index; |
| 154 | + expect(at(0)).toBe(0); |
| 155 | + expect(at(10)).toBe(0); |
| 156 | + expect(at(-10)).toBe(0); |
| 157 | + expect(at(400, SHEET.projection)).toBe(1); |
| 158 | + expect(at(-400, SHEET.projection)).toBe(0); |
| 159 | + }); |
| 160 | + |
| 161 | + it('needs at least one snap point', () => { |
| 162 | + expect(() => |
| 163 | + resolveSnapPoint({ |
| 164 | + points: [], |
| 165 | + position: 0, |
| 166 | + velocity: 0, |
| 167 | + flickThreshold: 1, |
| 168 | + }), |
| 169 | + ).toThrow(RangeError); |
| 170 | + }); |
| 171 | + |
| 172 | + it('stays internal: not exported from the package or any barrel', () => { |
| 173 | + expect(core).not.toHaveProperty('resolveSnapPoint'); |
| 174 | + const pkg = JSON.parse( |
| 175 | + fs.readFileSync(path.join(here, '../../package.json'), 'utf8'), |
| 176 | + ) as {exports: Record<string, unknown>}; |
| 177 | + expect( |
| 178 | + Object.keys(pkg.exports).filter(key => key.includes('motion')), |
| 179 | + ).toEqual([]); |
| 180 | + const barrels = fs |
| 181 | + .readdirSync(here) |
| 182 | + .filter(name => /^index\.(ts|tsx|js|mjs)$/.test(name)); |
| 183 | + for (const barrel of barrels) { |
| 184 | + expect(fs.readFileSync(path.join(here, barrel), 'utf8')).not.toMatch( |
| 185 | + /resolveSnapPoint/, |
| 186 | + ); |
| 187 | + } |
| 188 | + }); |
| 189 | +}); |
0 commit comments