storybook/docs/snippets/web-components/storybook-interactions-play-function.js.mdx
2023-11-28 10:58:12 +01:00

36 lines
981 B
Plaintext

```js
// Form.stories.js
import { userEvent, waitFor, within, expect, fn } from '@storybook/test';
export default {
component: 'my-form-element',
args: {
// 👇 Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
};
/*
* 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 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'));
});
// 👇 Now we can assert that the onSubmit arg was called
await waitFor(() => expect(args.onSubmit).toHaveBeenCalled());
},
};
```