Skip to content

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

TypeScript

test(interaction-harness): sampleCurve samples a ::before or ::after (#10052)

Cindy Zhang6fc2bfffa1a3
diff --git a/internal/interaction-harness/README.md b/internal/interaction-harness/README.md
index f31790753f..aae238bf65 100644
--- a/internal/interaction-harness/README.md
+++ b/internal/interaction-harness/README.md
@@ -129,6 +129,9 @@ seeking and sampling run a curve forward. The recorder keeps one entry per
`Animation` object, so one reversed or replayed mid-run is misread: a watch
taken before the reversal rejects, and an unwatched wait returns at once with
a curve dated before it. Take a fresh animation for each run a proof reads.
+`sampleCurve(locator, times, {pseudoElement: '::before'})` samples the
+element's `::before` or `::after` instead: its presented values, labelled by
+that pseudo-element's own animations.
**Waiting for an animation.** The page records every animation from the first
call into `curves.ts` on it (and on every later document): a frame loop on the
diff --git a/internal/interaction-harness/src/curves.ts b/internal/interaction-harness/src/curves.ts
index a60c059ba3..20c71ba332 100644
--- a/internal/interaction-harness/src/curves.ts
+++ b/internal/interaction-harness/src/curves.ts
@@ -897,7 +897,8 @@ export interface CurveSample {
* The time the sampled element presents: `t`, or just inside the end of
* its own animations when the sample was taken at or past it (see
* `seekAnimations`). Read from the element's own animations only (from
- * every held one if none targets it).
+ * every held one if none targets it); when sampling a pseudo-element, from
+ * that pseudo-element's.
*/
readonly presentedAt: number;
/**
@@ -931,13 +932,19 @@ export interface CurveSample {
* held origin, as for `seekAnimations`). Hold the animations first. An animation of the
* individual `translate`, `rotate` or `scale` properties is included: they
* are composed with `transform` into one matrix.
+ *
+ * `pseudoElement` samples the element's `::before` or `::after` instead:
+ * its presented values, labelled by its own animations (those whose effect
+ * targets the element with that pseudo-element), as a component that animates
+ * a pseudo-element (`element.animate(keyframes, {pseudoElement})`) draws it.
*/
export async function sampleCurve(
locator: Locator,
times: readonly number[],
+ {pseudoElement}: {pseudoElement?: '::before' | '::after'} = {},
): Promise<CurveSample[]> {
const raw = await locator.evaluate(
- (element, {ts, inset}) => {
+ (element, {ts, inset, pseudo}) => {
const w = window as unknown as {
__harnessHeld?: Animation[];
__harnessHeldStarts?: number[];
@@ -960,12 +967,17 @@ export async function sampleCurve(
'sampleCurve: no held animations (call holdAnimations first)',
);
}
- // The properties each held animation of this element animates. A
- // `::before` or `::after` animation is not the element's own: its
- // clock would label the element's samples with another box's time.
+ // The properties each held animation of the sampled box animates: the
+ // element's own, or its pseudo-element's when one is sampled. Another
+ // box's animation (the element's `::before` when the element is
+ // sampled) is not its own: its clock would label the samples with
+ // another box's time.
const own = held.map(animation => {
const effect = animation.effect as KeyframeEffect | null;
- if (effect?.target !== element || effect.pseudoElement != null) {
+ if (
+ effect?.target !== element ||
+ (effect.pseudoElement ?? null) !== pseudo
+ ) {
return null;
}
const properties = new Set<string>();
@@ -1014,7 +1026,10 @@ export async function sampleCurve(
const or = (value: number, fallback: number) =>
Number.isFinite(value) ? value : fallback;
const presentedAt = or(presentedOwn, or(presented, t));
- const style = getComputedStyle(element);
+ const style =
+ pseudo == null
+ ? getComputedStyle(element)
+ : getComputedStyle(element, pseudo);
// translate · rotate · scale · transform, as CSS applies them (all
// about the same transform origin, which `transform` excludes too).
// A percentage resolves against the border box, unrounded
@@ -1118,7 +1133,7 @@ export async function sampleCurve(
};
});
},
- {ts: [...times], inset: END_INSET_MS},
+ {ts: [...times], inset: END_INSET_MS, pseudo: pseudoElement ?? null},
);
return raw.map(r => ({
t: r.t,
diff --git a/internal/interaction-harness/src/harness.interaction.browser.spec.ts b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
index 83c5b7e218..4d97e53f9c 100644
--- a/internal/interaction-harness/src/harness.interaction.browser.spec.ts
+++ b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
@@ -2596,6 +2596,81 @@ test.describe('curve sampling', () => {
});
});
+// A full-size veil on the fixture's box, drawn by its ::before.
+const VEIL_STYLE =
+ "#box::before { content: ''; position: absolute; inset: 0; background: #000; opacity: 0; }";
+
+test.describe('pseudo-element animations', () => {
+ test('sampleCurve reads a ::before animation when given its pseudo-element', async ({
+ page,
+ }) => {
+ await fixture(page);
+ const box = page.locator('#box');
+ await box.evaluate((el, veil) => {
+ const style = document.createElement('style');
+ style.textContent = veil;
+ document.head.append(style);
+ el.animate(
+ [
+ {opacity: 0, transform: 'translateY(0px)'},
+ {opacity: 1, transform: 'translateY(40px)'},
+ ],
+ {duration: 300, easing: 'linear', pseudoElement: '::before'},
+ );
+ }, VEIL_STYLE);
+ await holdAnimations(page, box);
+ const samples = await sampleCurve(box, [0, 75, 150, 225, 300], {
+ pseudoElement: '::before',
+ });
+ // Labelled by the veil's own clock: the last read just inside its end.
+ expect(samples.at(-1)).toMatchObject({t: 300, presentedAt: 299.5});
+ const fade = compareCurve(curveOf(samples, 'opacity'), t => t / 300, 0.005);
+ expect(fade.maxError, JSON.stringify(fade.worst)).toBeLessThanOrEqual(
+ 0.005,
+ );
+ const move = compareCurve(
+ curveOf(samples, 'translateY'),
+ t => (40 * t) / 300,
+ 0.05,
+ );
+ expect(move.maxError, JSON.stringify(move.worst)).toBeLessThanOrEqual(0.05);
+ // Without it, the box itself, which does not move.
+ const [boxAt150] = await sampleCurve(box, [150]);
+ expect(boxAt150.opacity).toBe(1);
+ expect(boxAt150.transform.translateY).toBe(0);
+ });
+
+ test('waitForRest waits out a ::before animation', async ({page}) => {
+ await fixture(page);
+ const box = page.locator('#box');
+ await box.evaluate((el, veil) => {
+ const style = document.createElement('style');
+ style.textContent = veil;
+ document.head.append(style);
+ (window as unknown as {__veil: Animation}).__veil = el.animate(
+ [{opacity: 0}, {opacity: 1}],
+ {
+ duration: 600,
+ easing: 'linear',
+ fill: 'forwards',
+ pseudoElement: '::before',
+ },
+ );
+ }, VEIL_STYLE);
+ // Its subtree read includes the element's pseudo-elements.
+ await waitForRest(box);
+ const veil = await box.evaluate(element => {
+ const a = (window as unknown as {__veil: Animation}).__veil;
+ return {
+ playState: a.playState,
+ currentTime: Number(a.currentTime),
+ opacity: getComputedStyle(element, '::before').opacity,
+ };
+ });
+ expect(veil).toMatchObject({playState: 'finished', opacity: '1'});
+ });
+});
+
test.describe('holding', () => {
test('an armed hold takes a short animation on its first frames, from the page itself', async ({
page,