Skip to content

Commit

Permalink
Merge pull request #15394 from storybookjs/15370-dont-override-existi…
Browse files Browse the repository at this point in the history
…ng-action-args

Actions: Don't override existing action args
  • Loading branch information
shilman committed Jun 28, 2021
2 parents 5ffe201 + 020b10f commit 203ef37
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
66 changes: 57 additions & 9 deletions addons/actions/src/preset/addArgsHelpers.test.ts
Expand Up @@ -8,6 +8,7 @@ describe('actions parameter enhancers', () => {

it('should add actions that match a pattern', () => {
const args = inferActionsFromArgTypesRegex(({
args: {},
argTypes,
parameters,
} as unknown) as StoryContext);
Expand All @@ -17,25 +18,41 @@ describe('actions parameter enhancers', () => {
});
});

it('should override pre-existing argTypes', () => {
it('should NOT override pre-existing args', () => {
const args = inferActionsFromArgTypesRegex(({
args: { onClick: 'pre-existing value' },
argTypes,
parameters,
argTypes: {
onClick: { defaultValue: 'pre-existing value' },
},
} as unknown) as StoryContext);
expect(args).toEqual({
onClick: expect.any(Function),
});
expect(args).toEqual({ onFocus: expect.any(Function) });
});

it('should NOT override pre-existing args, if null', () => {
const args = inferActionsFromArgTypesRegex(({
args: { onClick: null },
argTypes,
parameters,
} as unknown) as StoryContext);
expect(args).toEqual({ onFocus: expect.any(Function) });
});

it('should override pre-existing args, if undefined', () => {
const args = inferActionsFromArgTypesRegex(({
args: { onClick: undefined },
argTypes,
parameters,
} as unknown) as StoryContext);
expect(args).toEqual({ onClick: expect.any(Function), onFocus: expect.any(Function) });
});

it('should do nothing if actions are disabled', () => {
const args = inferActionsFromArgTypesRegex(({
args: {},
argTypes,
parameters: {
...parameters,
actions: { ...parameters.actions, disable: true },
},
argTypes,
} as unknown) as StoryContext);
expect(args).toEqual({});
});
Expand All @@ -48,16 +65,47 @@ describe('actions parameter enhancers', () => {
};
it('should add actions based on action.args', () => {
expect(
addActionsFromArgTypes(({ argTypes, parameters: {} } as unknown) as StoryContext)
addActionsFromArgTypes(({ args: {}, argTypes, parameters: {} } as unknown) as StoryContext)
).toEqual({
onClick: expect.any(Function),
onBlur: expect.any(Function),
});
});

it('should NOT override pre-existing args', () => {
expect(
addActionsFromArgTypes(({
argTypes: { onClick: { action: 'clicked!' } },
args: { onClick: 'pre-existing value' },
parameters: {},
} as unknown) as StoryContext)
).toEqual({});
});

it('should NOT override pre-existing args, if null', () => {
expect(
addActionsFromArgTypes(({
argTypes: { onClick: { action: 'clicked!' } },
args: { onClick: null },
parameters: {},
} as unknown) as StoryContext)
).toEqual({});
});

it('should override pre-existing args, if undefined', () => {
expect(
addActionsFromArgTypes(({
argTypes: { onClick: { action: 'clicked!' } },
args: { onClick: undefined },
parameters: {},
} as unknown) as StoryContext)
).toEqual({ onClick: expect.any(Function) });
});

it('should do nothing if actions are disabled', () => {
expect(
addActionsFromArgTypes(({
args: {},
argTypes,
parameters: { actions: { disable: true } },
} as unknown) as StoryContext)
Expand Down
12 changes: 9 additions & 3 deletions addons/actions/src/preset/addArgsHelpers.ts
Expand Up @@ -14,8 +14,9 @@ import { action } from '../index';

export const inferActionsFromArgTypesRegex: ArgsEnhancer = (context) => {
const {
parameters: { actions },
args,
argTypes,
parameters: { actions },
} = context;
if (!actions || actions.disable || !actions.argTypesRegex || !argTypes) {
return {};
Expand All @@ -27,7 +28,9 @@ export const inferActionsFromArgTypesRegex: ArgsEnhancer = (context) => {
);

return argTypesMatchingRegex.reduce((acc, [name, argType]) => {
acc[name] = action(name);
if (typeof args[name] === 'undefined') {
acc[name] = action(name);
}
return acc;
}, {} as Args);
};
Expand All @@ -37,6 +40,7 @@ export const inferActionsFromArgTypesRegex: ArgsEnhancer = (context) => {
*/
export const addActionsFromArgTypes: ArgsEnhancer = (context) => {
const {
args,
argTypes,
parameters: { actions },
} = context;
Expand All @@ -47,7 +51,9 @@ export const addActionsFromArgTypes: ArgsEnhancer = (context) => {
const argTypesWithAction = Object.entries(argTypes).filter(([name, argType]) => !!argType.action);

return argTypesWithAction.reduce((acc, [name, argType]) => {
acc[name] = action(typeof argType.action === 'string' ? argType.action : name);
if (typeof args[name] === 'undefined') {
acc[name] = action(typeof argType.action === 'string' ? argType.action : name);
}
return acc;
}, {} as Args);
};

0 comments on commit 203ef37

Please sign in to comment.