-
-
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.
Merge pull request #28444 from storybookjs/kasper/mount-docs
Docs: Add docs for mounting inside the play function
- Loading branch information
Showing
7 changed files
with
352 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
```js filename=".storybook/preview.js" renderer="common" language="js" | ||
import { init } from '../project-bootstrap'; | ||
|
||
export default { | ||
async beforeAll() { | ||
await init(); | ||
}, | ||
}; | ||
``` | ||
|
||
```ts filename=".storybook/preview.ts" renderer="common" language="ts" | ||
// Replace your-renderer with the renderer you are using (e.g., react, vue3, angular, etc.) | ||
import { Preview } from '@storybook/your-renderer'; | ||
|
||
import { init } from '../project-bootstrap'; | ||
|
||
const preview: Preview = { | ||
async beforeAll() { | ||
await init(); | ||
}, | ||
}; | ||
|
||
export default preview; | ||
``` |
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,24 @@ | ||
```js filename=".storybook/preview.js" renderer="common" language="js" | ||
import MockDate from 'mockdate'; | ||
|
||
export default { | ||
async beforeEach() { | ||
MockDate.reset() | ||
} | ||
}; | ||
``` | ||
|
||
```ts filename=".storybook/preview.ts" renderer="common" language="ts" | ||
// Replace your-renderer with the renderer you are using (e.g., react, vue3, angular, etc.) | ||
import { Preview } from '@storybook/your-renderer'; | ||
import MockDate from 'mockdate'; | ||
|
||
const preview: Preview = { | ||
async beforeEach() { | ||
MockDate.reset() | ||
} | ||
}; | ||
|
||
export default preview; | ||
``` | ||
|
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,135 @@ | ||
```tsx filename="Page.stories.tsx" renderer="react" language="ts" | ||
export const Default: Story = { | ||
play: async ({ mount, args }) => { | ||
const note = await db.note.create({ | ||
data: { title: 'Mount inside of play' }, | ||
}); | ||
|
||
const canvas = await mount( | ||
// 👇 Pass data that is created inside of the play function to the component | ||
// For example, a just-generated UUID | ||
<Page {...args} params={{ id: String(note.id) }} /> | ||
); | ||
|
||
await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i })); | ||
}, | ||
argTypes: { | ||
// 👇 Make the params prop un-controllable, as the value is always overriden in the play function. | ||
params: { control: { disable: true } }, | ||
} | ||
}; | ||
``` | ||
|
||
```jsx filename="Page.stories.jsx" renderer="react" language="js" | ||
export const Default = { | ||
play: async ({ mount, args }) => { | ||
const note = await db.note.create({ | ||
data: { title: 'Mount inside of play' }, | ||
}); | ||
|
||
const canvas = await mount( | ||
// 👇 Pass data that is created inside of the play function to the component | ||
// For example, a just-generated UUID | ||
<Page {...args} params={{ id: String(note.id) }} /> | ||
); | ||
|
||
await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i })); | ||
}, | ||
argTypes: { | ||
// 👇 Make the params prop un-controllable, as the value is always overriden in the play function. | ||
params: { control: { disable: true } }, | ||
} | ||
}; | ||
``` | ||
|
||
```ts filename="Page.stories.ts" renderer="svelte" language="ts" | ||
export const Default: Story = { | ||
play: async ({ mount, args }) => { | ||
const note = await db.note.create({ | ||
data: { title: 'Mount inside of play' }, | ||
}); | ||
|
||
const canvas = await mount( | ||
Page, | ||
// 👇 Pass data that is created inside of the play function to the component | ||
// For example, a just-generated UUID | ||
{ props: { ...args, params: { id: String(note.id) } } } | ||
); | ||
|
||
await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i })); | ||
}, | ||
argTypes: { | ||
// 👇 Make the params prop un-controllable, as the value is always overriden in the play function. | ||
params: { control: { disable: true } }, | ||
} | ||
}; | ||
``` | ||
|
||
```js filename="Page.stories.js" renderer="svelte" language="js" | ||
export const Default = { | ||
play: async ({ mount, args }) => { | ||
const note = await db.note.create({ | ||
data: { title: 'Mount inside of play' }, | ||
}); | ||
|
||
const canvas = await mount( | ||
Page, | ||
// 👇 Pass data that is created inside of the play function to the component | ||
// For example, a just-generated UUID | ||
{ props: { ...args, params: { id: String(note.id) } } } | ||
); | ||
|
||
await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i })); | ||
}, | ||
argTypes: { | ||
// 👇 Make the params prop un-controllable, as the value is always overriden in the play function. | ||
params: { control: { disable: true } }, | ||
} | ||
}; | ||
``` | ||
|
||
```ts filename="Page.stories.ts" renderer="vue3" language="ts" | ||
export const Default: Story = { | ||
play: async ({ mount, args }) => { | ||
const note = await db.note.create({ | ||
data: { title: 'Mount inside of play' }, | ||
}); | ||
|
||
const canvas = await mount( | ||
Page, | ||
// 👇 Pass data that is created inside of the play function to the component | ||
// For example, a just-generated UUID | ||
{ props: { ...args, params: { id: String(note.id) } } } | ||
); | ||
|
||
await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i })); | ||
}, | ||
argTypes: { | ||
// 👇 Make the params prop un-controllable, as the value is always overriden in the play function. | ||
params: { control: { disable: true } }, | ||
} | ||
}; | ||
``` | ||
|
||
```js filename="Page.stories.js" renderer="vue3" language="js" | ||
export const Default = { | ||
play: async ({ mount, args }) => { | ||
const note = await db.note.create({ | ||
data: { title: 'Mount inside of play' }, | ||
}); | ||
|
||
const canvas = await mount( | ||
Page, | ||
// 👇 Pass data that is created inside of the play function to the component | ||
// For example, a just-generated UUID | ||
{ props: { ...args, params: { id: String(note.id) } } } | ||
); | ||
|
||
await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i })); | ||
}, | ||
argTypes: { | ||
// 👇 Make the params prop un-controllable, as the value is always overriden in the play function. | ||
params: { control: { disable: true } }, | ||
} | ||
}; | ||
``` |
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,44 @@ | ||
```js filename="Page.stories.js" renderer="common" language="js" | ||
import MockDate from 'mockdate'; | ||
|
||
// ...rest of story file | ||
|
||
export const ChristmasUI = { | ||
async play({ mount }) { | ||
MockDate.set('2024-12-25'); | ||
// 👇 Render the component with the mocked date | ||
await mount(); | ||
// ...rest of test | ||
}, | ||
}; | ||
``` | ||
|
||
```ts filename="Page.stories.ts" renderer="common" language="ts-4-9" | ||
import MockDate from 'mockdate'; | ||
|
||
// ...rest of story file | ||
|
||
export const ChristmasUI: Story = { | ||
async play({ mount }) { | ||
MockDate.set('2024-12-25'); | ||
// 👇 Render the component with the mocked date | ||
await mount(); | ||
// ...rest of test | ||
}, | ||
}; | ||
``` | ||
|
||
```ts filename="Page.stories.ts" renderer="common" language="ts" | ||
import MockDate from 'mockdate'; | ||
|
||
// ...rest of story file | ||
|
||
export const ChristmasUI: Story = { | ||
async play({ mount }) { | ||
MockDate.set('2024-12-25'); | ||
// 👇 Render the component with the mocked date | ||
await mount(); | ||
// ...rest of test | ||
}, | ||
}; | ||
``` |
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.