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

Angular: Filter out args whose argType are missing a control or action #14779

Merged
merged 2 commits into from
May 2, 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
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,11 @@ export default {
moduleMetadata({
imports: [FormsModule],
}),
(storyFn) => {
const story = storyFn();
console.log(story);
return story;
},
],
} as Meta;

Expand Down