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

Actions: Don't override existing action args #15394

Merged
merged 1 commit into from Jun 28, 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
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);
};