Skip to content

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

TypeScript

test(interaction-harness): openStory returns once Storybook has finished the story's render (#9772)

Cindy Zhanga24f7e01b3a1
diff --git a/internal/interaction-harness/src/story.ts b/internal/interaction-harness/src/story.ts
index a791579373..c64acfca0a 100644
--- a/internal/interaction-harness/src/story.ts
+++ b/internal/interaction-harness/src/story.ts
@@ -7,7 +7,7 @@
* @output `phone` (the touch-phone browser context every interaction proof
* runs in), `withStorybook` (serve the built Storybook for a spec file), and
* `openStory` (load one story, with Storybook's highlight module off, and
- * wait until it has rendered).
+ * wait until Storybook has finished rendering it).
* @position Stories are the reproduction path, as for every other browser
* check in this repository: a scenario used for verification is checked in,
* not built in a one-off page.
@@ -74,9 +74,17 @@ export interface OpenStoryOptions {
}
/**
- * Loads a story in the page and waits for its first render, its fonts and two
- * frames. Storybook's highlight module is off (see `OpenStoryOptions`), and
- * that is checked: its root element must be absent once the story renders.
+ * Loads a story in the page and waits for its first render, for Storybook to
+ * finish that render, for its fonts and for two frames. Storybook's highlight
+ * module is off (see `OpenStoryOptions`), and that is checked: its root
+ * element must be absent once the story renders.
+ *
+ * Storybook finishes a render after the story first paints: it waits out the
+ * story's animations (phase `completing`), runs `afterEach`, and only then
+ * reports the story finished (`STORY_FINISHED`, phase `finished`), about
+ * 150 ms later. Its decorators' effects (a `pointermove` listener on the
+ * document, for one) land in between, so a proof measuring from first paint
+ * would measure Storybook as well.
*/
export async function openStory(
page: Page,
@@ -115,6 +123,16 @@ export async function openStory(
.locator('#storybook-root > *')
.first()
.waitFor({state: 'attached'});
+ await page.waitForFunction(id => {
+ const render = (
+ globalThis as unknown as {
+ __STORYBOOK_PREVIEW__?: {
+ currentRender?: {readonly id?: string; readonly phase?: string};
+ };
+ }
+ ).__STORYBOOK_PREVIEW__?.currentRender;
+ return render?.id === id && render.phase === 'finished';
+ }, storyId);
await page.evaluate(async () => {
await document.fonts.ready;
await new Promise<void>(resolve =>
diff --git a/internal/interaction-harness/src/storybook.interaction.browser.spec.ts b/internal/interaction-harness/src/storybook.interaction.browser.spec.ts
index a711feb62c..ff307c5b7e 100644
--- a/internal/interaction-harness/src/storybook.interaction.browser.spec.ts
+++ b/internal/interaction-harness/src/storybook.interaction.browser.spec.ts
@@ -6,7 +6,9 @@
* @output `openStory` turns Storybook's highlight module off, so an insertion
* in a story carries no `getComputedStyle` scan of the page; with the module
* on (the control), every insertion carries one. Both engines; Chromium's
- * trace also counts the style recalculations the scan forces
+ * trace also counts the style recalculations the scan forces. `openStory`
+ * returns once Storybook has finished the story's render, so no listener
+ * Storybook adds to the document or the window lands after it
* @position Self-test for story.ts; it needs the built Storybook, as every
* component proof does
*/
@@ -57,6 +59,69 @@ async function insertCopy(page: Page) {
});
}
+/**
+ * Records every listener added to the document or the window, and when, from
+ * before Storybook loads.
+ */
+function recordListeners(): void {
+ const w = window as unknown as {
+ __listeners: {type: string; target: string; at: number}[];
+ };
+ w.__listeners = [];
+ const add = EventTarget.prototype.addEventListener;
+ EventTarget.prototype.addEventListener = function (
+ this: EventTarget,
+ ...args: Parameters<EventTarget['addEventListener']>
+ ) {
+ if (this === document || this === window) {
+ w.__listeners.push({
+ type: args[0],
+ target: this === document ? 'document' : 'window',
+ at: performance.now(),
+ });
+ }
+ return add.apply(this, args);
+ };
+}
+
+test("openStory returns once Storybook has finished the story's render: no listener Storybook adds lands after it", async ({
+ page,
+}) => {
+ await page.addInitScript(recordListeners);
+ await openStory(page, storybook(), 'core-button--primary');
+ const returned = await page.evaluate(() => ({
+ at: performance.now(),
+ phase: (
+ window as unknown as {
+ __STORYBOOK_PREVIEW__?: {currentRender?: {phase?: string}};
+ }
+ ).__STORYBOOK_PREVIEW__?.currentRender?.phase,
+ }));
+ // Two frames and 300 ms more, longer than Storybook's post-render tail
+ // (about 150 ms): a listener a render still in flight adds lands by then;
+ // a finished render adds none.
+ await page.evaluate(
+ () =>
+ new Promise<void>(resolve =>
+ requestAnimationFrame(() =>
+ requestAnimationFrame(() => setTimeout(resolve, 300)),
+ ),
+ ),
+ );
+ const late = await page.evaluate(
+ at =>
+ (
+ window as unknown as {
+ __listeners: {type: string; target: string; at: number}[];
+ }
+ ).__listeners
+ .filter(listener => listener.at > at)
+ .map(({type, target}) => `${target} ${type}`),
+ returned.at,
+ );
+ expect({phase: returned.phase, late}).toEqual({phase: 'finished', late: []});
+});
+
test("openStory turns Storybook's highlight module off: an insertion in a story carries no getComputedStyle scan of the page", async ({
page,
browserName,