docs/_snippets/before-each-in-meta-mock-date.md
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
},
};
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
},
});
<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
}}
/>
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
},
};
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
},
};
<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
}}
/>
// 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
},
};
// 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
},
};
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
},
};
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
},
};
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
},
});
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
},
});
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
},
});
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
},
});
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
},
});
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
},
});