Skip to content

Commit

Permalink
Update manager to look at parameters.docsOnly in stories.json.
Browse files Browse the repository at this point in the history
We sometimes set this in 6.x (if using `storyStoreV7 = false` and `breakingChangesV7 = false`), but we plan to use this for back-compat purposes in 7.x.
  • Loading branch information
tmeasday committed Jun 8, 2022
1 parent 7a48cd5 commit c8f611c
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 c8f611c

Please sign in to comment.