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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSF: Expose story id in composeStories #22471

Merged
merged 6 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -88,6 +88,7 @@ export function composeStory<TRenderer extends Renderer = Renderer, TArgs extend
composedStory.args = story.initialArgs as Partial<TArgs>;
composedStory.play = story.playFunction as ComposedStoryPlayFn<TRenderer, Partial<TArgs>>;
composedStory.parameters = story.parameters as Parameters;
composedStory.id = story.id;

return composedStory;
}
Expand Down
4 changes: 2 additions & 2 deletions code/lib/types/src/modules/composedStory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */

import type { Renderer } from '@storybook/csf';
import type { Renderer, StoryId } from '@storybook/csf';

import type {
AnnotatedStoryFn,
Expand Down Expand Up @@ -33,7 +33,7 @@ export type ComposedStoryPlayFn<TRenderer extends Renderer = Renderer, TArgs = A
export type PreparedStoryFn<TRenderer extends Renderer = Renderer, TArgs = Args> = AnnotatedStoryFn<
TRenderer,
TArgs
> & { play: ComposedStoryPlayFn<TRenderer, TArgs>; args: TArgs };
> & { play: ComposedStoryPlayFn<TRenderer, TArgs>; args: TArgs } & { id: StoryId };
sjwilczynski marked this conversation as resolved.
Show resolved Hide resolved

export type ComposedStory<TRenderer extends Renderer = Renderer, TArgs = Args> =
| StoryFn<TRenderer, TArgs>
Expand Down
19 changes: 19 additions & 0 deletions code/renderers/react/src/__test__/internals.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ test('returns composed parameters from story', () => {
);
});

describe('Id of the story', () => {
test('is exposed correctly when composeStories is used', () => {
expect(CSF2StoryWithParamsAndDecorator.id).toBe(
'example-button--csf-2-story-with-params-and-decorator'
);
});
test('is exposed correctly when composeStory is used and exportsName is passed', () => {
const exportName = Object.entries(stories).filter(
([_, story]) => story === stories.CSF3Primary
)[0][0];
const Primary = composeStory(stories.CSF3Primary, stories.default, {}, exportName);
expect(Primary.id).toBe('example-button--csf-3-primary');
});
test("is not unique when composeStory is used and exportsName isn't passed", () => {
const Primary = composeStory(stories.CSF3Primary, stories.default);
expect(Primary.id).toContain('unknown');
});
});

// common in addons that need to communicate between manager and preview
test('should pass with decorators that need addons channel', () => {
const PrimaryWithChannels = composeStory(stories.CSF3Primary, stories.default, {
Expand Down