-
-
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 #26349 from storybookjs/feature/portable-stories-s…
…velte-renderer Svelte: Add experimental portable stories support
- Loading branch information
Showing
65 changed files
with
13,996 additions
and
29 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
97 changes: 97 additions & 0 deletions
97
code/.yarn/patches/@testing-library-svelte-npm-4.1.0-34b7037bc0.patch
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
3 changes: 3 additions & 0 deletions
3
code/renderers/svelte/src/__test__/composeStories/AddWrapperDecorator.svelte
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,3 @@ | ||
<div style="margin: 3em;" data-testid="local-decorator"> | ||
<slot/> | ||
</div> |
154 changes: 154 additions & 0 deletions
154
code/renderers/svelte/src/__test__/composeStories/Button.stories.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { expect, fn, userEvent, within } from '@storybook/test'; | ||
import type { Meta, StoryFn as CSF2Story, StoryObj } from '../..'; | ||
|
||
import LoaderStoryComponent from './LoaderStoryComponent.svelte'; | ||
import InputFilledStoryComponent from './InputFilledStoryComponent.svelte'; | ||
import StoryWithLocaleComponent from './StoryWithLocaleComponent.svelte'; | ||
import AddWrapperDecorator from './AddWrapperDecorator.svelte'; | ||
import CustomRenderComponent from './CustomRenderComponent.svelte'; | ||
|
||
import Button from './Button.svelte'; | ||
|
||
const meta = { | ||
title: 'Example/Button', | ||
component: Button, | ||
argTypes: { | ||
backgroundColor: { control: 'color' }, | ||
size: { | ||
control: { type: 'select' }, | ||
options: ['small', 'medium', 'large'], | ||
}, | ||
}, | ||
excludeStories: /.*ImNotAStory$/, | ||
} satisfies Meta<Button>; | ||
|
||
export default meta; | ||
type CSF3Story = StoryObj<typeof meta>; | ||
|
||
// For testing purposes. Should be ignored in ComposeStories | ||
export const ImNotAStory = 123; | ||
|
||
const Template: CSF2Story = (args) => ({ | ||
Component: Button, | ||
props: args, | ||
}); | ||
|
||
export const CSF2Secondary = Template.bind({}); | ||
CSF2Secondary.args = { | ||
label: 'label coming from story args!', | ||
primary: false, | ||
}; | ||
|
||
const getCaptionForLocale = (locale: string) => { | ||
switch (locale) { | ||
case 'es': | ||
return 'Hola!'; | ||
case 'fr': | ||
return 'Bonjour!'; | ||
case 'kr': | ||
return '안녕하세요!'; | ||
case 'pt': | ||
return 'Olá!'; | ||
default: | ||
return 'Hello!'; | ||
} | ||
}; | ||
|
||
export const CSF2StoryWithLocale: CSF2Story<StoryWithLocaleComponent> = (args, { globals }) => ({ | ||
Component: StoryWithLocaleComponent, | ||
props: { | ||
...args, | ||
locale: globals.locale, | ||
label: getCaptionForLocale(globals.locale), | ||
}, | ||
}); | ||
CSF2StoryWithLocale.storyName = 'WithLocale'; | ||
|
||
export const CSF2StoryWithParamsAndDecorator = Template.bind({}); | ||
CSF2StoryWithParamsAndDecorator.args = { | ||
label: 'foo', | ||
}; | ||
CSF2StoryWithParamsAndDecorator.parameters = { | ||
layout: 'centered', | ||
}; | ||
CSF2StoryWithParamsAndDecorator.decorators = [ | ||
() => ({ | ||
Component: AddWrapperDecorator, | ||
}), | ||
]; | ||
|
||
export const NewStory: CSF3Story = { | ||
args: { | ||
label: 'foo', | ||
size: 'large', | ||
primary: true, | ||
}, | ||
decorators: [ | ||
() => ({ | ||
Component: AddWrapperDecorator, | ||
}), | ||
], | ||
}; | ||
|
||
export const CSF3Primary: CSF3Story = { | ||
args: { | ||
label: 'foo', | ||
size: 'large', | ||
primary: true, | ||
}, | ||
}; | ||
|
||
export const CSF3Button: CSF3Story = { | ||
args: { label: 'foo' }, | ||
}; | ||
|
||
export const CSF3ButtonWithRender: StoryObj<CustomRenderComponent> = { | ||
args: { | ||
buttonProps: CSF3Button.args, | ||
}, | ||
render: (args) => ({ | ||
Component: CustomRenderComponent, | ||
props: { | ||
buttonProps: args.buttonProps, | ||
}, | ||
}), | ||
}; | ||
|
||
export const CSF3InputFieldFilled: StoryObj<InputFilledStoryComponent> = { | ||
render: () => ({ | ||
Component: InputFilledStoryComponent, | ||
}), | ||
play: async ({ canvasElement, step }) => { | ||
const canvas = within(canvasElement); | ||
await step('Step label', async () => { | ||
const inputEl = canvas.getByTestId('input'); | ||
await userEvent.type(inputEl, 'Hello world!'); | ||
await expect(inputEl).toHaveValue('Hello world!'); | ||
}); | ||
}, | ||
}; | ||
|
||
const mockFn = fn(); | ||
export const LoaderStory: StoryObj<LoaderStoryComponent> = { | ||
args: { | ||
mockFn, | ||
}, | ||
loaders: [ | ||
async () => { | ||
mockFn.mockReturnValueOnce('mockFn return value'); | ||
return { | ||
value: 'loaded data', | ||
}; | ||
}, | ||
], | ||
render: (args, { loaded }) => ({ | ||
Component: LoaderStoryComponent, | ||
props: { | ||
...args, | ||
loaded, | ||
}, | ||
}), | ||
play: async () => { | ||
expect(mockFn).toHaveBeenCalledWith('render'); | ||
}, | ||
}; |
33 changes: 33 additions & 0 deletions
33
code/renderers/svelte/src/__test__/composeStories/Button.svelte
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,33 @@ | ||
<script lang="ts"> | ||
/** | ||
* Is this the principal call to action on the page? | ||
*/ | ||
export let primary = false; | ||
/** | ||
* What background color to use | ||
*/ | ||
export let backgroundColor: string | undefined = undefined; | ||
/** | ||
* How large should the button be? | ||
*/ | ||
export let size: 'small' | 'medium' | 'large' = 'medium'; | ||
/** | ||
* Button contents | ||
*/ | ||
export let label: string = ''; | ||
$: mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary'; | ||
$: style = backgroundColor ? `background-color: ${backgroundColor}` : ''; | ||
</script> | ||
|
||
<button | ||
type="button" | ||
class={['storybook-button', `storybook-button--${size}`, mode].join(' ')} | ||
{style} | ||
on:click | ||
> | ||
{label} | ||
<slot/> | ||
</button> |
11 changes: 11 additions & 0 deletions
11
code/renderers/svelte/src/__test__/composeStories/CustomRenderComponent.svelte
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,11 @@ | ||
<script lang="ts"> | ||
import Button from './Button.svelte'; | ||
import { ComponentProps } from 'svelte'; | ||
export let buttonProps: ComponentProps<Button>; | ||
</script> | ||
|
||
<div> | ||
<p data-testid="custom-render">I am a custom render function</p> | ||
<Button {...buttonProps} /> | ||
</div> |
1 change: 1 addition & 0 deletions
1
code/renderers/svelte/src/__test__/composeStories/InputFilledStoryComponent.svelte
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 @@ | ||
<input data-testid="input" /> |
15 changes: 15 additions & 0 deletions
15
code/renderers/svelte/src/__test__/composeStories/LoaderStoryComponent.svelte
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,15 @@ | ||
<script> | ||
export let mockFn; | ||
export let loaded; | ||
let data; | ||
$: if (mockFn && loaded) { | ||
data = mockFn('render'); | ||
} | ||
</script> | ||
|
||
<div> | ||
<div data-testid="loaded-data">{loaded.value}</div> | ||
<div data-testid="spy-data">{String(data)}</div> | ||
</div> |
Oops, something went wrong.