Skip to content

Commit

Permalink
Merge pull request #12958 from storybookjs/12697-fix-arg-combination
Browse files Browse the repository at this point in the history
Combine args with basic object spread semantics
  • Loading branch information
shilman authored Oct 30, 2020
2 parents 647a075 + b697fe6 commit f286ca2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
20 changes: 20 additions & 0 deletions lib/client-api/src/story_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ describe('preview.story_store', () => {
});

describe('args', () => {
it('composes component-level and story-level args, favoring story-level', () => {
const store = new StoryStore({ channel });
store.addKindMetadata('a', {
parameters: { args: { arg1: 1, arg2: 2, arg3: 3, arg4: { complex: 'object' } } },
});
addStoryToStore(store, 'a', '1', () => 0, {
args: {
arg1: 4,
arg2: undefined,
arg4: { other: 'object ' },
},
});
expect(store.getRawStory('a', '1').args).toEqual({
arg1: 4,
arg2: undefined,
arg3: 3,
arg4: { other: 'object ' },
});
});

it('is initialized to the value stored in parameters.args[name] || parameters.argType[name].defaultValue', () => {
const store = new StoryStore({ channel });
addStoryToStore(store, 'a', '1', () => 0, {
Expand Down
5 changes: 4 additions & 1 deletion lib/client-api/src/story_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,10 @@ export default class StoryStore {
};

// Pull out parameters.args.$ || .argTypes.$.defaultValue into initialArgs
const passedArgs: Args = combinedParameters.args;
const passedArgs: Args = {
...this._kinds[kind].parameters.args,
...storyParameters.args,
};
const defaultArgs: Args = Object.entries(
argTypes as Record<string, { defaultValue: any }>
).reduce((acc, [arg, { defaultValue }]) => {
Expand Down

0 comments on commit f286ca2

Please sign in to comment.