|
4 | 4 | * @file useSegmentedSelectionMotion.test.ts |
5 | 5 | * @input vitest, jsdom, SegmentedSelectionMotion with an animator on a |
6 | 6 | * scripted clock, stubbed segment geometry and a recording `animate` |
7 | | - * @output The touch layer's selection motion as a state machine: the slide, |
8 | | - * the hand-back, a change mid-flight, resize, Reduced Motion, the press, the |
9 | | - * chase, the commit at the lift and the face that follows a drag |
| 7 | + * @output The touch layer's selection motion as a state machine: when its |
| 8 | + * animators attach, the slide, the hand-back, a change mid-flight, resize, |
| 9 | + * Reduced Motion, the press, the chase, the commit at the lift and the face |
| 10 | + * that follows a drag |
10 | 11 | * @position Unit tests for useSegmentedSelectionMotion.ts; the browser proofs |
11 | 12 | * are __tests__/SegmentedControl.interaction.browser.spec.ts |
12 | 13 | */ |
@@ -252,6 +253,96 @@ describe('the motion roles (spec:AST-094 FR4)', () => { |
252 | 253 | }); |
253 | 254 | }); |
254 | 255 |
|
| 256 | +describe('when the animators attach', () => { |
| 257 | + interface Started { |
| 258 | + element: HTMLElement; |
| 259 | + keyframes: Keyframe[]; |
| 260 | + options: KeyframeAnimationOptions; |
| 261 | + } |
| 262 | + let started: Started[] = []; |
| 263 | + const realAnimate = HTMLElement.prototype.animate; |
| 264 | + |
| 265 | + // jsdom has no Web Animations. This records each animation that starts. |
| 266 | + beforeEach(() => { |
| 267 | + started = []; |
| 268 | + HTMLElement.prototype.animate = function ( |
| 269 | + this: HTMLElement, |
| 270 | + keyframes: Keyframe[] | PropertyIndexedKeyframes | null, |
| 271 | + options?: number | KeyframeAnimationOptions, |
| 272 | + ) { |
| 273 | + started.push({ |
| 274 | + element: this, |
| 275 | + keyframes: keyframes as Keyframe[], |
| 276 | + options: options as KeyframeAnimationOptions, |
| 277 | + }); |
| 278 | + return { |
| 279 | + finished: new Promise<void>(() => {}), |
| 280 | + cancel: vi.fn(), |
| 281 | + startTime: null, |
| 282 | + } as unknown as Animation; |
| 283 | + }; |
| 284 | + }); |
| 285 | + |
| 286 | + afterEach(() => { |
| 287 | + HTMLElement.prototype.animate = realAnimate; |
| 288 | + }); |
| 289 | + |
| 290 | + /** The static animation that holds the ::after's resting opacity. */ |
| 291 | + const leavingHolds = (track: HTMLElement) => |
| 292 | + started.filter( |
| 293 | + ({element, keyframes, options}) => |
| 294 | + element === track && |
| 295 | + options.pseudoElement === '::after' && |
| 296 | + keyframes.every(keyframe => keyframe.opacity === '0'), |
| 297 | + ); |
| 298 | + |
| 299 | + it('starts no animation when the track attaches, as in the commit that mounts the control', () => { |
| 300 | + const ctx = setup('grid'); |
| 301 | + expect(started).toEqual([]); |
| 302 | + expect(ctx.animator.element).toBeNull(); |
| 303 | + expect(ctx.leavingAnimator.element).toBeNull(); |
| 304 | + }); |
| 305 | + |
| 306 | + it('attaches the animators at the first selection change', () => { |
| 307 | + const ctx = setup('grid'); |
| 308 | + select(ctx, 'grid', 'table'); |
| 309 | + expect(ctx.animator.element).toBe(ctx.track); |
| 310 | + expect(ctx.leavingAnimator.element).toBe(ctx.track); |
| 311 | + expect(leavingHolds(ctx.track)).toHaveLength(1); |
| 312 | + expect(ctx.motion.isSliding).toBe(true); |
| 313 | + }); |
| 314 | + |
| 315 | + it('attaches the animators at the first press', () => { |
| 316 | + const ctx = setup('grid'); |
| 317 | + begin(ctx, 60); |
| 318 | + expect(ctx.animator.element).toBe(ctx.track); |
| 319 | + expect(ctx.leavingAnimator.element).toBe(ctx.track); |
| 320 | + expect(leavingHolds(ctx.track)).toHaveLength(1); |
| 321 | + }); |
| 322 | + |
| 323 | + it('attaches them once: a later change or press starts no second hold', () => { |
| 324 | + const ctx = setup('grid'); |
| 325 | + select(ctx, 'grid', 'table'); |
| 326 | + select(ctx, 'table', 'list'); |
| 327 | + begin(ctx, 60); |
| 328 | + expect(leavingHolds(ctx.track)).toHaveLength(1); |
| 329 | + }); |
| 330 | + |
| 331 | + it('detaches the animators with the track, and attaches them again at the next change', () => { |
| 332 | + const ctx = setup('grid'); |
| 333 | + select(ctx, 'grid', 'table'); |
| 334 | + ctx.motion.attachTrack(null); |
| 335 | + expect(ctx.animator.element).toBeNull(); |
| 336 | + expect(ctx.leavingAnimator.element).toBeNull(); |
| 337 | + |
| 338 | + ctx.motion.attachTrack(ctx.track); |
| 339 | + expect(ctx.animator.element).toBeNull(); |
| 340 | + select(ctx, 'table', 'list'); |
| 341 | + expect(ctx.animator.element).toBe(ctx.track); |
| 342 | + expect(ctx.leavingAnimator.element).toBe(ctx.track); |
| 343 | + }); |
| 344 | +}); |
| 345 | + |
255 | 346 | describe('the selection slide', () => { |
256 | 347 | it('paints the lens (the radiogroup’s ::before) with the selected surface and hides it on the segment: no element is added', () => { |
257 | 348 | const ctx = setup('grid'); |
|
0 commit comments