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

Controls: Don't set arg in validateOptions if it would be undefined #15654

Merged
merged 1 commit into from Jul 22, 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
5 changes: 5 additions & 0 deletions lib/client-api/src/args.test.ts
Expand Up @@ -185,6 +185,11 @@ describe('combineArgs', () => {
});

describe('validateOptions', () => {
// https://github.com/storybookjs/storybook/issues/15630
it('does not set args to `undefined` if they are unset', () => {
expect(validateOptions({}, { a: {} })).toStrictEqual({});
});

it('omits arg and warns if value is not one of options', () => {
expect(validateOptions({ a: 1 }, { a: { options: [2, 3] } })).toStrictEqual({});
expect(once.warn).toHaveBeenCalledWith(
Expand Down
4 changes: 3 additions & 1 deletion lib/client-api/src/args.ts
Expand Up @@ -72,7 +72,9 @@ export const combineArgs = (value: any, update: any): Args => {
export const validateOptions = (args: Args, argTypes: ArgTypes): Args => {
return Object.entries(argTypes).reduce((acc, [key, { options }]) => {
if (!options) {
acc[key] = args[key];
if (key in args) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also use args[key] !== undefined, or various other techniques. I honestly don't know what's the right way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I think this is fine and better than checking against undefined. The alternative would be hasOwnProperty, but since we're dealing with plain objects, it doesn't matter.

acc[key] = args[key];
}
return acc;
}

Expand Down