Skip to content

Commit

Permalink
Merge pull request #18433 from storybookjs/tom/sb-412-include-legacy-…
Browse files Browse the repository at this point in the history
…fields-in-storiesjson-and

UI: Update manager to respect `parameters.docsOnly` in `stories.json`
  • Loading branch information
shilman committed Jun 8, 2022
2 parents 7a48cd5 + c8f611c commit 08351b7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lib/api/src/lib/stories.ts
Expand Up @@ -122,6 +122,9 @@ export interface StoryIndexStory {
name: StoryName;
title: ComponentTitle;
importPath: Path;

// v2 or v2-compatible story index includes this
parameters?: Parameters;
}
export interface StoryIndex {
v: number;
Expand Down Expand Up @@ -183,16 +186,19 @@ export const transformStoryIndexToStoriesHash = (
{ provider }: { provider: Provider }
): StoriesHash => {
const countByTitle = countBy(Object.values(index.stories), 'title');
const input = Object.entries(index.stories).reduce((acc, [id, { title, name, importPath }]) => {
const docsOnly = name === 'Page' && countByTitle[title] === 1;
acc[id] = {
id,
kind: title,
name,
parameters: { fileName: importPath, options: {}, docsOnly },
};
return acc;
}, {} as StoriesRaw);
const input = Object.entries(index.stories).reduce(
(acc, [id, { title, name, importPath, parameters }]) => {
const docsOnly = name === 'Page' && countByTitle[title] === 1;
acc[id] = {
id,
kind: title,
name,
parameters: { fileName: importPath, options: {}, docsOnly, ...parameters },
};
return acc;
},
{} as StoriesRaw
);

return transformStoriesRawToStoriesHash(input, { provider, prepared: false });
};
Expand Down
31 changes: 31 additions & 0 deletions lib/api/src/tests/stories.test.js
Expand Up @@ -1116,6 +1116,37 @@ describe('stories API', () => {
expect(storedStoriesHash['component-b--page'].parameters.docsOnly).toBe(true);
expect(storedStoriesHash['component-c--story-4'].parameters.docsOnly).toBe(false);
});

it('prefers parameters.docsOnly to inferred docsOnly status', async () => {
mockStories.mockReset().mockReturnValue({
'component-a--docs': {
type: 'story',
title: 'Component A',
name: 'Docs', // Called 'Docs' rather than 'Page'
importPath: './path/to/component-a.ts',
parameters: {
docsOnly: true,
},
},
});

const navigate = jest.fn();
const store = createMockStore();
const fullAPI = Object.assign(new EventEmitter(), {
setStories: jest.fn(),
});

const { api, init } = initStories({ store, navigate, provider, fullAPI });
Object.assign(fullAPI, api);

await init();

const { storiesHash: storedStoriesHash } = store.getState();

// We need exact key ordering, even if in theory JS doesn't guarantee it
expect(Object.keys(storedStoriesHash)).toEqual(['component-a', 'component-a--docs']);
expect(storedStoriesHash['component-a--docs'].parameters.docsOnly).toBe(true);
});
});

describe('STORY_PREPARED', () => {
Expand Down

0 comments on commit 08351b7

Please sign in to comment.