storybook/docs/snippets/web-components/storybook-interactions-play-function.ts.mdx
2023-09-21 18:56:06 +02:00

41 lines
1.1 KiB
Plaintext

```ts
// Form.stories.ts
import type { Meta, StoryObj } from '@storybook/web-components';
import { userEvent, waitFor, within, expect, fn } from '@storybook/test';
const meta: Meta = {
component: 'my-form-element',
args: {
// transform the arg into a spy
onSubmit: fn(),
},
};
export default meta;
type Story = StoryObj;
/*
* See https://storybook.js.org/docs/web-components/writing-stories/play-function#working-with-the-canvas
* to learn more about using the canvasElement to query the DOM
*/
export const Submitted: Story = {
play: async ({ args, canvasElement, step }) => {
const canvas = within(canvasElement);
await step('Enter credentials', async () => {
await userEvent.type(canvas.getByTestId('email'), 'hi@example.com');
await userEvent.type(canvas.getByTestId('password'), 'supersecret');
});
await step('Submit form', async () => {
await userEvent.click(canvas.getByRole('button'));
});
// onSubmit is a spy so it can be asserted in various ways
await waitFor(() => expect(args.onSubmit).toHaveBeenCalled());
},
};
```