Skip to content

Commit 6b89b89

Browse files
authored
Merge pull request #129 from storybookjs/valentin/improve-storybook-story-instructions
Add play function parameters section to storybook instructions
2 parents b1f195f + afff6e0 commit 6b89b89

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

packages/addon-mcp/src/storybook-story-instructions.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,34 @@ export const LoggedIn: Story = {
110110

111111
Before doing this ensure you have mocked the import in the preview file.
112112

113+
### Play Function Parameters
114+
115+
- The play function has a `canvas` parameter that can be used directly with testing-library-like query methods.
116+
- It also has a `canvasElement` which is the actual DOM element.
117+
- The `within`-function imported from `storybook/test` transforms a DOM element to an object with query methods, similar to `canvas`.
118+
119+
**DO NOT** use `within(canvas)` - it is redundant because `canvas` already has the query methods, `canvas` is not a DOM element.
120+
121+
```ts
122+
// ✅ Correct: Use canvas directly
123+
play: async ({ canvas }) => {
124+
await canvas.getByLabelText('Submit').click();
125+
};
126+
127+
// ⚠️ Also acceptable: Use `canvasElement` with `within`
128+
import { within } from 'storybook/test';
129+
130+
play: async ({ canvasElement }) => {
131+
const canvas = within(canvasElement);
132+
await canvas.getByLabelText('Submit').click();
133+
};
134+
135+
// ❌ Wrong: Do NOT use within(canvas)
136+
play: async ({ canvas }) => {
137+
const screen = within(canvas); // Error!
138+
};
139+
```
140+
113141
### Key Requirements
114142

115143
- **Node.js 20+**, **TypeScript 4.9+**, **Vite 5+**

0 commit comments

Comments
 (0)