Skip to content

Commit

Permalink
pass story context to the play function of a composed story
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Feb 8, 2024
1 parent d77745e commit 8e2a375
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
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

0 comments on commit 8e2a375

Please sign in to comment.