From 7e89d72eb4c8e096a91d980d1f4d221ad3f51844 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Fri, 26 Nov 2021 14:08:33 +1100 Subject: [PATCH] Pass args/argTypes and component through parameters --- lib/client-api/src/ClientApi.ts | 10 ++++-- lib/core-client/src/preview/start.test.ts | 37 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/client-api/src/ClientApi.ts b/lib/client-api/src/ClientApi.ts index 895e3a334312..964a1c7e68ef 100644 --- a/lib/client-api/src/ClientApi.ts +++ b/lib/client-api/src/ClientApi.ts @@ -316,7 +316,7 @@ export class ClientApi { ); } - const { decorators, loaders, ...storyParameters } = parameters; + const { decorators, loaders, component, args, argTypes, ...storyParameters } = parameters; // eslint-disable-next-line no-underscore-dangle const storyId = parameters.__id || toId(kind, storyName); @@ -328,6 +328,9 @@ export class ClientApi { parameters: { fileName, __id: storyId, ...storyParameters }, decorators, loaders, + args, + argTypes, + component, render: storyFn, }; counter += 1; @@ -358,12 +361,15 @@ Read more here: https://github.com/storybookjs/storybook/blob/master/MIGRATION.m return api; }; - api.addParameters = (parameters: Parameters) => { + api.addParameters = ({ component, args, argTypes, ...parameters }: Parameters) => { if (hasAdded) throw new Error(`You cannot add parameters after the first story for a kind. Read more here: https://github.com/storybookjs/storybook/blob/master/MIGRATION.md#can-no-longer-add-decoratorsparameters-after-stories`); meta.parameters = combineParameters(meta.parameters, parameters); + if (component) meta.component = component; + if (args) meta.args = { ...meta.args, ...args }; + if (argTypes) meta.argTypes = { ...meta.argTypes, ...argTypes }; return api; }; diff --git a/lib/core-client/src/preview/start.test.ts b/lib/core-client/src/preview/start.test.ts index b981edc21aff..1eeaf1461a10 100644 --- a/lib/core-client/src/preview/start.test.ts +++ b/lib/core-client/src/preview/start.test.ts @@ -322,6 +322,43 @@ describe('start', () => { ); }); + it('allows setting compomnent/args/argTypes via a parameter', async () => { + const render = jest.fn(({ storyFn }) => storyFn()); + + const { configure, clientApi } = start(render); + + const component = {}; + configure('test', () => { + clientApi + .storiesOf('Component A', { id: 'file1' } as NodeModule) + .addParameters({ + component, + args: { a: 'a' }, + argTypes: { a: { type: 'string' } }, + }) + .add('default', jest.fn(), { + args: { b: 'b' }, + argTypes: { b: { type: 'string' } }, + }); + }); + + await waitForRender(); + + expect(render).toHaveBeenCalledWith( + expect.objectContaining({ + storyContext: expect.objectContaining({ + component, + args: { a: 'a', b: 'b' }, + argTypes: { + a: { name: 'a', type: { name: 'string' } }, + b: { name: 'b', type: { name: 'string' } }, + }, + }), + }), + undefined + ); + }); + it('supports forceRerender()', async () => { const render = jest.fn(({ storyFn }) => storyFn());