Back to Storybook

Before Each In Meta Mock Date

docs/_snippets/before-each-in-meta-mock-date.md

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

import MockDate from 'mockdate';

import { Page } from './Page.component';

const meta: Meta<Page> = {
  component: Page,
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
};
export default meta;

type Story = StoryObj<Page>;

export const Basic: Story = {
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
};
ts
import MockDate from 'mockdate';

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

import { Page } from './Page.component';

const meta = preview.meta({
  component: Page,
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
});

export const Basic = meta.story({
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
});
svelte
<script module>
  import { defineMeta } from '@storybook/addon-svelte-csf';

  import MockDate from 'mockdate';

  import Page from './Page.svelte';

  const { Story } = defineMeta({
    component: Page,
    // ๐Ÿ‘‡ Set the value of Date for every story in the file
    async beforeEach() {
      MockDate.set('2024-02-14');

      // ๐Ÿ‘‡ Reset the Date after each story
      return () => {
        MockDate.reset();
      };
    },
  });
</script>

<Story name="Default" play={async ({ canvas }) => {
  // ... This will run with the mocked Date
  }}
/>
js
import MockDate from 'mockdate';

import Page from './Page.svelte';

export default {
  component: Page,
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
};

export const Basic = {
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
};
js
import MockDate from 'mockdate';

import { Page } from './Page';

export default {
  component: Page,
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
};

export const Basic = {
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
};
svelte
<script module>
  import { defineMeta } from '@storybook/addon-svelte-csf';

  import MockDate from 'mockdate';

  import Page from './Page.svelte';

  const { Story } = defineMeta({
    component: Page,
    // ๐Ÿ‘‡ Set the value of Date for every story in the file
    async beforeEach() {
      MockDate.set('2024-02-14');

      // ๐Ÿ‘‡ Reset the Date after each story
      return () => {
        MockDate.reset();
      };
    },
  });
</script>

<Story name="Default" play={async ({ canvas }) => {
  // ... This will run with the mocked Date
  }}
/>
ts
// Replace your-framework with svelte-vite or sveltekit
import type { Meta, StoryObj } from '@storybook/your-framework';

import MockDate from 'mockdate';

import Page from './Page.svelte';

const meta = {
  component: Page,
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
} satisfies Meta<typeof Page>;
export default meta;

type Story = StoryObj<typeof meta>;

export const Basic: Story = {
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
};
ts
// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, vue3-vite, etc.
import type { Meta, StoryObj } from '@storybook/your-framework';

import MockDate from 'mockdate';

import { Page } from './Page';

const meta = {
  component: Page,
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
} satisfies Meta<typeof Page>;
export default meta;

type Story = StoryObj<typeof meta>;

export const Basic: Story = {
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
};
js
import MockDate from 'mockdate';

export default {
  component: 'my-page',
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
};

export const Basic = {
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
};
ts
import type { Meta, StoryObj } from '@storybook/web-components-vite';

import MockDate from 'mockdate';

const meta: Meta = {
  component: 'my-page',
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
};
export default meta;

type Story = StoryObj;

export const Basic: Story = {
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
};
js
import MockDate from 'mockdate';

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

const meta = preview.meta({
  component: 'my-page',
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
});

export const Basic = meta.story({
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
});
ts
import MockDate from 'mockdate';

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

const meta = preview.meta({
  component: 'my-page',
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
});

export const Basic = meta.story({
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
});
ts
import MockDate from 'mockdate';

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

import { Page } from './Page';

const meta = preview.meta({
  component: Page,
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
});

export const Basic = meta.story({
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
});
<!-- JS snippets still needed while providing both CSF 3 & Next -->
js
import MockDate from 'mockdate';

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

import { Page } from './Page';

const meta = preview.meta({
  component: Page,
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
});

export const Basic = meta.story({
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
});
ts
import MockDate from 'mockdate';

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

import Page from './Page.vue';

const meta = preview.meta({
  component: Page,
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
});

export const Basic = meta.story({
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
});
<!-- JS snippets still needed while providing both CSF 3 & Next -->
js
import MockDate from 'mockdate';

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

import Page from './Page.vue';

const meta = preview.meta({
  component: Page,
  // ๐Ÿ‘‡ Set the value of Date for every story in the file
  async beforeEach() {
    MockDate.set('2024-02-14');

    // ๐Ÿ‘‡ Reset the Date after each story
    return () => {
      MockDate.reset();
    };
  },
});

export const Basic = meta.story({
  async play({ canvas }) {
    // ... This will run with the mocked Date
  },
});