From 35d0096a772c29bdafac80065232f7728d483b1e Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Fri, 26 Nov 2021 13:52:21 +1100 Subject: [PATCH] Ensure that `context.args` is always set. --- lib/store/src/prepareStory.test.ts | 43 ++++++++++++++++++++++++++++++ lib/store/src/prepareStory.ts | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/store/src/prepareStory.test.ts b/lib/store/src/prepareStory.test.ts index 068062245152..4dd1c7b379c9 100644 --- a/lib/store/src/prepareStory.test.ts +++ b/lib/store/src/prepareStory.test.ts @@ -527,6 +527,49 @@ describe('prepareStory', () => { expect.objectContaining({ argsByTarget: { [NO_TARGET_NAME]: { a: 1 }, foo: { b: 2 } } }) ); }); + + it('always sets args, even when all are targetted', () => { + const renderMock = jest.fn(); + const firstStory = prepareStory( + { + id, + name, + args: { b: 2 }, + argTypes: { b: { name: 'b', target: 'foo' } }, + }, + { id, title }, + { render: renderMock } + ); + + firstStory.unboundStoryFn({ + args: firstStory.initialArgs, + hooks: new HooksContext(), + ...firstStory, + } as any); + expect(renderMock).toHaveBeenCalledWith( + {}, + expect.objectContaining({ argsByTarget: { foo: { b: 2 } } }) + ); + }); + + it('always sets args, even when none are set for the story', () => { + const renderMock = jest.fn(); + const firstStory = prepareStory( + { + id, + name, + }, + { id, title }, + { render: renderMock } + ); + + firstStory.unboundStoryFn({ + args: firstStory.initialArgs, + hooks: new HooksContext(), + ...firstStory, + } as any); + expect(renderMock).toHaveBeenCalledWith({}, expect.objectContaining({ argsByTarget: {} })); + }); }); }); diff --git a/lib/store/src/prepareStory.ts b/lib/store/src/prepareStory.ts index bbe9186c21fc..6547cf959810 100644 --- a/lib/store/src/prepareStory.ts +++ b/lib/store/src/prepareStory.ts @@ -187,7 +187,7 @@ export function prepareStory( ...context, allArgs: context.args, argsByTarget, - args: argsByTarget[NO_TARGET_NAME], + args: argsByTarget[NO_TARGET_NAME] || {}, }; }