Skip to content

Commit

Permalink
feat(angular): only keeps args with a control or an action in argTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaudAV committed May 1, 2021
1 parent 0ec9589 commit 97631ce
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
29 changes: 29 additions & 0 deletions app/angular/src/client/preview/decorateStory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,35 @@ describe('decorateStory', () => {
template: '<parent></parent>',
});
});

it('should only keeps args with a control or an action in argTypes', () => {
const decorated = decorateStory(
(context: StoryContext) => ({
template: `Args available in the story : ${Object.keys(context.args).join()}`,
}),
[]
);

expect(
decorated(
makeContext({
parameters: { component: FooComponent },
argTypes: {
withControl: { control: { type: 'object' }, name: 'withControl' },
withAction: { action: 'onClick', name: 'withAction' },
toRemove: { name: 'toRemove' },
},
args: {
withControl: 'withControl',
withAction: () => ({}),
toRemove: 'toRemove',
},
})
)
).toEqual({
template: 'Args available in the story : withControl,withAction',
});
});
});

describe('default behavior', () => {
Expand Down
22 changes: 21 additions & 1 deletion app/angular/src/client/preview/decorateStory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function decorateStory(
mainStoryFn: StoryFn<StoryFnAngularReturnType>,
decorators: DecoratorFunction<StoryFnAngularReturnType>[]
): StoryFn<StoryFnAngularReturnType> {
const returnDecorators = decorators.reduce(
const returnDecorators = [cleanArgsDecorator, ...decorators].reduce(
(previousStoryFn: StoryFn<StoryFnAngularReturnType>, decorator) => (
context: StoryContext = defaultContext
) => {
Expand Down Expand Up @@ -56,3 +56,23 @@ const prepareMain = (
function hasNoTemplate(template: string | null | undefined): template is undefined {
return template === null || template === undefined;
}

const cleanArgsDecorator: DecoratorFunction<StoryFnAngularReturnType> = (storyFn, context) => {
if (!context.argTypes || !context.args) {
return storyFn();
}

const argsToClean = context.args;

context.args = Object.entries(argsToClean).reduce((obj, [key, arg]) => {
const argType = context.argTypes[key];

// Only keeps args with a control or an action in argTypes
if (argType.action || argType.control) {
return { ...obj, [key]: arg };
}
return obj;
}, {});

return storyFn();
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ export default {
moduleMetadata({
imports: [FormsModule],
}),
(storyFn) => {
const stroy = storyFn();

console.log(stroy);

return stroy;
},
],
} as Meta;

Expand Down

0 comments on commit 97631ce

Please sign in to comment.