storybook/docs/snippets/common/my-component-play-function-waitfor.ts.mdx
2023-11-28 10:58:12 +01:00

43 lines
1.2 KiB
Plaintext

```ts
// MyComponent.stories.ts|tsx
// Replace your-framework with the name of your framework
import type { Meta, StoryObj } from '@storybook/your-framework';
import { userEvent, waitFor, within } from '@storybook/test';
import { MyComponent } from './MyComponent';
const meta: Meta<typeof MyComponent> = {
component: MyComponent,
};
export default meta;
type Story = StoryObj<typeof MyComponent>;
/* See https://storybook.js.org/docs/writing-stories/play-function#working-with-the-canvas
* to learn more about using the canvasElement to query the DOM
*/
export const ExampleAsyncStory: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const Input = canvas.getByLabelText('Username', {
selector: 'input',
});
await userEvent.type(Input, 'WrongInput', {
delay: 100,
});
// See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
const Submit = canvas.getByRole('button');
await userEvent.click(Submit);
await waitFor(async () => {
await userEvent.hover(canvas.getByTestId('error'));
});
},
};
```