Back to Storybook

Login Form With Play Function

docs/_snippets/login-form-with-play-function.md

10.3.621.4 KB
Original Source
ts
import type { Meta, StoryObj } from '@storybook/angular';

import { expect } from 'storybook/test';

import { LoginForm } from './login-form.component';

const meta: Meta<LoginForm> = {
  component: LoginForm,
};
export default meta;

type Story = StoryObj<LoginForm>;

export const EmptyForm: Story = {};

export const FilledForm: Story = {
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
};
ts
import { expect } from 'storybook/test';

import preview from '../.storybook/preview';

import { LoginForm } from './login-form.component';

const meta = preview.meta({
  component: LoginForm,
});

export const EmptyForm = meta.story();

export const FilledForm = meta.story({
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
});
js
import { expect } from 'storybook/test';

import { LoginForm } from './LoginForm';

export default {
  component: LoginForm,
};

export const EmptyForm = {};

export const FilledForm = {
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
};
ts
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, nextjs-vite, etc.
import type { Meta, StoryObj } from '@storybook/your-framework';

import { expect } from 'storybook/test';

import { LoginForm } from './LoginForm';

const meta = {
  component: LoginForm,
} satisfies Meta<typeof LoginForm>;
export default meta;

type Story = StoryObj<typeof meta>;

export const EmptyForm: Story = {};

export const FilledForm: Story = {
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
};
js
import { expect } from 'storybook/test';

import { LoginForm } from './LoginForm';

export default {
  component: LoginForm,
};

export const EmptyForm = {};

export const FilledForm = {
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
};
tsx
import type { Meta, StoryObj } from 'storybook-solidjs-vite';

import { expect } from 'storybook/test';

import { LoginForm } from './LoginForm';

const meta = {
  component: LoginForm,
} satisfies Meta<typeof LoginForm>;
export default meta;

type Story = StoryObj<typeof meta>;

export const EmptyForm: Story = {};

export const FilledForm: Story = {
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
};
svelte
<script module>
  import { defineMeta } from '@storybook/addon-svelte-csf';

  import { expect, within } from 'storybook/test';

  import LoginForm from './LoginForm.svelte';

  const { Story } = defineMeta({
    component: LoginForm,
  });
</script>

<Story name="EmptyForm" />

<!--
  See https://storybook.js.org/docs/writing-stories/play-function#working-with-the-canvas
  to learn more about using the canvas to query the DOM
-->
<Story
  name="FilledForm"
  play={async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!'
      )
    ).toBeInTheDocument();
  }}
/>
js
import { expect, within } from 'storybook/test';

import LoginForm from './LoginForm.svelte';

export default {
  component: LoginForm,
};

export const EmptyForm = {};

export const FilledForm = {
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
};
svelte
<script module>
  import { defineMeta } from '@storybook/addon-svelte-csf';

  import { expect, within } from 'storybook/test';

  import LoginForm from './LoginForm.svelte';

  const { Story } = defineMeta({
    component: LoginForm,
  });
</script>

<Story name="EmptyForm" />

<!--
  See https://storybook.js.org/docs/writing-stories/play-function#working-with-the-canvas
  to learn more about using the canvas to query the DOM
-->
<Story
  name="FilledForm"
  play={async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!'
      )
    ).toBeInTheDocument();
  }}
/>
ts
// Replace your-framework with svelte-vite or sveltekit
import type { Meta, StoryObj } from '@storybook/your-framework';

import { expect, within } from 'storybook/test';

import LoginForm from './LoginForm.svelte';

const meta = {
  component: LoginForm,
} satisfies Meta<typeof LoginForm>;
export default meta;

type Story = StoryObj<typeof meta>;

export const EmptyForm: Story = {};

export const FilledForm: Story = {
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
};
js
import { expect } from 'storybook/test';

import LoginForm from './LoginForm.vue';

export default {
  component: LoginForm,
};

export const EmptyForm = {
  render: () => ({
    components: { LoginForm },
    template: `<LoginForm />`,
  }),
};

export const FilledForm = {
  render: () => ({
    components: { LoginForm },
    template: `<LoginForm />`,
  }),
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
};
ts
import type { Meta, StoryObj } from '@storybook/vue3-vite';

import { expect } from 'storybook/test';

import LoginForm from './LoginForm.vue';

const meta = {
  component: LoginForm,
} satisfies Meta<typeof LoginForm>;
export default meta;

type Story = StoryObj<typeof meta>;

export const EmptyForm: Story = {
  render: () => ({
    components: { LoginForm },
    template: `<LoginForm />`,
  }),
};

export const FilledForm: Story = {
  render: () => ({
    components: { LoginForm },
    template: `<LoginForm />`,
  }),
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
};
ts
import { expect } from 'storybook/test';

import preview from '../.storybook/preview';

import LoginForm from './LoginForm.vue';

const meta = preview.meta({
  component: LoginForm,
});

export const EmptyForm = meta.story({
  render: () => ({
    components: { LoginForm },
    template: `<LoginForm />`,
  }),
});

export const FilledForm = meta.story({
  render: () => ({
    components: { LoginForm },
    template: `<LoginForm />`,
  }),
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
});
<!-- JS snippets still needed while providing both CSF 3 & Next -->
js
import { expect } from 'storybook/test';

import preview from '../.storybook/preview';

import LoginForm from './LoginForm.vue';

const meta = preview.meta({
  component: LoginForm,
});

export const EmptyForm = meta.story({
  render: () => ({
    components: { LoginForm },
    template: `<LoginForm />`,
  }),
});

export const FilledForm = meta.story({
  render: () => ({
    components: { LoginForm },
    template: `<LoginForm />`,
  }),
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
});
js
import { expect } from 'storybook/test';

export default {
  component: 'demo-login-form',
};

export const EmptyForm = {};

export const FilledForm = {
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
};
ts
import type { Meta, StoryObj } from '@storybook/web-components-vite';

import { expect } from 'storybook/test';

const meta: Meta = {
  component: 'demo-login-form',
};
export default meta;

type Story = StoryObj;

export const EmptyForm: Story = {};

export const FilledForm: Story = {
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
};
js
import { expect } from 'storybook/test';

import preview from '../.storybook/preview';

const meta = preview.meta({
  component: 'demo-login-form',
});

export const EmptyForm = meta.story();

export const FilledForm = meta.story({
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
});
ts
import { expect } from 'storybook/test';

import preview from '../.storybook/preview';

const meta = preview.meta({
  component: 'demo-login-form',
});

export const EmptyForm = meta.story();

export const FilledForm = meta.story({
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
});
ts
import { expect } from 'storybook/test';

import preview from '../.storybook/preview';

import { LoginForm } from './LoginForm';

const meta = preview.meta({
  component: LoginForm,
});

export const EmptyForm = meta.story();

export const FilledForm = meta.story({
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
});
<!-- JS snippets still needed while providing both CSF 3 & Next -->
js
import { expect } from 'storybook/test';

import preview from '../.storybook/preview';

import { LoginForm } from './LoginForm';

const meta = preview.meta({
  component: LoginForm,
});

export const EmptyForm = meta.story();

export const FilledForm = meta.story({
  play: async ({ canvas, userEvent }) => {
    // ๐Ÿ‘‡ Simulate interactions with the component
    await userEvent.type(canvas.getByTestId('email'), '[email protected]');

    await userEvent.type(canvas.getByTestId('password'), 'a-random-password');

    // See https://storybook.js.org/docs/essentials/actions#automatically-matching-args to learn how to setup logging in the Actions panel
    await userEvent.click(canvas.getByRole('button'));

    // ๐Ÿ‘‡ Assert DOM structure
    await expect(
      canvas.getByText(
        'Everything is perfect. Your account is ready and we should probably get you started!',
      ),
    ).toBeInTheDocument();
  },
});