-
-
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
Changes from all commits
e29d0e7
e8f08e0
07172c3
d2bbf3a
37a904c
e8117fd
fc05a7c
648e6e0
1ee3e0a
246fc20
76ebe43
6dbdb59
770678a
b650ca5
36bace3
613b4cb
4f135ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export { storiesOf, setAddon, addDecorator, configure, getStorybook } from './preview'; | ||
|
||
export { moduleMetadata } from './preview/angular/decorators'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { moduleMetadata } from './decorators'; | ||
import { addDecorator, storiesOf, clearDecorators, getStorybook } from '..'; | ||
|
||
class MockModule {} | ||
class MockModuleTwo {} | ||
class MockService {} | ||
class MockComponent {} | ||
|
||
describe('moduleMetadata', () => { | ||
it('should add metadata to a story without it', () => { | ||
const result = moduleMetadata({ | ||
imports: [MockModule], | ||
providers: [MockService], | ||
})(() => ({ | ||
component: MockComponent, | ||
})); | ||
|
||
expect(result).toEqual({ | ||
component: MockComponent, | ||
moduleMetadata: { | ||
declarations: [], | ||
entryComponents: [], | ||
imports: [MockModule], | ||
schemas: [], | ||
providers: [MockService], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should combine with individual metadata on a story', () => { | ||
const result = moduleMetadata({ | ||
imports: [MockModule], | ||
})(() => ({ | ||
component: MockComponent, | ||
moduleMetadata: { | ||
imports: [MockModuleTwo], | ||
providers: [MockService], | ||
}, | ||
})); | ||
|
||
expect(result).toEqual({ | ||
component: MockComponent, | ||
moduleMetadata: { | ||
declarations: [], | ||
entryComponents: [], | ||
imports: [MockModule, MockModuleTwo], | ||
schemas: [], | ||
providers: [MockService], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return the original metadata if passed null', () => { | ||
const result = moduleMetadata(null)(() => ({ | ||
component: MockComponent, | ||
moduleMetadata: { | ||
providers: [MockService], | ||
}, | ||
})); | ||
|
||
expect(result).toEqual({ | ||
component: MockComponent, | ||
moduleMetadata: { | ||
declarations: [], | ||
entryComponents: [], | ||
imports: [], | ||
schemas: [], | ||
providers: [MockService], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should work when added globally', () => { | ||
const metadata = { | ||
declarations: [MockComponent], | ||
providers: [MockService], | ||
entryComponents: [MockComponent], | ||
imports: [MockModule], | ||
}; | ||
|
||
addDecorator(moduleMetadata(metadata)); | ||
|
||
storiesOf('Test', module).add('Default', () => ({ | ||
component: MockComponent, | ||
})); | ||
|
||
const [storybook] = getStorybook(); | ||
|
||
expect(storybook.stories[0].render().moduleMetadata).toEqual({ | ||
declarations: [MockComponent], | ||
providers: [MockService], | ||
entryComponents: [MockComponent], | ||
imports: [MockModule], | ||
schemas: [], | ||
}); | ||
|
||
clearDecorators(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NgModuleMetadata } from './types'; | ||
|
||
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 commentThe 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 commentThe 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 |
||
|
||
return { | ||
...story, | ||
moduleMetadata: { | ||
declarations: [...(metadata.declarations || []), ...(storyMetadata.declarations || [])], | ||
entryComponents: [ | ||
...(metadata.entryComponents || []), | ||
...(storyMetadata.entryComponents || []), | ||
], | ||
imports: [...(metadata.imports || []), ...(storyMetadata.imports || [])], | ||
schemas: [...(metadata.schemas || []), ...(storyMetadata.schemas || [])], | ||
providers: [...(metadata.providers || []), ...(storyMetadata.providers || [])], | ||
}, | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export function storiesOf(kind: string, module: NodeModule): IApi; | ||
export function setAddon(addon: any): void; | ||
export function addDecorator(decorator: any): IApi; | ||
export function configure(loaders: () => NodeRequire, module: NodeModule): void; | ||
export function getStorybook(): IStoribookSection[]; | ||
export function clearDecorators(): void; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Storyshots Metadata|Combined Combined 1 1`] = ` | ||
<storybook-dynamic-app-root | ||
cfr={[Function CodegenComponentFactoryResolver]} | ||
data={[Function Object]} | ||
target={[Function ViewContainerRef_]} | ||
> | ||
<ng-component> | ||
<storybook-simple-token-component | ||
ng-reflect-name="Prop Name" | ||
> | ||
|
||
|
||
<h3> | ||
Prop Name | ||
</h3> | ||
|
||
|
||
<p> | ||
Items: | ||
</p> | ||
|
||
|
||
<ul> | ||
|
||
|
||
|
||
<li> | ||
|
||
Joe | ||
|
||
</li> | ||
<li> | ||
|
||
Jane | ||
|
||
</li> | ||
|
||
|
||
</ul> | ||
|
||
|
||
</storybook-simple-token-component> | ||
</ng-component> | ||
</storybook-dynamic-app-root> | ||
`; | ||
|
||
exports[`Storyshots Metadata|Combined Combined 2 1`] = ` | ||
<storybook-dynamic-app-root | ||
cfr={[Function CodegenComponentFactoryResolver]} | ||
data={[Function Object]} | ||
target={[Function ViewContainerRef_]} | ||
> | ||
<ng-component> | ||
<storybook-simple-token-component | ||
ng-reflect-name="CustomPipe: Prop Name" | ||
> | ||
|
||
|
||
<h3> | ||
CustomPipe: Prop Name | ||
</h3> | ||
|
||
|
||
<p> | ||
Items: | ||
</p> | ||
|
||
|
||
<ul> | ||
|
||
|
||
|
||
<li> | ||
|
||
Joe | ||
|
||
</li> | ||
<li> | ||
|
||
Jane | ||
|
||
</li> | ||
|
||
|
||
</ul> | ||
|
||
|
||
</storybook-simple-token-component> | ||
</ng-component> | ||
</storybook-dynamic-app-root> | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Storyshots Metadata|Individual Individual 1 1`] = ` | ||
<storybook-dynamic-app-root | ||
cfr={[Function CodegenComponentFactoryResolver]} | ||
data={[Function Object]} | ||
target={[Function ViewContainerRef_]} | ||
> | ||
<ng-component> | ||
<storybook-simple-token-component /> | ||
</ng-component> | ||
</storybook-dynamic-app-root> | ||
`; | ||
|
||
exports[`Storyshots Metadata|Individual Individual 2 1`] = ` | ||
<storybook-dynamic-app-root | ||
cfr={[Function CodegenComponentFactoryResolver]} | ||
data={[Function Object]} | ||
target={[Function ViewContainerRef_]} | ||
> | ||
<ng-component> | ||
<storybook-simple-token-component> | ||
|
||
|
||
<h3> | ||
Provider Name | ||
</h3> | ||
|
||
|
||
<p> | ||
Items: | ||
</p> | ||
|
||
|
||
<ul> | ||
|
||
|
||
|
||
<li> | ||
|
||
Jim | ||
|
||
</li> | ||
<li> | ||
|
||
Jill | ||
|
||
</li> | ||
|
||
|
||
</ul> | ||
|
||
|
||
</storybook-simple-token-component> | ||
</ng-component> | ||
</storybook-dynamic-app-root> | ||
`; |
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:
I would expect to have both
FooModule
andBarModule
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.