docs/_snippets/interaction-test-fn-mock-spy.md
import type { Meta, StoryObj } from '@storybook/angular';
import { fn, expect } from 'storybook/test';
import { LoginForm } from './login-form.component';
const meta: Meta<LoginForm> = {
component: LoginForm,
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
};
export default meta;
type Story = StoryObj<LoginForm>;
export const FilledForm: Story = {
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
};
import { fn, expect } from 'storybook/test';
import preview from '../.storybook/preview';
import { LoginForm } from './login-form.component';
const meta = preview.meta({
component: LoginForm,
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
});
export const FilledForm = meta.story({
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
});
// Replace your-framework with the name of your framework (e.g. react-vite, vue3-vite, etc.)
import type { Meta, StoryObj } from '@storybook/your-framework';
import { fn, expect } from 'storybook/test';
import { LoginForm } from './LoginForm';
const meta = {
component: LoginForm,
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
} satisfies Meta<typeof LoginForm>;
export default meta;
type Story = StoryObj<typeof meta>;
export const FilledForm: Story = {
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
};
import { fn, expect } from 'storybook/test';
import { LoginForm } from './LoginForm';
export default {
component: LoginForm,
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
};
export const FilledForm = {
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
};
<script module>
import { defineMeta } from '@storybook/addon-svelte-csf';
import LoginForm from './LoginForm.svelte';
const { Story } = defineMeta({
component: LoginForm,
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
});
</script>
<Story
name="FilledForm"
play={async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
}}
/>
// Replace your-framework with the framework you are using, e.g. sveltekit or svelte-vite
import type { Meta, StoryObj } from '@storybook/your-framework';
import { fn, expect } from 'storybook/test';
import LoginForm from './LoginForm.svelte';
const meta = {
component: LoginForm,
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
} satisfies Meta<typeof LoginForm>;
export default meta;
type Story = StoryObj<typeof meta>;
export const FilledForm: Story = {
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
};
<script module>
import { defineMeta } from '@storybook/addon-svelte-csf';
import LoginForm from './LoginForm.svelte';
const { Story } = defineMeta({
component: LoginForm,
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
});
</script>
<Story
name="FilledForm"
play={async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
}}
/>
import { fn, expect } from 'storybook/test';
import LoginForm from './LoginForm.svelte';
export default {
component: LoginForm,
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
};
export const FilledForm = {
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
};
import type { Meta, StoryObj } from '@storybook/web-components-vite';
import { fn, expect } from 'storybook/test';
const meta: Meta = {
component: 'demo-login-form',
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
};
export default meta;
type Story = StoryObj;
export const FilledForm: Story = {
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
};
import { fn, expect } from 'storybook/test';
export default {
component: 'demo-login-form',
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
};
export const FilledForm = {
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
};
import { fn, expect } from 'storybook/test';
import preview from '../.storybook/preview';
const meta = preview.meta({
component: 'demo-login-form',
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
});
export const FilledForm = meta.story({
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
});
import { fn, expect } from 'storybook/test';
import preview from '../.storybook/preview';
const meta = preview.meta({
component: 'demo-login-form',
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
});
export const FilledForm = meta.story({
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
});
import { fn, expect } from 'storybook/test';
import preview from '../.storybook/preview';
import { LoginForm } from './LoginForm';
const meta = preview.meta({
component: LoginForm,
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
});
export const FilledForm = meta.story({
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
});
import { fn, expect } from 'storybook/test';
import preview from '../.storybook/preview';
import { LoginForm } from './LoginForm';
const meta = preview.meta({
component: LoginForm,
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
});
export const FilledForm = meta.story({
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
});
import { fn, expect } from 'storybook/test';
import preview from '../.storybook/preview';
import LoginForm from './LoginForm.vue';
const meta = preview.meta({
component: LoginForm,
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
});
export const FilledForm = meta.story({
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
});
import { fn, expect } from 'storybook/test';
import preview from '../.storybook/preview';
import LoginForm from './LoginForm.vue';
const meta = preview.meta({
component: LoginForm,
args: {
// ๐ Use `fn` to spy on the onSubmit arg
onSubmit: fn(),
},
});
export const FilledForm = meta.story({
play: async ({ args, canvas, userEvent }) => {
await userEvent.type(canvas.getByLabelText('Email'), '[email protected]');
await userEvent.type(canvas.getByLabelText('Password'), 'a-random-password');
await userEvent.click(canvas.getByRole('button', { name: 'Log in' }));
// ๐ Now we can assert that the onSubmit arg was called
await expect(args.onSubmit).toHaveBeenCalled();
},
});