mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-08 09:22:09 +08:00
- For these pages (so far): - /docs/get-started/whats-a-story - /docs/get-started/setup - /docs/writing-stories/introduction - /docs/writing-stories/args - /docs/writing-stories/parameters - /docs/writing-stories/decorators - /docs/writing-stories/play-function - /docs/writing-stories/loaders - /docs/writing-stories/build-pages-in-storybook - /docs/writing-stories/stories-for-multiple-components - /docs/api/csf - Convert some snippets to "common" ones - Remove unused snippets
39 lines
1.0 KiB
Plaintext
39 lines
1.0 KiB
Plaintext
```tsx
|
|
// MyComponent.stories.ts
|
|
import { userEvent, within } from '@storybook/testing-library';
|
|
|
|
import type { Meta, StoryObj } from '@storybook/web-components';
|
|
|
|
const meta: Meta = {
|
|
title: 'WithSelectEvent',
|
|
component: 'demo-my-component',
|
|
};
|
|
export default meta;
|
|
|
|
type Story = StoryObj;
|
|
|
|
// Function to emulate pausing between interactions
|
|
function sleep(ms: number) {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
}
|
|
|
|
/* See https://storybook.js.org/docs/7.0/react/writing-stories/play-function#working-with-the-canvas
|
|
* to learn more about using the canvasElement to query the DOM
|
|
*/
|
|
export const ExampleChangeEvent: Story = {
|
|
play: async ({ canvasElement }) => {
|
|
const canvas = within(canvasElement);
|
|
|
|
const select = canvas.getByRole('listbox');
|
|
|
|
await userEvent.selectOptions(select, ['One Item']);
|
|
await sleep(2000);
|
|
|
|
await userEvent.selectOptions(select, ['Another Item']);
|
|
await sleep(2000);
|
|
|
|
await userEvent.selectOptions(select, ['Yet another item']);
|
|
},
|
|
};
|
|
```
|