storybook/docs/snippets/web-components/storybook-test-fn-mock-spy.ts.mdx
2024-04-30 22:21:31 -06:00

36 lines
887 B
Plaintext

```ts
// NoteUI.stories.ts
import { Meta, StoryObj } from '@storybook/react';
import { expect, userEvent, within } from '@storybook/test';
// 👇 Must use this import path to have mocks typed correctly
import { saveNote } from '#app/actions.mock';
import { createNotes } from '#mocks/notes';
const meta: Meta = {
title: 'Mocked/NoteUI',
component: 'note-ui',
};
export default meta;
type Story = StoryObj;
const notes = createNotes();
export const SaveFlow: Story = {
name: 'Save Flow ▶',
args: {
isEditing: true,
note: notes[0],
},
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement);
const saveButton = canvas.getByRole('menuitem', { name: /done/i });
await userEvent.click(saveButton);
// 👇 This is the mock function, so you can assert its behavior
await expect(saveNote).toHaveBeenCalled();
},
};
```