Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

test(core): the edge rides judge their curves at fixed times (#10514)

Christopher Chedeau6a8338b40e50
diff --git a/packages/core/src/motion/__tests__/animatedLayout.interaction.browser.spec.ts b/packages/core/src/motion/__tests__/animatedLayout.interaction.browser.spec.ts
index f2e4739bc8..db6403e081 100644
--- a/packages/core/src/motion/__tests__/animatedLayout.interaction.browser.spec.ts
+++ b/packages/core/src/motion/__tests__/animatedLayout.interaction.browser.spec.ts
@@ -20,6 +20,7 @@
import {fileURLToPath} from 'node:url';
import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright';
+import {requireFrames} from '@astryxdesign/interaction-harness';
import {build} from 'esbuild';
import {cubicBezier} from '../bezier';
import {LATE_START_MS} from '../frameBlock';
@@ -2647,7 +2648,7 @@ test.describe('spec:AST-105 FR9 (the edge reveal)', () => {
const content = document.querySelector('#content') as HTMLElement;
const a = document.querySelector('#a') as HTMLElement;
const layout = AL.createAnimatedLayout(() => area);
- const frames: EdgeFrame[] = [];
+ const frames: (EdgeFrame & {t: number})[] = [];
const bottoms: number[] = [];
const read = (phase: string) => {
const box = content.getBoundingClientRect();
@@ -2655,6 +2656,7 @@ test.describe('spec:AST-105 FR9 (the edge reveal)', () => {
const beside = window.glideBoxOf(a);
frames.push({
phase,
+ t: performance.timeOrigin + performance.now(),
edge: box.top + inset.top,
beside: beside.bottom,
shown: box.height - inset.top - inset.bottom,
@@ -2662,7 +2664,7 @@ test.describe('spec:AST-105 FR9 (the edge reveal)', () => {
Math.abs(window.computedInsetOf(content).top - inset.top) +
Math.abs(a.getBoundingClientRect().top - beside.top),
});
- if (phase === 'closing') {
+ if (phase.startsWith('closing')) {
bottoms.push(box.bottom);
}
};
@@ -2678,9 +2680,27 @@ test.describe('spec:AST-105 FR9 (the edge reveal)', () => {
content.style.display = 'block';
opening.animate();
AL.playLayoutChange(layout, content, {edge: 'block-start'});
+ await new Promise<void>(resolve => {
+ queueMicrotask(resolve);
+ });
+ // Held at fixed times on its own clock, once it rides, so the reads
+ // judge the curve however late the frames come. Then the reveal plays
+ // again from its start on real frames.
+ for (const ms of [20, 40, 60, 80, 100]) {
+ window.holdAt(ms);
+ read('opening held');
+ }
+ window.holdAt(0);
+ window.resumeAll();
+ // A frame after the reveal has ended shows it fully open, so it does
+ // not count as a frame of the motion.
+ const revealing = () =>
+ content
+ .getAnimations({subtree: true})
+ .some(animation => animation.playState === 'running');
for (let i = 0; i < 60; i++) {
await frame();
- read('opening');
+ read(revealing() ? 'opening' : 'opening ended');
}
const openBottom = content.getBoundingClientRect().bottom;
// The gap the layout leaves between them at rest (the rows' margins).
@@ -2689,11 +2709,29 @@ test.describe('spec:AST-105 FR9 (the edge reveal)', () => {
// Hidden again: the content before it glides back down over it, its
// top edge on that content, while it stays where it shows.
const closing = AL.reportLayoutChange(layout, () => content);
+ const before = new Set(document.getAnimations());
closing.measureBefore();
closing.animate();
const isHeld = AL.holdLayoutExit(layout, content, () => {}, {
edge: 'block-start',
});
+ await new Promise<void>(resolve => {
+ queueMicrotask(resolve);
+ });
+ // The conceal's own animations, held and played as the reveal's were.
+ const concealing = document.getAnimations().filter(a => !before.has(a));
+ const seek = (ms: number) => {
+ for (const animation of concealing) {
+ animation.pause();
+ animation.currentTime = ms;
+ }
+ };
+ for (const ms of [20, 40, 60, 80, 100]) {
+ seek(ms);
+ read('closing held');
+ }
+ seek(0);
+ window.resumeAll();
for (let i = 0; i < 60; i++) {
await frame();
read('closing');
@@ -2702,8 +2740,6 @@ test.describe('spec:AST-105 FR9 (the edge reveal)', () => {
});
expect(result.isHeld).toBe(true);
const shown = result.frames.filter(f => f.shown > 0.5);
- expect(shown.filter(f => f.phase === 'opening').length).toBeGreaterThan(4);
- expect(shown.filter(f => f.phase === 'closing').length).toBeGreaterThan(4);
for (const f of shown) {
expect(
Math.abs(f.edge - f.beside - result.restGap),
@@ -2715,6 +2751,14 @@ test.describe('spec:AST-105 FR9 (the edge reveal)', () => {
expect(Math.abs(bottom - result.openBottom)).toBeLessThan(1);
}
reportSkew(shown, browserName);
+ // The held reads judge the curves. These prove the page drew both.
+ for (const phase of ['opening', 'closing']) {
+ requireFrames(result.frames, {
+ min: 5,
+ during: `the ${phase === 'opening' ? 'reveal' : 'conceal'} plays`,
+ where: f => f.phase === phase && f.shown > 0.5,
+ });
+ }
});
test('a block-end reveal keeps its edge on the content after it on every frame, and a conceal that reverses it starts from what it shows', async ({
diff --git a/packages/core/src/motion/__tests__/animatedLayoutAdopters.interaction.browser.spec.ts b/packages/core/src/motion/__tests__/animatedLayoutAdopters.interaction.browser.spec.ts
index 7ef0900036..76b9edf5a2 100644
--- a/packages/core/src/motion/__tests__/animatedLayoutAdopters.interaction.browser.spec.ts
+++ b/packages/core/src/motion/__tests__/animatedLayoutAdopters.interaction.browser.spec.ts
@@ -24,6 +24,7 @@ import {
serveStorybook,
type StaticServer,
} from '@astryxdesign/a11y-spec/storybook';
+import {requireFrames} from '@astryxdesign/interaction-harness';
let storybook: StaticServer | undefined;
test.beforeAll(async () => {
@@ -85,6 +86,8 @@ async function mark(page: Page, text: string, id: string): Promise<void> {
interface EdgeRideFrame {
phase: string;
+ /** When the page read it, epoch ms. */
+ t: number;
/** From the revealed content's shown bottom edge to the content after it. */
gap: number;
/** How much of the revealed content shows. */
@@ -239,12 +242,20 @@ async function edgeRide(
const read = (phase: string) => {
const element = content();
if (element == null) {
+ // Gone: the frame still counts in the gaps between frames.
+ frames.push({
+ phase,
+ t: performance.timeOrigin + performance.now(),
+ gap: Number.NaN,
+ shown: 0,
+ });
return;
}
const height = element.offsetHeight;
const hidden = insetBottom(element);
frames.push({
phase,
+ t: performance.timeOrigin + performance.now(),
gap: clockTop(next) - (clockTop(element) + height - hidden),
shown:
getComputedStyle(element).display === 'none' ? 0 : height - hidden,
@@ -269,18 +280,6 @@ async function edgeRide(
const beyond = Math.max(0, openTop - closedTop - height);
button.click();
await settle();
- // Open, then close again 120 ms into the reveal, as the page shows it
- // then.
- button.click();
- for (let i = 0; i < 2; i++) {
- await frame();
- read('opening');
- }
- for (const animation of document.getAnimations()) {
- animation.pause();
- animation.currentTime = 120;
- }
- read('reversed');
const clipOf = () => {
const element = content();
if (element == null) {
@@ -297,15 +296,54 @@ async function edgeRide(
.at(-1) ?? slideOf(element)
);
};
- const opening = clipOf();
+ // Until a click's change commits. A component commits in the click's
+ // microtasks, before any frame, or in a later task.
+ const committed = async (isDone: () => boolean) => {
+ for (let i = 0; i < 10 && !isDone(); i++) {
+ await new Promise<void>(resolve => {
+ queueMicrotask(resolve);
+ });
+ }
+ for (let i = 0; i < 50 && !isDone(); i++) {
+ await new Promise<void>(resolve => {
+ setTimeout(resolve, 0);
+ });
+ }
+ };
+ // Pauses `animations` `ms` into their motion: the page as it shows then,
+ // however far apart the browser draws its frames.
+ const seek = (animations: Animation[], ms: number) => {
+ for (const animation of animations) {
+ animation.pause();
+ animation.currentTime = ms;
+ }
+ };
+ // Open, then close again 120 ms into the reveal, as the page shows it
+ // then. The reveal is read at fixed times on its own clock as it commits,
+ // so a late frame cannot end it before these reads.
button.click();
- // Until the close commits (a component may commit its state in a later
- // task), with every animation still paused where the close found it.
- for (let i = 0; i < 50 && clipOf() === opening; i++) {
- await new Promise<void>(resolve => {
- setTimeout(resolve, 0);
- });
+ await committed(() => clipOf() != null);
+ for (const ms of [20, 40, 60, 80, 100]) {
+ seek(document.getAnimations(), ms);
+ read('opening held');
}
+ // Then the reveal plays on from there for two real frames. At its start
+ // a reveal shows nothing yet, so those frames would show no ride.
+ for (const animation of document.getAnimations()) {
+ animation.play();
+ }
+ for (let i = 0; i < 2; i++) {
+ await frame();
+ read(clipOf() == null ? 'opening ended' : 'opening');
+ }
+ seek(document.getAnimations(), 120);
+ read('reversed');
+ const opening = clipOf();
+ const before = new Set(document.getAnimations());
+ button.click();
+ // Until the close commits, with every animation still paused where the
+ // close found it.
+ await committed(() => clipOf() !== opening);
const conceal = clipOf();
const [start] =
(conceal?.effect as KeyframeEffect | undefined)?.getKeyframes() ?? [];
@@ -317,6 +355,14 @@ async function edgeRide(
(start.clipPath == null
? Math.max(0, -y(start.translate))
: bottomOf(start.clipPath, startBox));
+ // The conceal held at fixed times too, then played from its start on
+ // real frames.
+ const concealing = document.getAnimations().filter(a => !before.has(a));
+ for (const ms of [20, 40, 60, 80, 100]) {
+ seek(concealing, ms);
+ read('closing held');
+ }
+ seek(concealing, 0);
for (const animation of document.getAnimations()) {
if (animation.playState === 'paused') {
animation.play();
@@ -337,7 +383,6 @@ async function edgeRide(
*/
function expectEdgeRide(result: EdgeRide): void {
const shown = result.frames.filter(f => f.shown > 0.5);
- expect(shown.length).toBeGreaterThan(8);
expect(result.beyond).toBeLessThan(8);
for (const f of shown) {
const detail = JSON.stringify({
@@ -348,6 +393,21 @@ function expectEdgeRide(result: EdgeRide): void {
expect(f.gap, detail).toBeLessThan(result.restGap + 1.5);
expect(f.gap, detail).toBeGreaterThan(result.restGap - result.beyond - 1.5);
}
+ // The held reads judge the curves. These prove the page drew the motion.
+ // Its two drawn reveal frames come while the reveal still rides, so the
+ // reversal still finds it.
+ requireFrames(result.frames, {
+ min: 2,
+ during: 'the reveal plays',
+ where: f => f.phase === 'opening',
+ });
+ // With those two, the conceal's 6 make the 8 drawn reads this proof always
+ // required: its 9 shown reads included the held reversal.
+ requireFrames(result.frames, {
+ min: 6,
+ during: 'the conceal plays',
+ where: f => f.phase === 'closing' && f.shown > 0.5,
+ });
const reversed = result.frames.find(f => f.phase === 'reversed');
expect(reversed?.shown).toBeGreaterThan(2);
expect(reversed?.shown).toBeLessThan(result.height - 2);