2023-10-26 13:27:17 +02:00
|
|
|
import type { Decorator } from '@storybook/react'
|
2022-12-07 11:40:56 +01:00
|
|
|
import MockDate from 'mockdate'
|
|
|
|
|
2023-10-27 22:47:36 +02:00
|
|
|
declare module '@storybook/types' {
|
|
|
|
interface Parameters {
|
|
|
|
mockDate?: string | number | Date
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 23:02:46 +02:00
|
|
|
/** Global story decorator that allows mocking of dates.
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* export default {
|
|
|
|
* title: 'My story',
|
|
|
|
* component: MyComponent,
|
|
|
|
* parameters: {
|
|
|
|
* mockDate: '2023-02-01', // add mock date here
|
|
|
|
* },
|
|
|
|
* } as ComponentMeta<typeof MyComponent>
|
|
|
|
* ```
|
|
|
|
*/
|
2023-10-26 13:27:17 +02:00
|
|
|
export const withMockDate: Decorator = (Story, { parameters }) => {
|
2022-12-07 11:40:56 +01:00
|
|
|
if (parameters.mockDate) {
|
|
|
|
MockDate.set(parameters.mockDate)
|
|
|
|
} else {
|
|
|
|
MockDate.reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
return <Story />
|
|
|
|
}
|