An open source design system that's fully customizable and agent ready
TypeScript
test(interaction-harness): a block can start on a pseudo-element (#10460)
Christopher Chedeau2bf084d6fc38
diff --git a/internal/interaction-harness/README.md b/internal/interaction-harness/README.mdindex 0b825c0d0d..1cb892ed5b 100644--- a/internal/interaction-harness/README.md+++ b/internal/interaction-harness/README.md@@ -199,7 +199,10 @@ only before its curve's held tail (keyframes that end in a run of identicalvalues, read as `waitForRest` reads them): when the only animation runningthere started while the page waited, or the wait times out with only suchanimations, the page looked too late, and the wait rejects with-`BusyWindowTooLateError`. The `verdict` judges the+`BusyWindowTooLateError`. With `pseudoElement` (`'::before'` or+`'::after'`), only that pseudo-element's animations on `during` itself count,+as `sampleCurve` reads them: a child's animation, such as a switch thumb's+press, does not start the block. The `verdict` judges theframes the screencast delivered, whatever its rate (a loaded machine's candeliver one frame in five or six), and tolerates nothing a frozen animationdoes: at least one frame in the judged part, no stall longer than three of thediff --git a/internal/interaction-harness/src/busy.ts b/internal/interaction-harness/src/busy.tsindex 2f39f16550..ac3922df27 100644--- a/internal/interaction-harness/src/busy.ts+++ b/internal/interaction-harness/src/busy.ts@@ -399,6 +399,9 @@ export interface BlockedWindow extends BusyWindow {* animation inside `during` counts but the busy-window control's own* (`startBusyControl`, mounted inside a dialog with `within`), so pick an* element that runs nothing else meanwhile.+ * With `pseudoElement`, only that pseudo-element's animations on `during`+ * itself count, as `sampleCurve` reads them. A child's animation, such as a+ * switch thumb's press, then does not start the block.** An animation counts once it is no longer pending and has run at least one* frame (`minProgressMs` below 16 ms is raised to 16). One created in the@@ -443,10 +446,21 @@ export async function blockMainThread({during,minProgressMs = 16,+ pseudoElement,timeout = 5_000,- }: {during?: Locator; minProgressMs?: number; timeout?: number} = {},+ }: {+ during?: Locator;+ minProgressMs?: number;+ pseudoElement?: '::before' | '::after';+ timeout?: number;+ } = {},): Promise<BlockedWindow> {const minProgress = Math.max(minProgressMs, ONE_FRAME_MS);+ if (pseudoElement != null && during == null) {+ throw new TypeError(+ 'blockMainThread: pseudoElement names a part of `during`; pass `during` too',+ );+ }if (during == null) {return page.evaluate(duration => {// The harness's own work: a trace sets it apart (trace.ts HARNESS_READ).@@ -481,7 +495,7 @@ export async function blockMainThread(// wait and not awaited, so the wait starts no later than it did.const defined = page.evaluate(defineCurveEnd);const waited = during.evaluate(- (element, {duration, minProgress, wait}) =>+ (element, {duration, minProgress, pseudo, wait}) =>new Promise<BlockedWindow>((resolve, reject) => {// The harness's own waits run on the real timers: a frozen timer gate// (story.ts) holds only the page's.@@ -507,6 +521,18 @@ export async function blockMainThread(target.closest('[data-harness-control]') != null);};+ // With a pseudo-element, only its own animations on the element.+ const counts = (a: Animation) => {+ if (isControl(a)) {+ return false;+ }+ const effect = a.effect as KeyframeEffect | null;+ return (+ pseudo == null ||+ (effect?.target === element &&+ (effect.pseudoElement ?? null) === pseudo)+ );+ };// Where each animation's curve ends, its held tail excluded.const curveEnd =(@@ -521,7 +547,9 @@ export async function blockMainThread();});// What already ran when the wait began.- const before = new Set(element.getAnimations({subtree: true}));+ const before = new Set(+ element.getAnimations({subtree: true}).filter(counts),+ );const numbersOf = (a: Animation) => ({currentTimeMs: Number(a.currentTime ?? 0),curveEndMs: curveEnd(a),@@ -538,7 +566,7 @@ export async function blockMainThread(.getAnimations({subtree: true}).filter(a =>- !isControl(a) &&+ counts(a) &&a.playState === 'running' &&!a.pending &&Number(a.currentTime ?? 0) >= minProgress,@@ -585,7 +613,7 @@ export async function blockMainThread(};frame(tick);}),- {duration: ms, minProgress, wait: timeout},+ {duration: ms, minProgress, pseudo: pseudoElement ?? null, wait: timeout},);try {const [, held] = await Promise.all([defined, waited]);diff --git a/internal/interaction-harness/src/harness.interaction.browser.spec.ts b/internal/interaction-harness/src/harness.interaction.browser.spec.tsindex 4ac20c8f76..1e27af881c 100644--- a/internal/interaction-harness/src/harness.interaction.browser.spec.ts+++ b/internal/interaction-harness/src/harness.interaction.browser.spec.ts@@ -5289,6 +5289,51 @@ test.describe('storyboards', {tag: '@storyboard'}, () => {expect(window.from).toBeGreaterThan(startedAt);});+ test("blockMainThread with during and a pseudoElement starts on that pseudo-element's animation, not on a child's earlier one", async ({+ page,+ }) => {+ await fixture(page);+ const box = page.locator('#box');+ // A child, as a switch's thumb, and a fade on the box's ::after, as the+ // switch track's.+ await box.evaluate(el => {+ const style = document.createElement('style');+ style.textContent =+ '#box::after{content:"";position:absolute;inset:0;background:#000;opacity:0}';+ document.head.append(style);+ const child = document.createElement('div');+ child.style.cssText = 'width:20px;height:20px;background:#c33';+ el.append(child);+ });+ const blocking = blockMainThread(page, 100, {+ during: box,+ pseudoElement: '::after',+ minProgressMs: 50,+ timeout: 3_000,+ });+ await page.waitForTimeout(100);+ // The child's press starts first, and the fade 300 ms after it.+ const fadeStartedAt = await box.evaluate(async el => {+ (el.firstElementChild as HTMLElement).animate(+ [{scale: '1'}, {scale: '0.9'}],+ {duration: 1_000, fill: 'forwards'},+ );+ await new Promise(resolve => setTimeout(resolve, 300));+ el.animate([{opacity: 0}, {opacity: 1}], {+ duration: 1_000,+ pseudoElement: '::after',+ });+ return performance.timeOrigin + performance.now();+ });+ const window = await blocking;+ // The block began once the fade had run 50 ms, never on the child.+ expect(window.from).toBeGreaterThanOrEqual(fadeStartedAt + 50);+ // A pseudo-element names a part of `during`, so it needs one.+ await expect(+ blockMainThread(page, 100, {pseudoElement: '::after'}),+ ).rejects.toThrow(/pass `during` too/);+ });+test('blockMainThread with during names a late block when the run it saw held ends before the wait times out', async ({page,}) => {