Skip to content

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

TypeScript

test(Lightbox): touch proofs read the photo without holding the lift (#10406)

Christopher Chedeaudff58aef3000
diff --git a/packages/core/src/Lightbox/__tests__/LightboxZoom.interaction.browser.spec.ts b/packages/core/src/Lightbox/__tests__/LightboxZoom.interaction.browser.spec.ts
index ba0de77744..64280037ef 100644
--- a/packages/core/src/Lightbox/__tests__/LightboxZoom.interaction.browser.spec.ts
+++ b/packages/core/src/Lightbox/__tests__/LightboxZoom.interaction.browser.spec.ts
@@ -60,8 +60,10 @@ import {
type ActiveAnimation,
type TouchDriver,
type TouchFrame,
+ toTouchEvents,
type TouchScript,
} from '@astryxdesign/interaction-harness';
+import {recordLiftOffset} from './liftOffset';
const storybook = withStorybook(test);
@@ -448,6 +450,9 @@ test('a pinch keeps the photo point under the fingers, and the page does not zoo
const errors: number[] = [];
const scales: number[] = [];
let moves = 0;
+ const lastMove = toTouchEvents(moved)
+ .map(step => step.type)
+ .lastIndexOf('touchMove');
// The centroid of the touches the page received (the engine may round a
// scripted position), which is what the stage follows.
await page.evaluate(() => {
@@ -464,11 +469,17 @@ test('a pinch keeps the photo point under the fingers, and the page does not zoo
);
});
await driver.play(moved, {
- onEvent: async step => {
+ onEvent: async (step, index) => {
if (step.type !== 'touchMove' || step.fingers.length < 2) {
return;
}
moves++;
+ // A read after the last move holds the lift back by its round trip.
+ // Past the 40 ms stillness interval, the page then reads a resting
+ // finger.
+ if (index === lastMove) {
+ return;
+ }
// The photo and the touches the page received, in one evaluate. The
// page takes a touch with its frame, after the dispatch returns. So two
// reads could straddle one on a busy page: the photo from one move and
@@ -795,24 +806,17 @@ test('a zoomed flick coasts and stops at the far edge', async ({page}) => {
),
);
await waitForRest(photo);
- const zoomed = await state();
const script = flick(mid, {
velocity: {x: -1500, y: 0},
durationMs: 80,
hz: 120,
});
- let released = zoomed;
- await driver.play(script, {
- onEvent: async step => {
- if (step.type === 'touchMove') {
- released = await state();
- }
- },
- });
+ const liftOffset = await recordLiftOffset(photo);
+ await driver.play(script);
await frames(page, 6);
const coasting = await state();
// The photo kept moving after the lift.
- expect(coasting.x).toBeLessThan(released.x - 5);
+ expect(coasting.x).toBeLessThan((await liftOffset()) - 5);
await waitForRest(photo, {timeout: 8_000});
const rest = await state();
// At the right edge: the photo's right edge on the stage's.
diff --git a/packages/core/src/Lightbox/__tests__/LightboxZoomModes.interaction.browser.spec.ts b/packages/core/src/Lightbox/__tests__/LightboxZoomModes.interaction.browser.spec.ts
index d44d19d18b..c48ee7bc13 100644
--- a/packages/core/src/Lightbox/__tests__/LightboxZoomModes.interaction.browser.spec.ts
+++ b/packages/core/src/Lightbox/__tests__/LightboxZoomModes.interaction.browser.spec.ts
@@ -40,6 +40,7 @@ import {
withStorybook,
type TouchScript,
} from '@astryxdesign/interaction-harness';
+import {recordLiftOffset} from './liftOffset';
const storybook = withStorybook(test);
@@ -544,14 +545,8 @@ test('under Reduce Motion a zoomed flick coasts on and settles at the edge, as o
await driver.play(doubleTap(mid));
await waitForRest(img);
await frames(page, 2);
- let lifted = Number.NaN;
- await driver.play(flick(mid, {velocity: {x: -1500, y: 0}, durationMs: 80}), {
- onEvent: async step => {
- if (step.type === 'touchEnd') {
- lifted = (await read()).x;
- }
- },
- });
+ const liftOffset = await recordLiftOffset(img);
+ await driver.play(flick(mid, {velocity: {x: -1500, y: 0}, durationMs: 80}));
const opacities = await img.evaluate(
async el =>
new Promise<number[]>(resolve => {
@@ -570,7 +565,7 @@ test('under Reduce Motion a zoomed flick coasts on and settles at the edge, as o
);
await waitForRest(img);
// It coasted on past where the finger lifted.
- expect((await read()).x).toBeLessThan(lifted - 20);
+ expect((await read()).x).toBeLessThan((await liftOffset()) - 20);
// No cross-fade.
expect(Math.min(...opacities)).toBe(1);
// It rests with no gap at the stage's edges.
diff --git a/packages/core/src/Lightbox/__tests__/liftOffset.ts b/packages/core/src/Lightbox/__tests__/liftOffset.ts
new file mode 100644
index 0000000000..c6d4b580eb
--- /dev/null
+++ b/packages/core/src/Lightbox/__tests__/liftOffset.ts
@@ -0,0 +1,44 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file liftOffset.ts
+ * @input A Lightbox photo's locator in an interaction spec
+ * @output Exports `recordLiftOffset`, which records the photo's horizontal
+ * offset as the next touch lifts, read in the page at the lift
+ * @position A helper for the Lightbox interaction specs. LightboxZoom and
+ * LightboxZoomModes use it to judge a flick's coast from the lift.
+ */
+
+import type {Locator} from '@astryxdesign/a11y-spec/playwright';
+
+/**
+ * Records the photo's horizontal offset as the next touch lifts, from a
+ * capture listener in the page, and returns a reader for it. A read from the
+ * runner after the lift comes a round trip late, and a flicked photo coasts
+ * on in that time. A read after the last move holds the lift back by that
+ * round trip instead. Past the 40 ms stillness interval, the page then reads
+ * a resting finger.
+ */
+export async function recordLiftOffset(
+ photo: Locator,
+): Promise<() => Promise<number>> {
+ await photo.evaluate(img => {
+ const record = window as unknown as {__liftOffset?: number};
+ delete record.__liftOffset;
+ document.addEventListener(
+ 'touchend',
+ () => {
+ const {translate} = getComputedStyle(img);
+ record.__liftOffset =
+ translate === 'none' ? 0 : parseFloat(translate.split(' ')[0]);
+ },
+ {capture: true, once: true},
+ );
+ });
+ return async () =>
+ photo.evaluate(
+ () =>
+ (window as unknown as {__liftOffset?: number}).__liftOffset ??
+ Number.NaN,
+ );
+}