From b697fe6456c0161f6566f4beece5c1a8176deb7a Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Fri, 30 Oct 2020 16:50:22 +1100 Subject: [PATCH] Combine args with basic object spread semantics Fixes #12697 --- lib/client-api/src/story_store.test.ts | 20 ++++++++++++++++++++ lib/client-api/src/story_store.ts | 5 ++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/client-api/src/story_store.test.ts b/lib/client-api/src/story_store.test.ts index 5000fcd66f43..1532feea38fa 100644 --- a/lib/client-api/src/story_store.test.ts +++ b/lib/client-api/src/story_store.test.ts @@ -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, { diff --git a/lib/client-api/src/story_store.ts b/lib/client-api/src/story_store.ts index 3387ea5958a2..a3501f8cbcbc 100644 --- a/lib/client-api/src/story_store.ts +++ b/lib/client-api/src/story_store.ts @@ -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 ).reduce((acc, [arg, { defaultValue }]) => {