storybook/docs/snippets/svelte/my-component-play-function-with-selectevent.js.mdx
2022-11-17 18:50:59 +00:00

46 lines
1.2 KiB
Plaintext

```js
// MyComponent.stories.js
import { userEvent, within } from '@storybook/testing-library';
import MyComponent from './MyComponent.svelte';
export default {
/* 👇 The title prop is optional.
* See https://storybook.js.org/docs/7.0/svelte/configure/overview#configure-story-loading
* to learn how to generate automatic titles
*/
title: 'WithSelectEvent',
component: MyComponent,
};
// Function to emulate pausing between interactions
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/*
* See https://storybook.js.org/docs/7.0/svelte/writing-stories/play-function#working-with-the-canvas
* to learn more about using the canvasElement to query the DOM
*/
export const ExampleChangeEvent = {
render: (args) => ({
Component: MyComponent,
props: args,
}),
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']);
},
};
```