storybook/docs/snippets/common/my-component-play-function-with-canvas.ts.mdx
Kyle Gach b97d94e005 Address feedback
- Newline following filename comment
- Fix URLs in comments
- Ensure consistent usage of `ts` vs `tsx` language
2023-01-12 14:17:39 -07:00

30 lines
820 B
Plaintext

```ts
// MyComponent.stories.ts|tsx
import { userEvent, within } from '@storybook/testing-library';
// Replace your-framework with the name of your framework
import type { Meta, StoryObj } from '@storybook/your-framework';
import { MyComponent } from './MyComponent';
const meta: Meta<typeof MyComponent> = {
title: 'WithCanvasElement',
component: MyComponent,
};
export default meta;
type Story = StoryObj<typeof MyComponent>;
export const ExampleStory: Story = {
play: async ({ canvasElement }) => {
// Assigns canvas to the component root element
const canvas = within(canvasElement);
// Starts querying from the component's root element
await userEvent.type(canvas.getByTestId('example-element'), 'something');
await userEvent.click(canvas.getByRole('another-element'));
},
};
```