CSF: Clean up loadCsf per review feedback

This commit is contained in:
Michael Shilman 2020-08-04 11:31:20 +08:00
parent 4f5e07f344
commit 5ef6771a15
2 changed files with 21 additions and 29 deletions

View File

@ -82,13 +82,14 @@ describe('core.preview.loadCsf', () => {
const mockedStoriesOf = clientApi.storiesOf as jest.Mock;
expect(mockedStoriesOf).toHaveBeenCalledWith('a', true);
const aApi = mockedStoriesOf.mock.results[0].value;
expect(aApi.add).toHaveBeenCalledWith('1', input.a[1], { __id: 'a--1' });
expect(aApi.add).toHaveBeenCalledWith('2', input.a[2], { __id: 'a--2' });
const extras: any = { decorators: [], args: {}, argTypes: {} };
expect(aApi.add).toHaveBeenCalledWith('1', input.a[1], { __id: 'a--1', ...extras });
expect(aApi.add).toHaveBeenCalledWith('2', input.a[2], { __id: 'a--2', ...extras });
expect(mockedStoriesOf).toHaveBeenCalledWith('b', true);
const bApi = mockedStoriesOf.mock.results[1].value;
expect(bApi.add).toHaveBeenCalledWith('1', input.b[1], { __id: 'b--1' });
expect(bApi.add).toHaveBeenCalledWith('two', input.b[2], { __id: 'b--2' });
expect(bApi.add).toHaveBeenCalledWith('1', input.b[1], { __id: 'b--1', ...extras });
expect(bApi.add).toHaveBeenCalledWith('two', input.b[2], { __id: 'b--2', ...extras });
});
it('adds stories in the right order if __namedExportsOrder is supplied', () => {
@ -175,7 +176,12 @@ describe('core.preview.loadCsf', () => {
const mockedStoriesOf = clientApi.storiesOf as jest.Mock;
const aApi = mockedStoriesOf.mock.results[0].value;
expect(aApi.add).toHaveBeenCalledWith('X', input.a.x, { __id: 'random--x' });
expect(aApi.add).toHaveBeenCalledWith('X', input.a.x, {
__id: 'random--x',
decorators: [],
args: {},
argTypes: {},
});
});
it('sets various parameters on components', () => {

View File

@ -161,41 +161,27 @@ const loadStories = (
if (isExportStory(key, meta)) {
const storyFn = exports[key];
const { story } = storyFn;
const { storyName = story?.name, parameters, decorators, args, argTypes } = storyFn;
const decoratorParams = decorators ? { decorators } : {};
const argsParams = { args, argTypes };
const { storyName = story?.name } = storyFn;
// storyFn.x and storyFn.story.x get merged with
// storyFn.x taking precedence in the merge
let deprecatedStoryParams = null;
const parameters = { ...story?.parameters, ...storyFn.parameters };
const decorators = [...(storyFn.decorators || []), ...(story?.decorators || [])];
const args = { ...story?.args, ...storyFn.args };
const argTypes = { ...story?.argTypes, ...storyFn.argTypes };
if (story) {
logger.debug('deprecated story', story);
deprecatedStoryAnnotationWarning();
deprecatedStoryParams = story.parameters;
if (story.decorators) {
decoratorParams.decorators = decoratorParams.decorators
? [...decoratorParams.decorators, ...story.decorators]
: story.decorators;
}
if (story.args) {
argsParams.args = { ...story.args, ...argsParams.args };
}
if (story.argTypes) {
argsParams.argTypes = { ...story.argTypes, ...argsParams.argTypes };
}
}
const exportName = storyNameFromExport(key);
const idParams = { __id: toId(componentId || kindName, exportName) };
const storyParams = {
...deprecatedStoryParams,
__id: toId(componentId || kindName, exportName),
...parameters,
...decoratorParams,
...idParams,
...argsParams,
decorators,
args,
argTypes,
};
kind.add(storyName || exportName, storyFn, storyParams);
}