-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Add moduleMetdata decorator for supplying common Angular metadata #2959
Add moduleMetdata decorator for supplying common Angular metadata #2959
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2959 +/- ##
==========================================
+ Coverage 37.28% 37.32% +0.04%
==========================================
Files 435 435
Lines 9432 9427 -5
Branches 926 896 -30
==========================================
+ Hits 3517 3519 +2
+ Misses 5336 5335 -1
+ Partials 579 573 -6
Continue to review full report at Codecov.
|
I'm not really sure why those CI tests are failing? |
I'll check your PR today =) Thanks ! |
@@ -1,4 +1,4 @@ | |||
import { storiesOf } from '@storybook/angular'; | |||
import { storiesOf, moduleMetadata } from '@storybook/angular'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think better having a separate example of this usage, also because it's necessary to check it in conjunction with addon-knobs.
How will it work with template
instead of component
prop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added. Seems to work fine with template
. I don't see any reason why they should affect each other, as this only touches the metadata, and passes other parts of the story through untouched.
})(() => ({ | ||
component: MockComponent, | ||
moduleMetadata: { | ||
providers: MockService, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should it be an array?
@@ -0,0 +1,42 @@ | |||
import { moduleMetadata } from './decorators'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we also need to support an arrays concatenation...
for example:
const result = moduleMetadata({
imports: [FooModule],
})(() => ({
component: MockComponent,
moduleMetadata: {
imports: [BarModule],
}
}));
I would expect to have both FooModule
and BarModule
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have modified it to combine rather than override.
Also, I see "netlify" is failing. DId you manage to run |
@igor-dv Yeah, I can run bootstrap without any problems. Not really sure what netlify is, or how I could have broken it? It was the CI CLI step that was failing earlier. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
@alterx, do you wanna add your two cents?
export const moduleMetadata = (metadata: Partial<NgModuleMetadata>) => (storyFn: () => any) => { | ||
const story = storyFn(); | ||
const storyMetadata = story.moduleMetadata || {}; | ||
metadata = metadata || {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the default be introduced in the method signature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's how I did it originally, but default params affect undefined or no value, but not null. So I'd need to add a check for null and assignment to {}
anyway.
@@ -64,3 +64,40 @@ storiesOf('Custom ngModule metadata', module) | |||
}, | |||
}; | |||
}); | |||
|
|||
storiesOf('Common ngModule metadata', module) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, it'd be better to have a separate example that shows:
- An example with this decorator (basic usage)
- Decorator + Knobs addon
- Decorator + Knobs +
template
- Decorator + knobs +
component
We don't have official docs for most of this and we usually point people to the examples so we try to keep as many as we can with all the use cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I'm not really seeing what value these examples would have? All this decorator does is allow you to declare a set of metadata that is shared amongst your subsequent stories. Whether you're using knobs, template, component, etc. is completely orthogonal to it. Since there is already examples of those things, I'm not sure how duplicating them here would really help understanding it?
Wouldn't it make more sense to modify all the other examples to use this decorator where appropriate, i.e. instead of supplying duplicate metadata, as in custom-pipes
and custom-providers
? Then the metadata example file could just be:
- An example with metadata supplied on the story.
- Metadata supplied using this decorator.
- Metadata supplied on the story that combines with that from the decorator.
I added a small comment regarding the examples, other than that it looks good to me. Also, let's make sure that this works if we add it as a 'global' decorator (https://storybook.js.org/addons/introduction/#storybook-decorators) instead of using it inside a story (it should work but let's test it, just in case) |
# Conflicts: # examples/angular-cli/src/stories/__snapshots__/custom-providers.stories. storyshot # examples/angular-cli/src/stories/custom-metadata.stories.ts
@alterx I've added the test, though it feels a little wrong to be testing something that's purely provided by the core module here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happened to the stories? Or you didn't finish yet?
app/angular/index.d.ts
Outdated
@@ -29,4 +30,5 @@ declare module '@storybook/angular' { | |||
export function addDecorator(decorator: any): IApi; | |||
export function configure(loaders: () => NodeRequire, module: NodeModule): void; | |||
export function getStorybook(): IStoribookSection[]; | |||
export function clearDecorators(): void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this here makes it a public API. I think we don't want to expose this method.
@igor-dv I've removed WRT the stories, I added a reply about this elsewhere but it seems to have gone missing. I'm not sure it makes sense to just have a single example file using this decorator, containing examples with component, template, knobs, etc. The use of this decorator is entirely orthogonal to those use-cases, and I'm not sure they would aid users in understanding it. I think it would be better to use this decorator in all the examples, where it makes sense. E.g. in The specific metadata example(s) could then be focussed on demonstrating scenarios relating to metadata only. I.e.
I'll update the PR to reflect this approach. |
Yeah, but if it's not on a top level, IMO we can breaking change easier =)
Makes sense to me |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍺
Please update integration test screenshot for angular:
|
🎉 🍻 Thanks for all your help guys! |
Issue: #2694
What I did
This PR adds a decorator called
moduleMetadata()
to@storybook/angular
that can be used to supply common metadata to all the Angular stories registered by a call tostoriesOf()
.How to test
The new decorator has an associated test that is ran as part of the core:
yarn test --core
The
angular-cli
example has been updated to demonstrate use of the new decorator.The Angular guide has been updated to describe the use of the existing
moduleMetadata
parameter on stories, and the use of the new decorator.