Skip to content

Commit 7e82663

Browse files
authored
test(DropdownMenu): the lazy sheet case waits for its rows by frames (#10643)
test(DropdownMenu): the lazy sheet case waits for its rows by frames The keyboard-open case waited for the sheet's rows with findByRole, whose one-second budget is wall-clock time. A worker that stalls while the rows load comes back with that timeout due beside React's reveal timer, and Node runs the timeout before React's render. So the case failed with no product fault. It now reads the row once a frame for up to 120 frames, with React outside act as in a findBy query, and fails by name if the rows never render.
1 parent 4082ba7 commit 7e82663

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

‎packages/core/src/DropdownMenu/DropdownMenuLazySheet.test.tsx‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
import {describe, it, expect, vi, beforeEach} from 'vitest';
17-
import {render, screen, act} from '@testing-library/react';
17+
import {render, screen, act, getConfig} from '@testing-library/react';
1818
import userEvent from '@testing-library/user-event';
1919

2020
const sheetLoad = vi.hoisted(() => {
@@ -84,6 +84,29 @@ if ('eager' in sheetCode) {
8484
);
8585
}
8686

87+
/**
88+
* The row named `name`, once it renders, read once a frame for up to
89+
* `frames` frames. A worker that stalls loses frames, not a wall-clock
90+
* budget. React runs outside `act` meanwhile, as in a `findBy` query.
91+
*/
92+
async function rowWithinFrames(
93+
name: string,
94+
frames: number,
95+
): Promise<HTMLElement | null> {
96+
return getConfig().asyncWrapper(async () => {
97+
for (let frame = 0; frame < frames; frame++) {
98+
const row = screen.queryByRole('button', {name});
99+
if (row != null) {
100+
return row;
101+
}
102+
await new Promise<void>(resolve => {
103+
requestAnimationFrame(() => resolve());
104+
});
105+
}
106+
return screen.queryByRole('button', {name});
107+
});
108+
}
109+
87110
beforeEach(() => {
88111
HTMLDialogElement.prototype.showModal = vi.fn(function (
89112
this: HTMLDialogElement,
@@ -156,7 +179,11 @@ describe('DropdownMenu touch sheet loaded on demand', () => {
156179
});
157180
// React renders the rows when the dropdown's own loads settle. On a busy
158181
// machine that can be after this act, so wait for the rows themselves.
159-
const action = await screen.findByRole('button', {name: 'Edit project'});
182+
const action = await rowWithinFrames('Edit project', 120);
183+
expect(
184+
action,
185+
'the rows render within 120 frames of the load',
186+
).not.toBeNull();
160187

161188
// The dropdown focuses the first row a frame after the rows appear. The
162189
// frame this awaits runs after that one.

0 commit comments

Comments
 (0)