Skip to content

Commit

Permalink
only provide a play function wrapper if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Feb 9, 2024
1 parent 903317b commit 3783493
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('composeStory', () => {
};

const composedStory = composeStory(Story, meta);
await composedStory.play({ canvasElement: null });
await composedStory.play!({ canvasElement: null });
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
args: {
Expand All @@ -52,16 +52,6 @@ describe('composeStory', () => {
);
});

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
17 changes: 7 additions & 10 deletions code/lib/preview-api/src/modules/store/csf/portable-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,13 @@ export function composeStory<TRenderer extends Renderer = Renderer, TArgs extend
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>>,
play: story.playFunction
? ((async (extraContext: ComposedStoryPlayContext<TRenderer, TArgs>) =>
story.playFunction!({
...context,
...extraContext,
})) as unknown as ComposedStoryPlayFn<TRenderer, Partial<TArgs>>)
: undefined,
}
);

Expand Down
2 changes: 1 addition & 1 deletion code/lib/types/src/modules/composedStory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type ComposedStoryFn<
TRenderer extends Renderer = Renderer,
TArgs = Args,
> = PartialArgsStoryFn<TRenderer, TArgs> & {
play: ComposedStoryPlayFn<TRenderer, TArgs>;
play: ComposedStoryPlayFn<TRenderer, TArgs> | undefined;
args: TArgs;
id: StoryId;
storyName: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('CSF3', () => {

const { container } = render(<CSF3InputFieldFilled />);

await CSF3InputFieldFilled.play({ canvasElement: container });
await CSF3InputFieldFilled.play!({ canvasElement: container });

const input = screen.getByTestId('input') as HTMLInputElement;
expect(input.value).toEqual('Hello world!');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('CSF3', () => {

const { container } = render(CSF3InputFieldFilled());

await CSF3InputFieldFilled.play({ canvasElement: container as HTMLElement });
await CSF3InputFieldFilled.play!({ canvasElement: container as HTMLElement });

const input = screen.getByTestId('input') as HTMLInputElement;
expect(input.value).toEqual('Hello world!');
Expand Down

0 comments on commit 3783493

Please sign in to comment.