-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Vue3: CSF factories support #33365
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
Open
kasperpeulen
wants to merge
7
commits into
next
Choose a base branch
from
kasper/vue-csf-factories
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+378
−10
Open
Vue3: CSF factories support #33365
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
56aa06d
Fix type of meta.input.play is undefined in CSF factories
kasperpeulen 28ea8b0
Only allow empty story when all required args are provided elsewhere
kasperpeulen c59a0a2
Filter out non-factory stories, if at least story is a factory
kasperpeulen e0c73db
Merge branch 'kasper/meta-play' into kasper/empty-story-factory
kasperpeulen f817405
Fix mount destructuring in Story.test()
kasperpeulen a6d79ae
Start with Vue3 work
kasperpeulen 6e5a012
Better inference
kasperpeulen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -77,7 +77,7 @@ describe('Args can be provided in multiple ways', () => { | |||||||||||||||
| args: { label: 'good' }, | ||||||||||||||||
| }); | ||||||||||||||||
| // @ts-expect-error disabled not provided ❌ | ||||||||||||||||
| const Basic = meta.story({}); | ||||||||||||||||
| const Basic = meta.story(); | ||||||||||||||||
| } | ||||||||||||||||
| { | ||||||||||||||||
| const meta = preview.meta({ component: Button }); | ||||||||||||||||
|
|
@@ -384,3 +384,20 @@ describe('Composed getters', () => { | |||||||||||||||
| expect(renderSpy).toHaveBeenCalled(); | ||||||||||||||||
| }); | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| it('meta.input also contains play', () => { | ||||||||||||||||
| const meta = preview.meta({ | ||||||||||||||||
| /** Title, component, etc... */ | ||||||||||||||||
| play: async ({ canvas }) => { | ||||||||||||||||
| /** Do some common interactions */ | ||||||||||||||||
| }, | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| const ExtendedInteractionsStory = meta.story({ | ||||||||||||||||
| play: async ({ canvas, ...rest }) => { | ||||||||||||||||
| await meta.input.play?.({ canvas, ...rest }); | ||||||||||||||||
|
|
||||||||||||||||
| /** Do some extra interactions */ | ||||||||||||||||
| }, | ||||||||||||||||
| }); | ||||||||||||||||
|
Comment on lines
+396
to
+402
|
||||||||||||||||
| const ExtendedInteractionsStory = meta.story({ | |
| play: async ({ canvas, ...rest }) => { | |
| await meta.input.play?.({ canvas, ...rest }); | |
| /** Do some extra interactions */ | |
| }, | |
| }); |
This file contains hidden or 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 hidden or 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,184 @@ | ||
| // this file tests Typescript types that's why there are no assertions | ||
| import { describe, expect, expectTypeOf, it, test } from 'vitest'; | ||
|
|
||
| import type { Canvas, ComponentAnnotations, StoryAnnotations } from 'storybook/internal/types'; | ||
|
|
||
| import { h } from 'vue'; | ||
|
|
||
| import BaseLayout from './__tests__/BaseLayout.vue'; | ||
| import Button from './__tests__/Button.vue'; | ||
| import Decorator2TsVue from './__tests__/Decorator2.vue'; | ||
| import DecoratorTsVue from './__tests__/Decorator.vue'; | ||
| import { __definePreview } from './preview'; | ||
| import type { ComponentPropsAndSlots, Decorator, Meta, StoryObj } from './public-types'; | ||
|
|
||
| type ButtonProps = ComponentPropsAndSlots<typeof Button>; | ||
|
|
||
| const preview = __definePreview({ | ||
| addons: [], | ||
| }); | ||
|
|
||
| test('csf factories', () => { | ||
| const config = __definePreview({ | ||
| addons: [ | ||
| { | ||
| decorators: [], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| const meta = config.meta({ component: Button, args: { disabled: false } }); | ||
|
|
||
| const MyStory = meta.story({ | ||
| args: { | ||
| label: 'Hello world', | ||
| }, | ||
| }); | ||
|
|
||
| expect(MyStory.input.args?.label).toBe('Hello world'); | ||
| }); | ||
|
|
||
| describe('Meta', () => { | ||
| it('Generic parameter of Meta can be a component', () => { | ||
| const meta = preview.meta({ | ||
| component: Button, | ||
| args: { label: 'good', disabled: false }, | ||
| }); | ||
| }); | ||
|
|
||
| it('Events are inferred from component', () => { | ||
| const meta = preview.meta({ | ||
| component: Button, | ||
| args: { | ||
| label: 'good', | ||
| disabled: false, | ||
| onMyChangeEvent: (value) => { | ||
| expectTypeOf(value).toMatchTypeOf<number>(); | ||
| }, | ||
| }, | ||
| render: (args) => { | ||
| return h(Button, { | ||
| ...args, | ||
| onMyChangeEvent: (value) => { | ||
| expectTypeOf(value).toMatchTypeOf<number>(); | ||
| }, | ||
| }); | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('StoryObj', () => { | ||
| it('✅ Required args may be provided partial in meta and the story', () => { | ||
| const meta = preview.meta({ | ||
| component: Button, | ||
| args: { label: 'good' }, | ||
| }); | ||
|
|
||
| const Story = meta.story({ | ||
| args: { | ||
| disabled: true, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('❌ The combined shape of meta args and story args must match the required args.', () => { | ||
| { | ||
| const meta = preview.meta({ | ||
| component: Button, | ||
| args: { label: 'good' }, | ||
| }); | ||
| // @ts-expect-error disabled not provided ❌ | ||
| const Basic = meta.story(); | ||
| } | ||
| { | ||
| const meta = preview.meta({ component: Button }); | ||
| // @ts-expect-error disabled not provided ❌ | ||
| const Basic = meta.story({ | ||
| args: { label: 'good' }, | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| type ThemeData = 'light' | 'dark'; | ||
|
|
||
| describe('Story args can be inferred', () => { | ||
| it('Correct args are inferred when type is widened for render function', () => { | ||
| const meta = preview.meta({ | ||
| render: (args: ButtonProps & { theme: ThemeData }) => { | ||
| return h('div', [h('div', `Use the theme ${args.theme}`), h(Button, args)]); | ||
| }, | ||
| args: { disabled: false }, | ||
| }); | ||
|
|
||
| const Basic = meta.story({ args: { theme: 'light', label: 'good' } }); | ||
| }); | ||
|
|
||
| const withDecorator: Decorator<{ decoratorArg: string }> = ( | ||
| storyFn, | ||
| { args: { decoratorArg } } | ||
| ) => h(DecoratorTsVue, { decoratorArg }, h(storyFn())); | ||
|
|
||
| it('Correct args are inferred when type is widened for decorators', () => { | ||
| type Props = ButtonProps & { decoratorArg: string }; | ||
|
|
||
| const meta = preview.meta({ | ||
| component: Button, | ||
| args: { disabled: false }, | ||
| decorators: [withDecorator], | ||
| }); | ||
|
|
||
| const Basic = meta.story({ args: { decoratorArg: 'title', label: 'good' } }); | ||
| }); | ||
|
|
||
| it('Correct args are inferred when type is widened for multiple decorators', () => { | ||
| type Props = ButtonProps & { | ||
| decoratorArg: string; | ||
| decoratorArg2: string; | ||
| }; | ||
|
|
||
| const secondDecorator: Decorator<{ decoratorArg2: string }> = ( | ||
| storyFn, | ||
| { args: { decoratorArg2 } } | ||
| ) => h(Decorator2TsVue, { decoratorArg2 }, h(storyFn())); | ||
|
|
||
| const meta = preview.meta({ | ||
| component: Button, | ||
| args: { disabled: false }, | ||
| decorators: [withDecorator, secondDecorator], | ||
| }); | ||
|
|
||
| const Basic = meta.story({ | ||
| args: { decoratorArg: '', decoratorArg2: '', label: 'good' }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('Infer type of slots', () => { | ||
| const meta = preview.meta({ | ||
| component: BaseLayout, | ||
| }); | ||
|
|
||
| const Basic = meta.story({ | ||
| args: { | ||
| otherProp: true, | ||
| header: ({ title }) => | ||
| h({ | ||
| components: { Button }, | ||
| template: `<Button :primary='true' label='${title}'></Button>`, | ||
| }), | ||
| default: 'default slot', | ||
| footer: h(Button, { disabled: true, label: 'footer' }), | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('mount accepts a Component', () => { | ||
| const Basic: StoryObj<typeof Button> = { | ||
| async play({ mount }) { | ||
| const canvas = await mount(Button, { props: { label: 'label', disabled: true } }); | ||
| expectTypeOf(canvas).toMatchTypeOf<Canvas>(); | ||
| }, | ||
| }; | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Unused variable Basic.