mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-07 07:21:17 +08:00
39 lines
1.0 KiB
Plaintext
39 lines
1.0 KiB
Plaintext
```js
|
|
// Form.stories.js|jsx
|
|
|
|
import { userEvent, waitFor, within, expect, fn } from '@storybook/test';
|
|
|
|
import { Form } from './Form';
|
|
|
|
export default {
|
|
component: Form,
|
|
args: {
|
|
// transform the arg into a spy
|
|
onSubmit: fn(),
|
|
},
|
|
};
|
|
|
|
/*
|
|
* See https://storybook.js.org/docs/react/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 }) => {
|
|
// Starts querying the component from its root element
|
|
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());
|
|
},
|
|
};
|
|
```
|