|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file createDragToSnapPoints.reducedMotion.test.ts |
| 5 | + * @input vitest (jsdom, fake clock), the snap-point drag with the public pan |
| 6 | + * replaced by a stand-in that hands its pan events to the test, Reduce |
| 7 | + * Motion on with a fine primary pointer, a recording stand-in for |
| 8 | + * Element.animate |
| 9 | + * @output Checks `design:reduced-motion` DR2 and DR3 for the snap-point |
| 10 | + * drag's release (`spec:AST-107` FR2): a touch drag's release cross-fades |
| 11 | + * into the snap point, a mouse or pen drag's release lands at once, and |
| 12 | + * `settleTo` lands at once on a fine pointer |
| 13 | + * @position Testing; validates createDragToSnapPoints.ts's release plan |
| 14 | + */ |
| 15 | + |
| 16 | +import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; |
| 17 | +import type { |
| 18 | + PanGestureEvent, |
| 19 | + PanGestureOptions, |
| 20 | +} from '../gestures/createPanGesture'; |
| 21 | + |
| 22 | +let onPan: ((event: PanGestureEvent) => void) | null = null; |
| 23 | + |
| 24 | +vi.mock('../gestures/createPanGesture', () => ({ |
| 25 | + createPanGesture: (_element: Element, options: PanGestureOptions) => { |
| 26 | + onPan = options.onPan; |
| 27 | + return {destroy: () => {}}; |
| 28 | + }, |
| 29 | +})); |
| 30 | + |
| 31 | +const {createDragToSnapPoints} = await import('./createDragToSnapPoints'); |
| 32 | + |
| 33 | +interface Recorded { |
| 34 | + keyframes: Keyframe[]; |
| 35 | +} |
| 36 | + |
| 37 | +const recorded: Recorded[] = []; |
| 38 | +const originalMatchMedia = window.matchMedia; |
| 39 | + |
| 40 | +beforeEach(() => { |
| 41 | + vi.useFakeTimers({ |
| 42 | + toFake: [ |
| 43 | + 'setTimeout', |
| 44 | + 'clearTimeout', |
| 45 | + 'performance', |
| 46 | + 'requestAnimationFrame', |
| 47 | + 'cancelAnimationFrame', |
| 48 | + ], |
| 49 | + }); |
| 50 | + // Reduce Motion on, with a fine primary pointer. |
| 51 | + window.matchMedia = (query: string) => |
| 52 | + ({ |
| 53 | + matches: query.includes('prefers-reduced-motion: reduce'), |
| 54 | + media: query, |
| 55 | + onchange: null, |
| 56 | + addEventListener: () => {}, |
| 57 | + removeEventListener: () => {}, |
| 58 | + addListener: () => {}, |
| 59 | + removeListener: () => {}, |
| 60 | + dispatchEvent: () => false, |
| 61 | + }) as MediaQueryList; |
| 62 | + HTMLElement.prototype.animate = function (keyframes: Keyframe[]) { |
| 63 | + let start: number | null = null; |
| 64 | + const animation = { |
| 65 | + get startTime() { |
| 66 | + return start; |
| 67 | + }, |
| 68 | + set startTime(value: number | null) { |
| 69 | + start = value; |
| 70 | + }, |
| 71 | + playState: 'running', |
| 72 | + ready: new Promise(() => {}), |
| 73 | + finish() {}, |
| 74 | + cancel() {}, |
| 75 | + }; |
| 76 | + requestAnimationFrame(() => { |
| 77 | + start ??= performance.now(); |
| 78 | + }); |
| 79 | + recorded.push({keyframes: Array.isArray(keyframes) ? keyframes : []}); |
| 80 | + return animation as unknown as Animation; |
| 81 | + }; |
| 82 | +}); |
| 83 | + |
| 84 | +afterEach(() => { |
| 85 | + window.matchMedia = originalMatchMedia; |
| 86 | + Reflect.deleteProperty(HTMLElement.prototype, 'animate'); |
| 87 | + vi.useRealTimers(); |
| 88 | + recorded.length = 0; |
| 89 | + onPan = null; |
| 90 | + document.body.replaceChildren(); |
| 91 | +}); |
| 92 | + |
| 93 | +function drawer() { |
| 94 | + const element = document.createElement('div'); |
| 95 | + document.body.append(element); |
| 96 | + const onSettle = vi.fn(); |
| 97 | + const snap = createDragToSnapPoints(element, { |
| 98 | + axis: 'y', |
| 99 | + snapPoints: [0, 240, 480], |
| 100 | + onSettle, |
| 101 | + }); |
| 102 | + return {element, onSettle, snap}; |
| 103 | +} |
| 104 | + |
| 105 | +/** A drag down by `dy`, released at `vy` px/s, driven by `pointerType`. */ |
| 106 | +function release( |
| 107 | + dy: number, |
| 108 | + vy: number, |
| 109 | + pointerType: PanGestureEvent['pointerType'], |
| 110 | +): void { |
| 111 | + const at = (state: PanGestureEvent['state'], y: number, v: number) => |
| 112 | + onPan?.({ |
| 113 | + state, |
| 114 | + translation: {x: 0, y}, |
| 115 | + velocity: {x: 0, y: v}, |
| 116 | + pointerType, |
| 117 | + }); |
| 118 | + at('began', 0, 0); |
| 119 | + at('changed', dy, 0); |
| 120 | + at('ended', dy, vy); |
| 121 | +} |
| 122 | + |
| 123 | +const fades = () => |
| 124 | + recorded.some(({keyframes}) => |
| 125 | + keyframes.some(frame => frame.opacity !== undefined), |
| 126 | + ); |
| 127 | + |
| 128 | +describe('the release under Reduce Motion on a fine pointer (DR2, DR3)', () => { |
| 129 | + it('a touch drag\u2019s release cross-fades into the snap point', async () => { |
| 130 | + const {onSettle} = drawer(); |
| 131 | + release(300, 0, 'touch'); |
| 132 | + expect(fades()).toBe(true); |
| 133 | + await vi.advanceTimersByTimeAsync(1000); |
| 134 | + expect(onSettle).toHaveBeenCalledWith(1); |
| 135 | + }); |
| 136 | + |
| 137 | + it.each(['mouse', 'pen'] as const)( |
| 138 | + 'a %s drag\u2019s release lands at once', |
| 139 | + async pointerType => { |
| 140 | + const {onSettle} = drawer(); |
| 141 | + release(300, 0, pointerType); |
| 142 | + expect(fades()).toBe(false); |
| 143 | + await vi.advanceTimersByTimeAsync(0); |
| 144 | + expect(onSettle).toHaveBeenCalledWith(1); |
| 145 | + }, |
| 146 | + ); |
| 147 | + |
| 148 | + it('settleTo lands at once', async () => { |
| 149 | + const {onSettle, snap} = drawer(); |
| 150 | + snap.settleTo(2); |
| 151 | + expect(fades()).toBe(false); |
| 152 | + await vi.advanceTimersByTimeAsync(0); |
| 153 | + expect(onSettle).toHaveBeenCalledWith(2); |
| 154 | + }); |
| 155 | +}); |
0 commit comments