-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2959 from jonrimmer/angular-modulemetadata-decorator
Add moduleMetdata decorator for supplying common Angular metadata
- Loading branch information
Showing
19 changed files
with
586 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 || {}; | ||
|
||
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 || [])], | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
examples/angular-cli/src/stories/__snapshots__/metadata-combined.stories.storyshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
`; |
57 changes: 57 additions & 0 deletions
57
examples/angular-cli/src/stories/__snapshots__/metadata-individual.stories.storyshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
`; |
Oops, something went wrong.