Skip to content

Commit 7d37fb5

Browse files
authored
test(BottomSheet): the drag test waits for the sheet's own motion, not for fixed times (#9862)
test(BottomSheet): the drag test waits for the sheet's own motion, not for fixed times BottomSheetDrag's "a drag writes the transform per move" waited a fixed 600 ms for the sheet's entrance and read the settle 40 ms after the release. On a loaded page either race can be lost: the drag starts while the sheet is still entering, or the settle is nearly over before the read. It now waits for the entrance's own end (the panel's transform transition ending, the signal the panel waits on), and reads where the settle starts from the settle transition's first keyframe, recorded when it runs. No timeouts change, and no product code changes.
1 parent 72ac1b7 commit 7d37fb5

1 file changed

Lines changed: 67 additions & 9 deletions

File tree

‎packages/core/src/BottomSheet/__tests__/BottomSheetDrag.a11y.chromium.spec.ts‎

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,39 @@ async function openSheet(page: Page) {
4343
await page.goto(
4444
`${storybook.origin}/iframe.html?id=core-bottomsheet--snap-points&viewMode=story`,
4545
);
46+
// The entrance ends when the panel's transform transition ends, which is
47+
// the signal the panel itself waits on. Listen before the click, so that
48+
// no entrance ends unseen.
49+
await page.evaluate(() => {
50+
const entrance = window as unknown as {sheetEntered: boolean};
51+
entrance.sheetEntered = false;
52+
document.addEventListener(
53+
'transitionend',
54+
event => {
55+
if (
56+
event.propertyName === 'transform' &&
57+
event.target instanceof Element &&
58+
event.target.classList.contains('astryx-bottom-sheet')
59+
) {
60+
entrance.sheetEntered = true;
61+
}
62+
},
63+
{capture: true},
64+
);
65+
});
4666
await page.getByRole('button', {name: 'Open nearby places'}).click();
4767
const dialog = page.getByRole('dialog', {name: 'Nearby places'});
4868
await expect(dialog).toBeVisible();
4969
// The sheet panel carries the component class and the transform.
5070
const sheet = dialog.locator('.astryx-bottom-sheet').first();
5171
await expect(sheet).toBeVisible();
52-
await page.waitForTimeout(600); // the entrance
72+
await expect
73+
.poll(async () =>
74+
page.evaluate(
75+
() => (window as unknown as {sheetEntered: boolean}).sheetEntered,
76+
),
77+
)
78+
.toBe(true);
5379
return {dialog, sheet};
5480
}
5581

@@ -108,18 +134,50 @@ test('a drag writes the transform per move and renders nothing between', async (
108134
// ... and the moves cost a handful of commits (the drag beginning, the
109135
// sheet traveling), not one per move.
110136
expect(commitsDuring - commitsBefore).toBeLessThanOrEqual(4);
137+
// The settle's transition starts from where it is as it begins, whatever
138+
// the clock reads when the page gets to it. So record its first keyframe.
139+
await sheet.evaluate(element => {
140+
const el = element as HTMLElement;
141+
const record = window as unknown as {settleFrom: string | null};
142+
record.settleFrom = null;
143+
el.addEventListener('transitionrun', event => {
144+
if (event.target !== el || event.propertyName !== 'transform') {
145+
return;
146+
}
147+
for (const animation of el.getAnimations()) {
148+
if (
149+
animation instanceof CSSTransition &&
150+
animation.transitionProperty === 'transform' &&
151+
animation.effect instanceof KeyframeEffect
152+
) {
153+
record.settleFrom = String(
154+
animation.effect.getKeyframes()[0]?.transform ?? '',
155+
);
156+
}
157+
}
158+
});
159+
});
111160
await cdp.send('Input.dispatchTouchEvent', {
112161
type: 'touchEnd',
113162
touchPoints: [],
114163
});
115-
// The release animates back from where the finger left the sheet: the
116-
// computed transform is still off rest a frame later, and at rest after the
117-
// transition.
118-
await page.waitForTimeout(40);
119-
const midway = await sheet.evaluate(el => getComputedStyle(el).transform);
120-
const midwayTy = /matrix\(1, 0, 0, 1, 0, (-?[\d.]+)\)/.exec(midway);
121-
expect(midwayTy, midway).not.toBeNull();
122-
expect(Number(midwayTy?.[1])).toBeGreaterThan(5);
164+
// The release animates back from where the finger left the sheet, and the
165+
// sheet is at rest after the transition.
166+
await expect
167+
.poll(async () =>
168+
page.evaluate(
169+
() => (window as unknown as {settleFrom: string | null}).settleFrom,
170+
),
171+
)
172+
.not.toBeNull();
173+
const settleFrom = await page.evaluate(
174+
() => (window as unknown as {settleFrom: string}).settleFrom,
175+
);
176+
const fromTy =
177+
/matrix\(1, 0, 0, 1, 0, (-?[\d.]+)\)/.exec(settleFrom)?.[1] ??
178+
/translateY\((-?[\d.]+)px\)/.exec(settleFrom)?.[1];
179+
expect(fromTy, settleFrom).toBeDefined();
180+
expect(Number(fromTy)).toBeGreaterThan(5);
123181
await expect
124182
.poll(async () => sheet.evaluate(el => getComputedStyle(el).transform), {
125183
timeout: 2_000,

0 commit comments

Comments
 (0)