|
3 | 3 | import React, { act, useState } from "react"; |
4 | 4 | import { createRoot } from "react-dom/client"; |
5 | 5 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import { parseSpringBounce } from "@hyperframes/core/spring-ease"; |
| 7 | +import { parseWiggleEase } from "@hyperframes/core/wiggle-ease"; |
6 | 8 | import { EaseCurveSection, MiniCurveSvg } from "./EaseCurveSection"; |
| 9 | +import { resolveEaseCurveTuple } from "./gsapAnimationConstants"; |
7 | 10 | import type { AnimationKeyframeTarget } from "../../hooks/gsapTweenSynth"; |
8 | 11 |
|
9 | 12 | (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
@@ -76,6 +79,19 @@ function renderStatefulSection(initialEase = "none", onCustomEaseCommit = vi.fn( |
76 | 79 | return { host, root, onCustomEaseCommit }; |
77 | 80 | } |
78 | 81 |
|
| 82 | +function renderControlledSection(initialEase = "none", onCustomEaseCommit = vi.fn()) { |
| 83 | + const host = document.createElement("div"); |
| 84 | + document.body.append(host); |
| 85 | + const root = createRoot(host); |
| 86 | + const renderEase = (ease: string) => { |
| 87 | + act(() => |
| 88 | + root.render(<EaseCurveSection ease={ease} onCustomEaseCommit={onCustomEaseCommit} />), |
| 89 | + ); |
| 90 | + }; |
| 91 | + renderEase(initialEase); |
| 92 | + return { host, root, onCustomEaseCommit, renderEase }; |
| 93 | +} |
| 94 | + |
79 | 95 | function clickMode(host: HTMLElement, mode: "curve" | "spring" | "wiggle"): void { |
80 | 96 | const toggle = host.querySelector<HTMLButtonElement>(`[data-ease-mode="${mode}"]`); |
81 | 97 | expect(toggle).not.toBeNull(); |
@@ -291,12 +307,107 @@ describe("EaseCurveSection preset grid", () => { |
291 | 307 |
|
292 | 308 | clickMode(host, "spring"); |
293 | 309 | expect(onCustomEaseCommit).toHaveBeenLastCalledWith("spring(0.42)"); |
| 310 | + expect(parseSpringBounce(onCustomEaseCommit.mock.lastCall![0])).toBe(0.42); |
294 | 311 |
|
295 | 312 | clickMode(host, "curve"); |
296 | 313 | expect(onCustomEaseCommit).toHaveBeenLastCalledWith("custom(M0,0 C0.16,1 0.3,1 1,1)"); |
| 314 | + expect(resolveEaseCurveTuple(onCustomEaseCommit.mock.lastCall![0])).toEqual([0.16, 1, 0.3, 1]); |
297 | 315 |
|
298 | 316 | clickMode(host, "wiggle"); |
299 | 317 | expect(onCustomEaseCommit).toHaveBeenLastCalledWith("wiggle(3,easeInOut,0.12)"); |
| 318 | + expect(parseWiggleEase(onCustomEaseCommit.mock.lastCall![0])).toEqual({ |
| 319 | + wiggles: 3, |
| 320 | + type: "easeInOut", |
| 321 | + amplitude: 0.12, |
| 322 | + }); |
| 323 | + expect(onCustomEaseCommit).toHaveBeenCalledTimes(3); |
| 324 | + |
| 325 | + act(() => root.unmount()); |
| 326 | + }); |
| 327 | + |
| 328 | + it("keeps an optimistic mode visible through its canonical prop round-trip", () => { |
| 329 | + const { host, root, onCustomEaseCommit, renderEase } = renderControlledSection(); |
| 330 | + |
| 331 | + clickMode(host, "spring"); |
| 332 | + expect(host.querySelector('[data-ease-mode="spring"]')?.getAttribute("aria-checked")).toBe( |
| 333 | + "true", |
| 334 | + ); |
| 335 | + expect(host.querySelector('[aria-label="Spring bounce"]')).not.toBeNull(); |
| 336 | + |
| 337 | + renderEase("spring(0.42)"); |
| 338 | + expect(host.querySelector('[data-ease-mode="spring"]')?.getAttribute("aria-checked")).toBe( |
| 339 | + "true", |
| 340 | + ); |
| 341 | + expect(host.querySelector('[aria-label="Spring bounce"]')).not.toBeNull(); |
| 342 | + expect(onCustomEaseCommit).toHaveBeenCalledExactlyOnceWith("spring(0.42)"); |
| 343 | + |
| 344 | + act(() => root.unmount()); |
| 345 | + }); |
| 346 | + |
| 347 | + // Two switches before the first commit round-trips: the commits serialize, so |
| 348 | + // the older value arrives while the newer one is still in flight. Repainting |
| 349 | + // it would flash wiggle, spring, wiggle in the panel. |
| 350 | + it("ignores an older in-flight commit arriving after a newer switch", () => { |
| 351 | + const { host, root, renderEase } = renderControlledSection(); |
| 352 | + |
| 353 | + clickMode(host, "spring"); |
| 354 | + clickMode(host, "wiggle"); |
| 355 | + renderEase("spring(0.42)"); |
| 356 | + |
| 357 | + expect(host.querySelector('[data-ease-mode="wiggle"]')?.getAttribute("aria-checked")).toBe( |
| 358 | + "true", |
| 359 | + ); |
| 360 | + |
| 361 | + renderEase("wiggle(3,easeInOut,0.12)"); |
| 362 | + expect(host.querySelector('[data-ease-mode="wiggle"]')?.getAttribute("aria-checked")).toBe( |
| 363 | + "true", |
| 364 | + ); |
| 365 | + |
| 366 | + act(() => root.unmount()); |
| 367 | + }); |
| 368 | + |
| 369 | + // The commit is fire-and-forget: a rejected write or one that lands as a |
| 370 | + // no-op never changes `ease`, so nothing else can retire the optimistic |
| 371 | + // value and the panel would keep claiming a curve that was never saved. |
| 372 | + it("falls back to the committed ease when the commit never round-trips", () => { |
| 373 | + vi.useFakeTimers(); |
| 374 | + try { |
| 375 | + const { host, root } = renderControlledSection("power2.out"); |
| 376 | + |
| 377 | + clickMode(host, "spring"); |
| 378 | + expect(host.querySelector('[data-ease-mode="spring"]')?.getAttribute("aria-checked")).toBe( |
| 379 | + "true", |
| 380 | + ); |
| 381 | + |
| 382 | + act(() => vi.advanceTimersByTime(2000)); |
| 383 | + |
| 384 | + expect(host.querySelector('[data-ease-mode="spring"]')?.getAttribute("aria-checked")).toBe( |
| 385 | + "false", |
| 386 | + ); |
| 387 | + expect(host.querySelector('[data-ease-mode="curve"]')?.getAttribute("aria-checked")).toBe( |
| 388 | + "true", |
| 389 | + ); |
| 390 | + |
| 391 | + act(() => root.unmount()); |
| 392 | + } finally { |
| 393 | + vi.useRealTimers(); |
| 394 | + } |
| 395 | + }); |
| 396 | + |
| 397 | + it("replaces an optimistic mode when the canonical prop changes externally", () => { |
| 398 | + const { host, root, renderEase } = renderControlledSection(); |
| 399 | + |
| 400 | + clickMode(host, "spring"); |
| 401 | + renderEase("wiggle(2,uniform,0.3)"); |
| 402 | + |
| 403 | + expect(host.querySelector('[data-ease-mode="spring"]')?.getAttribute("aria-checked")).toBe( |
| 404 | + "false", |
| 405 | + ); |
| 406 | + expect(host.querySelector('[data-ease-mode="wiggle"]')?.getAttribute("aria-checked")).toBe( |
| 407 | + "true", |
| 408 | + ); |
| 409 | + expect(host.querySelector('[aria-label="Wiggle count"]')).not.toBeNull(); |
| 410 | + expect(host.querySelector('[aria-label="Spring bounce"]')).toBeNull(); |
300 | 411 |
|
301 | 412 | act(() => root.unmount()); |
302 | 413 | }); |
|
0 commit comments