generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathTemplating.stories.svelte
169 lines (146 loc) · 4.47 KB
/
Templating.stories.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<script module lang="ts">
import { defineMeta, setTemplate, type Args } from '@storybook/addon-svelte-csf';
import { expect, within } from '@storybook/test';
/**
* Demonstration on the **different ways of setting a template** for `<Story />` components within one stories file.
*/
const { Story } = defineMeta({
title: 'Templating',
tags: ['autodocs'],
argTypes: {
text: { control: 'text' },
},
});
</script>
<script>
/**
* call setTemplate with a reference to any root-level snippet, for that snippet to be the fallback snippet,
* that is used in any story without explicit children.
*/
setTemplate(defaultTemplate);
</script>
{#snippet defaultTemplate(args: Args<typeof Story>)}
<h2 data-testid="heading">Default template</h2>
<p>{args?.text}</p>
{/snippet}
<!--
The simplest story has a **static** template, which doesn't consume Storybook's `args`.
These stories don't react to arg changes caused by changes in the Controls panel.
Example:
```svelte
<Story name="Static story">
<p>Static template</p>
</Story>
```
-->
<Story
name="Static template"
play={async (context) => {
const { canvasElement } = context;
const canvas = within(canvasElement);
const h2 = await canvas.findByTestId('heading');
expect(h2).toHaveTextContent('Static template');
}}
>
<h2 data-testid="heading">Static template</h2>
<p>
This story is static and isn't defined with a snippet. It will ignore any <code>args</code> changes
because they are not passed in.
</p>
</Story>
<!--
Pass a `children` snippet to the story to make it dynamic and react to Storybook's `args` changes.
The snippet takes two arguments, `args` and `context`.
Example:
```svelte
<Story name="Dynamic story">
{#snippet children(args)}
<SomeComponent {...args}>
Dynamic template
</SomeComponent>
{/snippet}
</Story>
```
-->
<Story
name="Children snippet"
args={{ text: 'This story uses a children snippet' }}
play={async (context) => {
const { args, canvasElement } = context;
const canvas = within(canvasElement);
const h2 = await canvas.findByTestId('heading');
const p = await canvas.findByText(args.text);
expect(h2).toBeInTheDocument();
expect(p).toBeInTheDocument();
}}
>
{#snippet children(args)}
<h2 data-testid="heading">Children snippet</h2>
<p>{args?.text}</p>
{/snippet}
</Story>
{#snippet sharedTemplate(args: Args<typeof Story>)}
<h2 data-testid="heading">Shared template</h2>
<p>{args?.text}</p>
{/snippet}
<!--
If you want to share the template between multiple stories,
you can define the snippet at the root and pass it in as the `children` **prop** to the `<Story>` component.
Example:
```svelte
{#snippet template(args: Args<typeof Story>)}
<SomeComponent {...args}>
My custom template to reuse across several stories
</SomeComponent>
{/snippet}
<Story name="Explicit snippet" children={template} />
```
-->
<Story
name="Shared template"
children={sharedTemplate}
args={{
text: 'This story uses a shared snippet, which is explicitly set as the `children` prop to the <Story> component',
}}
play={async (context) => {
const { args, canvasElement } = context;
const canvas = within(canvasElement);
const h2 = await canvas.findByTestId('heading');
const p = await canvas.findByText(args.text);
expect(h2).toBeInTheDocument();
expect(p).toBeInTheDocument();
}}
/>
<!--
To set a default template for all stories in the file, call the **`setTemplate()`** function with a reference to a root snippet.
Any story without `children` will use this default template.
Example:
```svelte
<script>
import { setTemplate, type Args } from '@storybook/addon-svelte-csf';
</script>
<script>
setTemplate(defaultTemplate);
</script>
{#snippet defaultTemplate(args: Args<typeof Story>)}
<SomeComponent {...args}>
A default template to be used in <Story> components which doesn't have an explicit template set
</SomeComponent>
{/snippet}
<Story name="Default" />
```
-->
<Story
name="setTemplate"
args={{
text: 'This story is based on the snippet passed to the setTemplate() function',
}}
play={async (context) => {
const { args, canvasElement } = context;
const canvas = within(canvasElement);
const h2 = await canvas.findByTestId('heading');
const p = await canvas.findByText(args.text);
expect(h2).toBeInTheDocument();
expect(p).toBeInTheDocument();
}}
/>