From 020b10fdb9708a71bfc01de040b796fb3182c7f1 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Mon, 28 Jun 2021 16:12:35 +1000 Subject: [PATCH] Don't override existing action args --- .../actions/src/preset/addArgsHelpers.test.ts | 66 ++++++++++++++++--- addons/actions/src/preset/addArgsHelpers.ts | 12 +++- 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/addons/actions/src/preset/addArgsHelpers.test.ts b/addons/actions/src/preset/addArgsHelpers.test.ts index 6132e1a49b6f..ecece9bcae59 100644 --- a/addons/actions/src/preset/addArgsHelpers.test.ts +++ b/addons/actions/src/preset/addArgsHelpers.test.ts @@ -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); @@ -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({}); }); @@ -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) diff --git a/addons/actions/src/preset/addArgsHelpers.ts b/addons/actions/src/preset/addArgsHelpers.ts index 13e775042c3c..abe4456d8aad 100644 --- a/addons/actions/src/preset/addArgsHelpers.ts +++ b/addons/actions/src/preset/addArgsHelpers.ts @@ -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 {}; @@ -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); }; @@ -37,6 +40,7 @@ export const inferActionsFromArgTypesRegex: ArgsEnhancer = (context) => { */ export const addActionsFromArgTypes: ArgsEnhancer = (context) => { const { + args, argTypes, parameters: { actions }, } = context; @@ -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); };