Back to Storybook

Mount Advanced

docs/_snippets/mount-advanced.md

10.3.612.1 KB
Original Source
tsx
// 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';

// ๐Ÿ‘‡ Automocked module resolves to '../lib/__mocks__/db'
import db from '../lib/db';
import { Page } from './Page';

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

type Story = StoryObj<typeof meta>;

export const Basic: Story = {
  play: async ({ mount, args, userEvent }) => {
    const note = await db.note.create({
      data: { title: 'Mount inside of play' },
    });

    const canvas = await mount(
      // ๐Ÿ‘‡ Pass data that is created inside of the play function to the component
      //   For example, a just-generated UUID
      <Page {...args} params={{ id: String(note.id) }} />,
    );

    await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i }));
  },
  argTypes: {
    // ๐Ÿ‘‡ Make the params prop un-controllable, as the value is always overridden in the play function.
    params: { control: { disable: true } },
  },
};
jsx
// ๐Ÿ‘‡ Automocked module resolves to '../lib/__mocks__/db'
import db from '../lib/db';
import { Page } from './Page';

export default { component: Page };

export const Basic = {
  play: async ({ mount, args, userEvent }) => {
    const note = await db.note.create({
      data: { title: 'Mount inside of play' },
    });

    const canvas = await mount(
      // ๐Ÿ‘‡ Pass data that is created inside of the play function to the component
      //   For example, a just-generated UUID
      <Page {...args} params={{ id: String(note.id) }} />,
    );

    await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i }));
  },
  argTypes: {
    // ๐Ÿ‘‡ Make the params prop un-controllable, as the value is always overridden in the play function.
    params: { control: { disable: true } },
  },
};
tsx
import type { Meta, StoryObj } from '@storybook/angular';

// ๐Ÿ‘‡ Automocked module resolves to '../lib/__mocks__/db'
import db from '../lib/db';
import { Page } from './page.component';

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

type Story = StoryObj<typeof meta>;

export const Basic: Story = {
  play: async ({ mount, args, userEvent }) => {
    const note = await db.note.create({
      data: { title: 'Mount inside of play' },
    });

    const canvas = await mount(
      Page,
      // ๐Ÿ‘‡ Pass data that is created inside of the play function to the component
      //   For example, a just-generated UUID
      { props: { ...args, params: { id: String(note.id) } } },
    );

    await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i }));
  },
  argTypes: {
    // ๐Ÿ‘‡ Make the params prop un-controllable, as the value is always overridden in the play function.
    params: { control: { disable: true } },
  },
};
tsx
import preview from '../.storybook/preview';

// ๐Ÿ‘‡ Automocked module resolves to '../lib/__mocks__/db'
import db from '../lib/db';
import { Page } from './page.component';

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

export const Basic = meta.story({
  play: async ({ mount, args, userEvent }) => {
    const note = await db.note.create({
      data: { title: 'Mount inside of play' },
    });

    const canvas = await mount(
      Page,
      // ๐Ÿ‘‡ Pass data that is created inside of the play function to the component
      //   For example, a just-generated UUID
      { props: { ...args, params: { id: String(note.id) } } },
    );

    await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i }));
  },
  argTypes: {
    // ๐Ÿ‘‡ Make the params prop un-controllable, as the value is always overridden in the play function.
    params: { control: { disable: true } },
  },
});
ts
// Replace your-framework with the framework you are using, e.g., svelte-vite, sveltekit, etc.
import type { Meta, StoryObj } from '@storybook/your-framework';

// ๐Ÿ‘‡ Automocked module resolves to '../lib/__mocks__/db'
import db from '../lib/db';
import Page from './Page.svelte';

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

type Story = StoryObj<typeof meta>;

export const Basic: Story = {
  play: async ({ mount, args, userEvent }) => {
    const note = await db.note.create({
      data: { title: 'Mount inside of play' },
    });

    const canvas = await mount(
      Page,
      // ๐Ÿ‘‡ Pass data that is created inside of the play function to the component
      //   For example, a just-generated UUID
      { props: { ...args, params: { id: String(note.id) } } },
    );

    await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i }));
  },
  argTypes: {
    // ๐Ÿ‘‡ Make the params prop un-controllable, as the value is always overridden in the play function.
    params: { control: { disable: true } },
  },
};
js
// ๐Ÿ‘‡ Automocked module resolves to '../lib/__mocks__/db'
import db from '../lib/db';
import Page from './Page.svelte';

export default { component: Page };

export const Basic = {
  play: async ({ mount, args, userEvent }) => {
    const note = await db.note.create({
      data: { title: 'Mount inside of play' },
    });

    const canvas = await mount(
      Page,
      // ๐Ÿ‘‡ Pass data that is created inside of the play function to the component
      //   For example, a just-generated UUID
      { props: { ...args, params: { id: String(note.id) } } },
    );

    await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i }));
  },
  argTypes: {
    // ๐Ÿ‘‡ Make the params prop un-controllable, as the value is always overridden in the play function.
    params: { control: { disable: true } },
  },
};
ts
import type { Meta, StoryObj } from '@storybook/vue3-vite';

// ๐Ÿ‘‡ Automocked module resolves to '../lib/__mocks__/db'
import db from '../lib/db';
import Page from './Page.vue';

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

type Story = StoryObj<typeof meta>;

export const Basic: Story = {
  play: async ({ mount, args, userEvent }) => {
    const note = await db.note.create({
      data: { title: 'Mount inside of play' },
    });

    const canvas = await mount(
      Page,
      // ๐Ÿ‘‡ Pass data that is created inside of the play function to the component
      //   For example, a just-generated UUID
      { props: { ...args, params: { id: String(note.id) } } },
    );

    await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i }));
  },
  argTypes: {
    // ๐Ÿ‘‡ Make the params prop un-controllable, as the value is always overridden in the play function.
    params: { control: { disable: true } },
  },
};
js
// ๐Ÿ‘‡ Automocked module resolves to '../lib/__mocks__/db'
import db from '../lib/db';
import Page from './Page.vue';

export default { component: Page };

export const Basic = {
  play: async ({ mount, args, userEvent }) => {
    const note = await db.note.create({
      data: { title: 'Mount inside of play' },
    });

    const canvas = await mount(
      Page,
      // ๐Ÿ‘‡ Pass data that is created inside of the play function to the component
      //   For example, a just-generated UUID
      { props: { ...args, params: { id: String(note.id) } } },
    );

    await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i }));
  },
  argTypes: {
    // ๐Ÿ‘‡ Make the params prop un-controllable, as the value is always overridden in the play function.
    params: { control: { disable: true } },
  },
};
tsx
import preview from '../.storybook/preview';

// ๐Ÿ‘‡ Automocked module resolves to '../lib/__mocks__/db'
import db from '../lib/db';
import { Page } from './Page';

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

export const Basic = meta.story({
  play: async ({ mount, args, userEvent }) => {
    const note = await db.note.create({
      data: { title: 'Mount inside of play' },
    });

    const canvas = await mount(
      // ๐Ÿ‘‡ Pass data that is created inside of the play function to the component
      //   For example, a just-generated UUID
      <Page {...args} params={{ id: String(note.id) }} />,
    );

    await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i }));
  },
  argTypes: {
    // ๐Ÿ‘‡ Make the params prop un-controllable, as the value is always overridden in the play function.
    params: { control: { disable: true } },
  },
});
<!-- JS snippets still needed while providing both CSF 3 & Next -->
jsx
import preview from '../.storybook/preview';
// ๐Ÿ‘‡ Automocked module resolves to '../lib/__mocks__/db'
import db from '../lib/db';
import { Page } from './Page';

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

export const Basic = meta.story({
  play: async ({ mount, args, userEvent }) => {
    const note = await db.note.create({
      data: { title: 'Mount inside of play' },
    });

    const canvas = await mount(
      // ๐Ÿ‘‡ Pass data that is created inside of the play function to the component
      //   For example, a just-generated UUID
      <Page {...args} params={{ id: String(note.id) }} />,
    );

    await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i }));
  },
  argTypes: {
    // ๐Ÿ‘‡ Make the params prop un-controllable, as the value is always overridden in the play function.
    params: { control: { disable: true } },
  },
});
ts
import preview from '../.storybook/preview';

// ๐Ÿ‘‡ Automocked module resolves to '../lib/__mocks__/db'
import db from '../lib/db';
import Page from './Page.vue';

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

export const Basic = meta.story({
  play: async ({ mount, args, userEvent }) => {
    const note = await db.note.create({
      data: { title: 'Mount inside of play' },
    });

    const canvas = await mount(
      Page,
      // ๐Ÿ‘‡ Pass data that is created inside of the play function to the component
      //   For example, a just-generated UUID
      { props: { ...args, params: { id: String(note.id) } } },
    );

    await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i }));
  },
  argTypes: {
    // ๐Ÿ‘‡ Make the params prop un-controllable, as the value is always overridden in the play function.
    params: { control: { disable: true } },
  },
});
<!-- JS snippets still needed while providing both CSF 3 & Next -->
js
import preview from '../.storybook/preview';

// ๐Ÿ‘‡ Automocked module resolves to '../lib/__mocks__/db'
import db from '../lib/db';
import Page from './Page.vue';

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

export const Basic = meta.story({
  play: async ({ mount, args, userEvent }) => {
    const note = await db.note.create({
      data: { title: 'Mount inside of play' },
    });

    const canvas = await mount(
      Page,
      // ๐Ÿ‘‡ Pass data that is created inside of the play function to the component
      //   For example, a just-generated UUID
      { props: { ...args, params: { id: String(note.id) } } },
    );

    await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i }));
  },
  argTypes: {
    // ๐Ÿ‘‡ Make the params prop un-controllable, as the value is always overridden in the play function.
    params: { control: { disable: true } },
  },
});