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

UI: Update manager to respect parameters.docsOnly in stories.json #18433

Merged
merged 1 commit into from Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tmeasday could ...parameters ever override options or fileName? if so, would that be an issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think fileName will always be the same. I think we just define options here because other code assumes it exists, I don't really remember why.

};
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