-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a2d028
commit 9563ce7
Showing
29 changed files
with
417 additions
and
92 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
File renamed without changes.
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
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
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,12 @@ | ||
<script lang="ts"> | ||
import { createEventDispatcher } from 'svelte'; | ||
export let disabled: boolean; | ||
export let label: string; | ||
const dispatch = createEventDispatcher(); | ||
</script> | ||
|
||
<button on:click={(e) => dispatch('clicked', e)} on:dblclick on:mousemove {disabled}> | ||
{label} | ||
</button> |
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,226 @@ | ||
import { describe, test } from '@jest/globals'; | ||
import { satisfies } from '@storybook/core-common'; | ||
import { ComponentAnnotations, StoryAnnotations } from '@storybook/csf'; | ||
import { expectTypeOf } from 'expect-type'; | ||
import { ComponentProps, SvelteComponentTyped } from 'svelte'; | ||
|
||
import { DecoratorFn, Meta, StoryObj } from '../public-types'; | ||
import { SvelteFramework } from '../types'; | ||
import Button from './Button.svelte'; | ||
import Decorator from './Decorator.svelte'; | ||
import Decorator2 from './Decorator2.svelte'; | ||
|
||
type SvelteStory<Component extends SvelteComponentTyped, Args, RequiredArgs> = StoryAnnotations< | ||
SvelteFramework<Component>, | ||
Args, | ||
RequiredArgs | ||
>; | ||
|
||
describe('Meta', () => { | ||
test('Generic parameter of Meta can be a component', () => { | ||
const meta: Meta<Button> = { | ||
component: Button, | ||
args: { | ||
label: 'good', | ||
disabled: false, | ||
}, | ||
}; | ||
|
||
expectTypeOf(meta).toEqualTypeOf< | ||
ComponentAnnotations<SvelteFramework<Button>, { disabled: boolean; label: string }> | ||
>(); | ||
}); | ||
|
||
test('Generic parameter of Meta can be the props of the component', () => { | ||
const meta: Meta<{ disabled: boolean; label: string }> = { | ||
component: Button, | ||
args: { label: 'good', disabled: false }, | ||
}; | ||
|
||
expectTypeOf(meta).toEqualTypeOf< | ||
ComponentAnnotations<SvelteFramework, { disabled: boolean; label: string }> | ||
>(); | ||
}); | ||
|
||
test('Events are inferred from component', () => { | ||
const meta: Meta<Button> = { | ||
component: Button, | ||
args: { | ||
label: 'good', | ||
disabled: false, | ||
}, | ||
render: (args) => ({ | ||
Component: Button, | ||
props: args, | ||
on: { | ||
mousemove: (event) => { | ||
expectTypeOf(event).toEqualTypeOf<MouseEvent>(); | ||
}, | ||
}, | ||
}), | ||
}; | ||
}); | ||
|
||
test('Events fallback to custom events when no component is specified', () => { | ||
const meta: Meta<{ disabled: boolean; label: string }> = { | ||
component: Button, | ||
args: { label: 'good', disabled: false }, | ||
render: (args) => ({ | ||
Component: Button, | ||
props: args, | ||
on: { | ||
mousemove: (event) => { | ||
expectTypeOf(event).toEqualTypeOf<CustomEvent>(); | ||
}, | ||
}, | ||
}), | ||
}; | ||
}); | ||
}); | ||
|
||
describe('StoryObj', () => { | ||
test('✅ Required args may be provided partial in meta and the story', () => { | ||
const meta = satisfies<Meta<Button>>()({ | ||
component: Button, | ||
args: { label: 'good' }, | ||
}); | ||
|
||
type Actual = StoryObj<typeof meta>; | ||
type Expected = SvelteStory< | ||
Button, | ||
{ disabled: boolean; label: string }, | ||
{ disabled: boolean; label?: string } | ||
>; | ||
expectTypeOf<Actual>().toEqualTypeOf<Expected>(); | ||
}); | ||
|
||
test('❌ The combined shape of meta args and story args must match the required args.', () => { | ||
{ | ||
const meta = satisfies<Meta<Button>>()({ component: Button }); | ||
|
||
type Expected = SvelteStory< | ||
Button, | ||
{ disabled: boolean; label: string }, | ||
{ disabled: boolean; label: string } | ||
>; | ||
expectTypeOf<StoryObj<typeof meta>>().toEqualTypeOf<Expected>(); | ||
} | ||
{ | ||
const meta = satisfies<Meta<Button>>()({ | ||
component: Button, | ||
args: { label: 'good' }, | ||
}); | ||
// @ts-expect-error disabled not provided ❌ | ||
const Basic: StoryObj<typeof meta> = {}; | ||
|
||
type Expected = SvelteStory< | ||
Button, | ||
{ disabled: boolean; label: string }, | ||
{ disabled: boolean; label?: string } | ||
>; | ||
expectTypeOf(Basic).toEqualTypeOf<Expected>(); | ||
} | ||
{ | ||
const meta = satisfies<Meta<{ label: string; disabled: boolean }>>()({ component: Button }); | ||
const Basic: StoryObj<typeof meta> = { | ||
// @ts-expect-error disabled not provided ❌ | ||
args: { label: 'good' }, | ||
}; | ||
|
||
type Expected = SvelteStory< | ||
Button, | ||
{ disabled: boolean; label: string }, | ||
{ disabled: boolean; label: string } | ||
>; | ||
expectTypeOf(Basic).toEqualTypeOf<Expected>(); | ||
} | ||
}); | ||
|
||
test('Component can be used as generic parameter for StoryObj', () => { | ||
type Actual = StoryObj<Button>; | ||
|
||
type Expected = SvelteStory< | ||
Button, | ||
{ disabled: boolean; label: string }, | ||
{ disabled: boolean; label: string } | ||
>; | ||
|
||
expectTypeOf<Actual>().toEqualTypeOf<Expected>(); | ||
}); | ||
}); | ||
|
||
type ThemeData = 'light' | 'dark'; | ||
|
||
describe('Story args can be inferred', () => { | ||
test('Correct args are inferred when type is widened for render function', () => { | ||
const meta = satisfies<Meta<ComponentProps<Button> & { theme: ThemeData }>>()({ | ||
component: Button, | ||
args: { disabled: false }, | ||
render: (args, { component }) => { | ||
return { | ||
Component: component, | ||
props: args, | ||
}; | ||
}, | ||
}); | ||
|
||
const Basic: StoryObj<typeof meta> = { args: { theme: 'light', label: 'good' } }; | ||
|
||
type Expected = SvelteStory< | ||
Button, | ||
{ theme: ThemeData; disabled: boolean; label: string }, | ||
{ theme: ThemeData; disabled?: boolean; label: string } | ||
>; | ||
expectTypeOf(Basic).toEqualTypeOf<Expected>(); | ||
}); | ||
|
||
const withDecorator: DecoratorFn<Decorator> = (storyFn, { args: { decoratorArg } }) => ({ | ||
Component: Decorator, | ||
props: { decoratorArg }, | ||
}); | ||
|
||
test('Correct args are inferred when type is widened for decorators', () => { | ||
type Props = ComponentProps<Button> & { decoratorArg: string }; | ||
|
||
const meta = satisfies<Meta<Props>>()({ | ||
component: Button, | ||
args: { disabled: false }, | ||
decorators: [withDecorator], | ||
}); | ||
|
||
const Basic: StoryObj<typeof meta> = { args: { decoratorArg: 'title', label: 'good' } }; | ||
|
||
type Expected = SvelteStory< | ||
Button, | ||
Props, | ||
{ decoratorArg: string; disabled?: boolean; label: string } | ||
>; | ||
expectTypeOf(Basic).toEqualTypeOf<Expected>(); | ||
}); | ||
|
||
test('Correct args are inferred when type is widened for multiple decorators', () => { | ||
type Props = ComponentProps<Button> & { decoratorArg: string; decoratorArg2: string }; | ||
|
||
const secondDecorator: DecoratorFn<Decorator2> = (storyFn, { args: { decoratorArg2 } }) => ({ | ||
Component: Decorator2, | ||
props: { decoratorArg2 }, | ||
}); | ||
|
||
const meta = satisfies<Meta<Props>>()({ | ||
component: Button, | ||
args: { disabled: false }, | ||
decorators: [withDecorator, secondDecorator], | ||
}); | ||
|
||
const Basic: StoryObj<typeof meta> = { | ||
args: { decoratorArg: '', decoratorArg2: '', label: 'good' }, | ||
}; | ||
|
||
type Expected = SvelteStory< | ||
Button, | ||
Props, | ||
{ decoratorArg: string; decoratorArg2: string; disabled?: boolean; label: string } | ||
>; | ||
expectTypeOf(Basic).toEqualTypeOf<Expected>(); | ||
}); | ||
}); |
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,8 @@ | ||
<script lang="ts"> | ||
export let decoratorArg: string; | ||
</script> | ||
|
||
<div> | ||
Decorator: {decoratorArg} | ||
<slot /> | ||
</div> |
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,8 @@ | ||
<script lang="ts"> | ||
export let decoratorArg2: string; | ||
</script> | ||
|
||
<div> | ||
Decorator: {decoratorArg2} | ||
<slot /> | ||
</div> |
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
1 change: 1 addition & 0 deletions
1
code/renderers/svelte/src/docs/extractComponentDescription.test.ts
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
Oops, something went wrong.