Skip to content

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

TypeScript

test(core): read the cross-fade landing at its write; pace snap releases (#10548)

Christopher Chedeau521cfb4830cb
diff --git a/packages/core/src/animator/__tests__/createAnimator.interaction.browser.spec.ts b/packages/core/src/animator/__tests__/createAnimator.interaction.browser.spec.ts
index 47a48d60d3..b1599595d3 100644
--- a/packages/core/src/animator/__tests__/createAnimator.interaction.browser.spec.ts
+++ b/packages/core/src/animator/__tests__/createAnimator.interaction.browser.spec.ts
@@ -155,10 +155,30 @@ test('under Reduce Motion on a touch screen, a move cross-fades: the card jumps
// the bottom of the fade, and no seek of the running animations shows it.
// A first cross-fade warms the page up (a fresh WebKit page skips frames
// for about 100 ms); the second, back home, is the one measured.
- const frames = await page.evaluate(async travel => {
+ const {frames, landed} = await page.evaluate(async travel => {
const card = document.getElementById('card') as HTMLElement;
await window.card.animate({x: travel});
const samples: {left: number; opacity: number}[] = [];
+ // The travel lands with a style write, so the card's opacity is read as
+ // that write runs. The next frame can come well into the fade back in on
+ // a loaded runner, and a landing that runs late enough appears at once.
+ // Only a change of `translate` counts: the animator writes other inline
+ // styles too (`will-change`).
+ const landed: number[] = [];
+ const {style} = card;
+ const setProperty = style.setProperty.bind(style);
+ style.setProperty = (property, value, priority) => {
+ if (property !== 'translate') {
+ setProperty(property, value, priority);
+ return;
+ }
+ const was = style.getPropertyValue(property);
+ const opacity = Number(getComputedStyle(card).opacity);
+ setProperty(property, value, priority);
+ if (style.getPropertyValue(property) !== was) {
+ landed.push(opacity);
+ }
+ };
let done = false;
void window.card.animate({x: 0}).then(() => {
done = true;
@@ -170,7 +190,8 @@ test('under Reduce Motion on a touch screen, a move cross-fades: the card jumps
opacity: Number(getComputedStyle(card).opacity),
});
}
- return samples;
+ delete (style as {setProperty?: unknown}).setProperty;
+ return {frames: samples, landed};
}, TRAVEL);
// Never between the ends: the travel is substituted, not slowed.
for (const {left: x} of frames) {
@@ -189,7 +210,9 @@ test('under Reduce Motion on a touch screen, a move cross-fades: the card jumps
for (let i = 1; i < after.length; i++) {
expect(after[i]).toBeGreaterThanOrEqual(after[i - 1] - 0.01);
}
- expect(Math.min(before[before.length - 1], after[0])).toBeLessThan(0.1);
+ // It lands once, while transparent.
+ expect(landed).toHaveLength(1);
+ expect(landed[0]).toBeLessThan(0.1);
expect(frames[frames.length - 1].opacity).toBeCloseTo(1, 2);
});
diff --git a/packages/core/src/dragToSnapPoints/__tests__/createDragToSnapPoints.interaction.browser.spec.ts b/packages/core/src/dragToSnapPoints/__tests__/createDragToSnapPoints.interaction.browser.spec.ts
index 046076e8fa..fc0ab46e02 100644
--- a/packages/core/src/dragToSnapPoints/__tests__/createDragToSnapPoints.interaction.browser.spec.ts
+++ b/packages/core/src/dragToSnapPoints/__tests__/createDragToSnapPoints.interaction.browser.spec.ts
@@ -35,6 +35,16 @@ test.beforeAll(async () => {
const POINTS = [0, 240, 480];
+// WebKit stamps a touch when it reaches the page, so a loaded runner can hand
+// the page another release than the script's. Each play that depends on its
+// release fails by name when the page's strays past what its settle allows.
+// The flick's script releases at 1,375 px/s: within a quarter of it, the page
+// reads at least 1,031 px/s, past the flick speed (1,008).
+const FLICK_TOLERANCE = 0.25;
+// The move back releases at -303 px/s: within 90% of it, the page still reads
+// it moving back, all its settle depends on.
+const MOVE_BACK_TOLERANCE = 0.9;
+
// A real navigation: touch emulation exposes the Touch Events API to
// documents it creates.
async function open(page: Page): Promise<TouchDriver> {
@@ -60,18 +70,22 @@ async function open(page: Page): Promise<TouchDriver> {
return touchDriver(page);
}
-/** A finger from (x, y) moving by `dy` over `steps` steps of `ms`, then lifting. */
+/**
+ * A finger from (x, y) moving by `dy` over `steps` steps of `ms`, then lifting
+ * `liftMs` after its last move (one more step by default).
+ */
function dragScript(
x: number,
y: number,
moves: ReadonlyArray<number>,
ms: number,
+ liftMs = ms,
) {
const steps = [{t: 0, fingers: [{x, y, id: 1}]}];
moves.forEach((dy, i) => {
steps.push({t: (i + 1) * ms, fingers: [{x, y: y + dy, id: 1}]});
});
- steps.push({t: (moves.length + 1) * ms, fingers: []});
+ steps.push({t: moves.length * ms + liftMs, fingers: []});
return steps;
}
@@ -121,7 +135,10 @@ test('a slow drag settles on the nearest snap point', async ({page}) => {
test('a quick flick carries to the next snap point', async ({page}) => {
const driver = await open(page);
// 110 px in about 80 ms: past 1008 px/s, yet nearer 0 than 240.
- await driver.play(dragScript(120, 100, linear(110, 5), 16));
+ await driver.play(dragScript(120, 100, linear(110, 5), 16), {
+ pacing: 'spacing',
+ releaseTolerance: FLICK_TOLERANCE,
+ });
expect(await settled(page)).toEqual([1]);
expect(await panelTop(page)).toBeCloseTo(40 + 240, 0);
});
@@ -131,8 +148,13 @@ test('a release moving back returns to the snap point the drag started from', as
}) => {
const driver = await open(page);
// Down 300 px, then back up 40 px over the last frames before the lift.
+ // It lifts a frame after its last move: a lift past the stillness
+ // interval (40 ms) would read a resting finger, not a move back.
const moves = [...linear(300, 20), 290, 280, 270, 260];
- await driver.play(dragScript(120, 100, moves, 33));
+ await driver.play(dragScript(120, 100, moves, 33, 16), {
+ pacing: 'spacing',
+ releaseTolerance: MOVE_BACK_TOLERANCE,
+ });
expect(await settled(page)).toEqual([0]);
expect(await panelTop(page)).toBeCloseTo(40, 0);
});