Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Allow args/argTypes/component to be set via parameters for storiesOf back-compat #16791

Merged
merged 2 commits into from Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/client-api/src/ClientApi.ts
Expand Up @@ -316,7 +316,7 @@ export class ClientApi<TFramework extends AnyFramework> {
);
}

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);
Expand All @@ -328,6 +328,9 @@ export class ClientApi<TFramework extends AnyFramework> {
parameters: { fileName, __id: storyId, ...storyParameters },
decorators,
loaders,
args,
argTypes,
component,
render: storyFn,
};
counter += 1;
Expand Down Expand Up @@ -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;
};

Expand Down
37 changes: 37 additions & 0 deletions lib/core-client/src/preview/start.test.ts
Expand Up @@ -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());

Expand Down