-
-
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 9 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,10 @@ | ||
export { storiesOf, setAddon, addDecorator, configure, getStorybook } from './preview'; | ||
export { | ||
storiesOf, | ||
setAddon, | ||
addDecorator, | ||
configure, | ||
getStorybook, | ||
clearDecorators, | ||
} 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'; | ||
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. 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 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. I have modified it to combine rather than override. |
||
import { addDecorator, storiesOf, clearDecorators, getStorybook } from '@storybook/angular'; | ||
|
||
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 || [])], | ||
}, | ||
}; | ||
}; |
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.