An open source design system that's fully customizable and agent ready
TypeScript
test(motion, SideNav): the rail proofs judge the frames a loaded page draws, not a frame rate (#9518)
Christopher Chedeaue02c9552c739
diff --git a/packages/core/src/SideNav/__tests__/SideNavRailMotion.interaction.browser.spec.ts b/packages/core/src/SideNav/__tests__/SideNavRailMotion.interaction.browser.spec.tsindex 6ae0fd3c5a..b083751aaf 100644--- a/packages/core/src/SideNav/__tests__/SideNavRailMotion.interaction.browser.spec.ts+++ b/packages/core/src/SideNav/__tests__/SideNavRailMotion.interaction.browser.spec.ts@@ -64,7 +64,14 @@ async function openStory(page: Page, story: string): Promise<void> {/** What the frames of one motion showed, sampled every animation frame. */interface Frames {- readonly frames: number;+ /**+ * Positions of the content's glide judged on its own clock, one every 50 ms.+ * The sampler holds the page's animations and seeks them to each position.+ * The count does not depend on how many frames a loaded runner draws.+ */+ readonly heldPositions: number;+ /** The content glide's end on its own clock, in ms. */+ readonly glideEndMs: number;/** The largest distance between the rail's shown edge and the content's. */readonly maxGap: number;/** Whether any running animation changed a width. */@@ -95,7 +102,9 @@ async function sampleMotion(): Promise<Frames> {await page.evaluate(() => {const state = {- frames: 0,+ heldPositions: 0,+ glideEndMs: 0,+ held: false,maxGap: 0,animatesWidth: false,copiesHidden: true,@@ -126,15 +135,86 @@ async function sampleMotion(element.getBoundingClientRect().right - (inset ? Number(inset[1]) : 0));};+ const railCopies = () =>+ [...document.querySelectorAll('[inert]')].filter(+ element => element.querySelector('nav') != null,+ );+ // The sampler judges the glide once, on its own clock, as soon as it+ // rides. It holds every running animation and seeks each one by its own+ // start. Their offsets then match what a frame at that time shows.+ const holdAndRead = () => {+ if (state.held || !state.running) {+ return;+ }+ const glide = column+ .getAnimations()+ .find(+ animation =>+ animation.playState === 'running' && animation.startTime != null,+ );+ const end = Number(glide?.effect?.getComputedTiming().endTime);+ if (glide == null || !Number.isFinite(end)) {+ return;+ }+ state.held = true;+ state.glideEndMs = end;+ const start = Number(glide.startTime);+ // A script pause overrides a CSS animation's play state permanently, so+ // the hold skips CSS animations and transitions.+ const held = document+ .getAnimations()+ .filter(+ animation =>+ animation.playState === 'running' &&+ animation.startTime != null &&+ !('animationName' in animation) &&+ !('transitionProperty' in animation),+ )+ .map(animation => ({+ animation,+ startTime: Number(animation.startTime),+ }));+ for (const {animation} of held) {+ animation.pause();+ }+ for (let time = 0; time <= end; time += 50) {+ for (const {animation, startTime} of held) {+ animation.currentTime = start + time - startTime;+ }+ const edge = Math.max(+ shownRight(panel),+ ...railCopies().map(shownRight),+ );+ state.maxGap = Math.max(+ state.maxGap,+ Math.abs(edge - column.getBoundingClientRect().left),+ );+ state.heldPositions++;+ }+ // Each animation gets its own start time back, so it plays on where it+ // would have been. The hold does not delay it.+ for (const {animation, startTime} of held) {+ animation.startTime = startTime;+ }+ };+ // Read the glide in the press's own task, once the change's animations+ // ride. On a loaded runner, the collapse's work can hold the main thread+ // for the whole glide, so the page draws no frame of it. If the glide is+ // not riding yet, the next frame reads it.+ window.addEventListener(+ 'click',+ () => {+ queueMicrotask(holdAndRead);+ },+ {once: true},+ );const glides = new WeakSet<Animation>();const sample = () => {if (!state.running) {return;}- state.frames++;- const copies = [...document.querySelectorAll('[inert]')].filter(- element => element.querySelector('nav') != null,- );+ holdAndRead();+ const copies = railCopies();state.copiesSeen = Math.max(state.copiesSeen, copies.length);state.wrapCopiesSeen = Math.max(state.wrapCopiesSeen,@@ -222,11 +302,13 @@ async function sampleMotion(}).railFrames;state.running = false;- const {frames, maxGap, animatesWidth, copiesHidden, copiesSeen} = state;+ const {heldPositions, glideEndMs, maxGap, animatesWidth} = state;+ const {copiesHidden, copiesSeen} = state;const {wrapCopiesSeen} = state;const edges = state.edges.size;return {- frames,+ heldPositions,+ glideEndMs,maxGap,animatesWidth,copiesHidden,@@ -237,9 +319,14 @@ async function sampleMotion(});}-/** The rail's width and whether its labels show, at rest. */+/**+ * The rail's width, whether its labels show, and what still runs, at rest.+ * Animations on the toggle element itself are its press feedback, not the+ * rail's motion, as in `sampleMotion` and `recordMotion`. On a loaded runner,+ * that feedback can still run after the rail's motion ends.+ */async function railAtRest(page: Page) {- return page.evaluate(() => {+ return toggleButton(page).evaluate(toggle => {const panel = document.querySelector('.astryx-layout-panel') as HTMLElement;const label = [...panel.querySelectorAll('span, div')].find(element =>@@ -258,7 +345,12 @@ async function railAtRest(page: Page) {width: Math.round(panel.getBoundingClientRect().width),labelsShown: shown,copies: document.querySelectorAll('[inert]').length,- running: document.getAnimations().length,+ running: document+ .getAnimations()+ .filter(+ animation =>+ (animation.effect as KeyframeEffect | null)?.target !== toggle,+ ).length,};});}@@ -422,7 +514,12 @@ test.describe('component:SideNav FR6 and component:AppShell FR17 under a finger'const collapse = await sampleMotion(page, async () => {await toggleButton(page).tap();});- expect(collapse.frames).toBeGreaterThan(20);+ // The sampler judges the whole glide on its own clock, every 50 ms. It+ // also judges every frame the page draws, however many the runner gets.+ expect(collapse.glideEndMs).toBeGreaterThan(0);+ expect(collapse.heldPositions).toBe(+ Math.floor(collapse.glideEndMs / 50) + 1,+ );expect(collapse.maxGap).toBeLessThanOrEqual(1);expect(collapse.edges).toBeGreaterThan(5);expect(collapse.animatesWidth).toBe(false);diff --git a/packages/core/src/motion/__tests__/animatedLayout.interaction.browser.spec.ts b/packages/core/src/motion/__tests__/animatedLayout.interaction.browser.spec.tsindex 2a62e97e6c..aaaa7214ed 100644--- a/packages/core/src/motion/__tests__/animatedLayout.interaction.browser.spec.ts+++ b/packages/core/src/motion/__tests__/animatedLayout.interaction.browser.spec.ts@@ -2238,8 +2238,14 @@ test.describe('spec:AST-105 FR9 (the edge reveal)', () => {edge: 'inline-end',rideWith: column,});- for (let i = 0; i < 2; i++) {- await frame();+ await new Promise<void>(resolve => {+ queueMicrotask(resolve);+ });+ // Hold the collapse at fixed times on its own clock, once it rides.+ // Late frames then cannot end it before these reads or move it+ // between them.+ for (const ms of [20, 40, 60, 80, 100, 120]) {+ window.holdAt(ms);const copy = copyOf();copies = Math.max(copies, copy == null ? 0 : 1);if (copy != null) {@@ -2248,12 +2254,6 @@ test.describe('spec:AST-105 FR9 (the edge reveal)', () => {}// Expanded again 120 ms into the collapse, from the width it shows// then.- window.holdAt(120);- const copy = copyOf();- copies = Math.max(copies, copy == null ? 0 : 1);- if (copy != null) {- read('collapsing', copy);- }const shown = motion.shown()?.extent ?? null;motion.end();hold = AL.holdLayoutEdge(layout, panel, {@@ -2272,6 +2272,16 @@ test.describe('spec:AST-105 FR9 (the edge reveal)', () => {queueMicrotask(resolve);});window.resumeAll();+ // Read where the expand starts in the task that makes it, before any+ // frame.+ read('expand start', panel);+ // Hold the expand at fixed times too, so late frames cannot skip its+ // edge check.+ for (const ms of [20, 40, 60, 80, 100, 120]) {+ window.holdAt(ms);+ read('expand held', panel);+ }+ window.resumeAll();for (let i = 0; i < 60; i++) {await frame();read('expanding', panel);@@ -2281,24 +2291,36 @@ test.describe('spec:AST-105 FR9 (the edge reveal)', () => {expect(result.copies).toBe(1);// The copy is gone once the motion ends.expect(result.left).toBe(0);+ // The case reads the collapse six times and the expand six times on+ // their own clock, plus the expand's start. It also judges every drawn+ // expanding frame, however many the page draws.+ const count = (phase: string) =>+ result.frames.filter(f => f.phase === phase).length;+ expect(count('collapsing')).toBe(6);+ expect(count('expand start')).toBe(1);+ expect(count('expand held')).toBe(6);const moving = result.frames.filter(f => f.shown > 48.5 && f.shown < 259.5,);- expect(moving.length).toBeGreaterThan(5);+ // Every read on the motion's own clock is in mid-motion.+ expect(moving.filter(f => f.phase !== 'expanding')).toHaveLength(13);for (const f of moving) {expect(Math.abs(f.beside - f.edge), JSON.stringify(f)).toBeLessThan(1.5,);}reportSkew(moving, browserName);- // The expand starts where the collapse got to.+ // The expand starts where the collapse got to. Read in that task, the+ // two match to 0.0 px.const lastCollapse = result.frames.filter(f => f.phase === 'collapsing').at(-1) as EdgeFrame;- const firstExpand = result.frames.find(- f => f.phase === 'expanding',+ const expandStart = result.frames.find(+ f => f.phase === 'expand start',) as EdgeFrame;- expect(Math.abs(firstExpand.shown - lastCollapse.shown)).toBeLessThan(30);+ expect(Math.abs(expandStart.shown - lastCollapse.shown)).toBeLessThan(+ 1.5,+ );});}@@ -2362,8 +2384,14 @@ test.describe('spec:AST-105 FR9 (the edge reveal)', () => {};await frame();change(true);- for (let i = 0; i < 2; i++) {- await frame();+ await new Promise<void>(resolve => {+ queueMicrotask(resolve);+ });+ // Hold the collapse at fixed times on its own clock, once it rides.+ // Late frames then cannot end it before these reads or move it+ // between them.+ for (const ms of [20, 40, 60, 80, 100]) {+ window.holdAt(ms);read('collapsing');}// Opened again 120 ms into the collapse, as the page shows it then.