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

36 lines
984 B
Plaintext

```js
// Form.stories.js
import { userEvent, waitFor, within, expect, fn } from '@storybook/test';
export default {
component: 'my-form-element',
args: {
// transform the arg into a spy
onSubmit: fn(),
},
};
/*
* 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 = {
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());
},
};
```