Skip to content

Commit

Permalink
fix: ensure story has a name before calling composeStory
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Jan 24, 2022
1 parent a56ad97 commit ef7731b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,6 @@ exports[`Renders CSF2Secondary story 1`] = `
</body>
`;

exports[`Renders CSF2StoryWithLocale story 1`] = `
<body>
<div>
<div>
Locale:
en
</div>
<button
class="storybook-button storybook-button--medium storybook-button--secondary"
type="button"
>
Hello!
</button>
</div>
</body>
`;

exports[`Renders CSF2StoryWithParamsAndDecorator story 1`] = `
<body>
<div>
Expand Down Expand Up @@ -125,3 +108,20 @@ exports[`Renders CSF3Primary story 1`] = `
</div>
</body>
`;

exports[`Renders WithLocale story 1`] = `
<body>
<div>
<div>
Locale:
en
</div>
<button
class="storybook-button storybook-button--medium storybook-button--secondary"
type="button"
>
Hello!
</button>
</div>
</body>
`;
26 changes: 19 additions & 7 deletions lib/store/src/csf/testing-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { normalizeStory } from '../normalizeStory';
import { HooksContext } from '../../hooks';
import { normalizeComponentAnnotations } from '../normalizeComponentAnnotations';
import { getValuesFromArgTypes, normalizeProjectAnnotations } from '..';
import type { CSFExports, TestingStoryPlayFn } from './types';
import type { CSFExports, TestingStoryPlayFn, TestingStory } from './types';

export * from './types';

Expand Down Expand Up @@ -80,23 +80,35 @@ export function composeStory<
return composedStory;
}

const getStoryName = (story: TestingStory) => {
if (story.storyName) {
return story.storyName;
}

if (typeof story !== 'function' && story.name) {
return story.name;
}

return undefined;
};

export function composeStories<TModule extends CSFExports>(
storiesImport: TModule,
globalConfig: ProjectAnnotations<AnyFramework>,
composeStoryFn: typeof composeStory
) {
const { default: meta, __esModule, __namedExportsOrder, ...stories } = storiesImport;
const composedStories = Object.entries(stories).reduce((storiesMap, [key, _story]) => {
if (!isExportStory(key as string, meta)) {
const composedStories = Object.entries(stories).reduce((storiesMap, [exportsName, _story]) => {
if (!isExportStory(exportsName as string, meta)) {
return storiesMap;
}

const storyName = String(key);
const story = _story as any;
story.storyName = storyName;
const story = _story as TestingStory;
// Ensure story name is already set, else use exportsName as fallback. This is important for scenarios like snapshot testing
story.storyName = getStoryName(story) || String(exportsName);

const result = Object.assign(storiesMap, {
[key]: composeStoryFn(story, meta, globalConfig),
[exportsName]: composeStoryFn(story, meta, globalConfig),
});
return result;
}, {});
Expand Down

0 comments on commit ef7731b

Please sign in to comment.