Skip to content

Commit a455f18

Browse files
yannbfstorybook-bot
authored andcommitted
Merge pull request #31335 from adamscybot/fix-pass-functions-playwright-portable-stories
Portable stories: Fix playwright CT to allow functions to be passed as props (cherry picked from commit d1eab95)
1 parent be6d1ba commit a455f18

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

code/core/src/preview-api/modules/store/csf/portable-stories.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ export function composeStories<TModule extends Store_CSFExports>(
301301
return composedStories;
302302
}
303303

304-
type WrappedStoryRef = { __pw_type: 'jsx' | 'importRef' };
304+
type WrappedStoryRef =
305+
| { __pw_type: 'jsx'; props: Record<string, any> }
306+
| { __pw_type: 'importRef' };
305307
type UnwrappedJSXStoryRef = {
306308
__pw_type: 'jsx';
307309
type: UnwrappedImportStoryRef;
@@ -341,12 +343,16 @@ export function createPlaywrightTest<TFixture extends { extend: any }>(
341343
`);
342344
}
343345

346+
// Props are not necessarily serialisable and so can't be passed to browser via
347+
// `page.evaluate`. Regardless they are not needed for storybook load/play steps.
348+
const { props, ...storyRefWithoutProps } = storyRef;
349+
344350
await page.evaluate(async (wrappedStoryRef: WrappedStoryRef) => {
345351
const unwrappedStoryRef = await globalThis.__pwUnwrapObject?.(wrappedStoryRef);
346352
const story =
347353
'__pw_type' in unwrappedStoryRef ? unwrappedStoryRef.type : unwrappedStoryRef;
348354
return story?.load?.();
349-
}, storyRef);
355+
}, storyRefWithoutProps);
350356

351357
// mount the story
352358
const mountResult = await mount(storyRef, ...restArgs);
@@ -358,7 +364,7 @@ export function createPlaywrightTest<TFixture extends { extend: any }>(
358364
'__pw_type' in unwrappedStoryRef ? unwrappedStoryRef.type : unwrappedStoryRef;
359365
const canvasElement = document.querySelector('#root');
360366
return story?.play?.({ canvasElement });
361-
}, storyRef);
367+
}, storyRefWithoutProps);
362368

363369
return mountResult;
364370
});

test-storybooks/portable-stories-kitchen-sink/react/stories/Button.playwright.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ test('renders with composeStory (singular)', async ({ mount }) => {
1818
});
1919

2020
test('renders story with props', async ({ mount }) => {
21+
let called = false
2122
const component = await mount(
22-
<stories.CSF3Button primary={true}>child from test</stories.CSF3Button>
23+
<stories.CSF3Button primary={true} onClick={() => { called = true }}>child from test</stories.CSF3Button>
2324
);
2425
await expect(component).toContainText('child from test');
2526
await expect(component.getByRole('button')).toHaveClass(/storybook-button--primary/);
27+
await component.getByRole('button').click()
28+
await expect(called).toBe(true)
2629
});
2730

2831
test('renders story with custom render', async ({ mount }) => {

0 commit comments

Comments
 (0)