Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Portable stories: Pass story context to the play function of a composed story #25943

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,38 @@ describe('composeStory', () => {
);
});

it('should compose with a play function', async () => {
const spy = vi.fn();
const Story = () => {};
Story.args = {
primary: true,
};
Story.play = async (context: any) => {
spy(context);
};

const composedStory = composeStory(Story, meta);
await composedStory.play({ canvasElement: null });
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
args: {
...Story.args,
...meta.args,
},
})
);
});

it('should throw when executing the play function but the story does not have one', async () => {
const Story = () => {};
Story.args = {
primary: true,
};

const composedStory = composeStory(Story, meta);
expect(composedStory.play({ canvasElement: null })).rejects.toThrow();
});

it('should throw an error if Story is undefined', () => {
expect(() => {
// @ts-expect-error (invalid input)
Expand Down
33 changes: 26 additions & 7 deletions code/lib/preview-api/src/modules/store/csf/portable-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
Parameters,
ComposedStoryFn,
StrictArgTypes,
ComposedStoryPlayContext,
} from '@storybook/types';

import { HooksContext } from '../../../addons';
Expand Down Expand Up @@ -74,24 +75,42 @@ export function composeStory<TRenderer extends Renderer = Renderer, TArgs extend

const defaultGlobals = getValuesFromArgTypes(projectAnnotations.globalTypes);

const context: StoryContext<TRenderer> = {
hooks: new HooksContext(),
globals: defaultGlobals,
args: { ...story.initialArgs },
viewMode: 'story',
loaded: {},
abortSignal: null as unknown as AbortSignal,
canvasElement: null,
...story,
};

const composedStory: ComposedStoryFn<TRenderer, Partial<TArgs>> = Object.assign(
(extraArgs?: Partial<TArgs>) => {
const context: Partial<StoryContext> = {
...story,
hooks: new HooksContext(),
globals: defaultGlobals,
args: { ...story.initialArgs, ...extraArgs },
const finalContext: StoryContext<TRenderer> = {
...context,
args: { ...context.initialArgs, ...extraArgs },
};

return story.unboundStoryFn(prepareContext(context as StoryContext<TRenderer>));
return story.unboundStoryFn(prepareContext(finalContext));
},
{
storyName,
args: story.initialArgs as Partial<TArgs>,
play: story.playFunction as ComposedStoryPlayFn<TRenderer, Partial<TArgs>>,
parameters: story.parameters as Parameters,
argTypes: story.argTypes as StrictArgTypes<TArgs>,
id: story.id,
play: (async (extraContext: ComposedStoryPlayContext<TRenderer, TArgs>) => {
if (story.playFunction === undefined) {
throw new Error('The story does not have a play function. Make sure to add one.');
}

await story.playFunction({
...context,
...extraContext,
});
}) as unknown as ComposedStoryPlayFn<TRenderer, Partial<TArgs>>,
}
);

Expand Down
Loading