Skip to content

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

TypeScript

test(CodeBlock): the copy-clearance contract measures only after the story re-renders with its args (#9119)

Cindy Zhang71199c40ff89
diff --git a/packages/core/src/CodeBlock/__tests__/CodeBlockCopyClearance.a11y.chromium.spec.ts b/packages/core/src/CodeBlock/__tests__/CodeBlockCopyClearance.a11y.chromium.spec.ts
index 0e8c10e0e5..a789c2ad92 100644
--- a/packages/core/src/CodeBlock/__tests__/CodeBlockCopyClearance.a11y.chromium.spec.ts
+++ b/packages/core/src/CodeBlock/__tests__/CodeBlockCopyClearance.a11y.chromium.spec.ts
@@ -39,9 +39,24 @@ async function mount(
);
await expect(copyButton(page)).toBeVisible();
if (Object.keys(args).length > 0) {
+ // Measure only the story as these args render it. Storybook emits
+ // `storyRendered` for the story's own first render too, shortly after it
+ // is visible, so the update waits for that render to finish first; the
+ // `storyRendered` it then waits for is the re-render with the args, and a
+ // re-render that never comes fails here instead of being measured. Each
+ // arg the cases pass is also checked in the page, so a slow runner can
+ // never measure the default story in its place.
+ await page.waitForFunction(
+ () =>
+ (
+ window as unknown as {
+ __STORYBOOK_PREVIEW__?: {currentRender?: {phase?: string}};
+ }
+ ).__STORYBOOK_PREVIEW__?.currentRender?.phase === 'finished',
+ );
await page.evaluate(
async ({storyId, updatedArgs}) =>
- new Promise<void>(resolve => {
+ new Promise<void>((resolve, reject) => {
const channel = (
window as unknown as {
__STORYBOOK_ADDONS_CHANNEL__: {
@@ -50,12 +65,32 @@ async function mount(
};
}
).__STORYBOOK_ADDONS_CHANNEL__;
- channel.once('storyRendered', () => resolve());
+ const timer = setTimeout(
+ () =>
+ reject(new Error('the story did not re-render with its args')),
+ 20_000,
+ );
+ channel.once('storyRendered', () => {
+ clearTimeout(timer);
+ resolve();
+ });
channel.emit('updateStoryArgs', {storyId, updatedArgs});
- setTimeout(resolve, 1500);
}),
{storyId: STORY, updatedArgs: args},
);
+ if (typeof args.code === 'string') {
+ await expect(
+ page.locator('pre.astryx-code-block [data-line]'),
+ ).toHaveCount(args.code.split('\n').length);
+ }
+ if (typeof args.title === 'string') {
+ await expect(page.getByText(args.title, {exact: true})).toBeVisible();
+ }
+ if (args.isWrapped === true) {
+ await expect(
+ page.locator('pre.astryx-code-block [data-line="1"]'),
+ ).toHaveCSS('white-space', 'pre-wrap');
+ }
}
}