/
with-screenshot.ts
59 lines (51 loc) · 1.76 KB
/
with-screenshot.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { StoryKind, MakeDecorator } from '@storybook/addons';
let makeDecorator: MakeDecorator | undefined;
try {
makeDecorator = require('@storybook/addons').makeDecorator;
} catch {}
try {
makeDecorator = require('@storybook/preview-api').makeDecorator;
} catch {}
import type { ScreenshotOptions } from '../shared/types';
const { triggerScreenshot } = require('./trigger-screenshot');
export interface WithScreenshot {
/**
*
* @deprecated
* You can use `addParameters` instead of call decorator as a function with Storybook v5 or later.
*
*/
<T = Function>(options?: Partial<ScreenshotOptions>): T;
}
// NOTE:
// `makeDecorator` is only available with @storybook/addons@^5.0.0 .
const withScreenshotDecorator =
makeDecorator &&
makeDecorator({
name: 'withScreenshot',
parameterName: 'screenshot',
skipIfNoParametersOrOptions: false,
allowDeprecatedUsage: true,
wrapper: (getStory, context, { parameters, options }) => {
if (typeof process !== 'undefined' && process?.env.JEST_WORKER_ID !== undefined) {
return getStory(context);
}
const screenshotOptions = parameters || options;
triggerScreenshot(screenshotOptions, context);
return getStory(context);
},
});
function withScreenshotLegacy(screenshotOptions: ScreenshotOptions = {}) {
return (storyFn: Function, ctx: StoryKind | undefined) => {
const wrapperWithContext = (context: any) => {
triggerScreenshot(screenshotOptions, context);
return storyFn(context);
};
if (ctx) {
return wrapperWithContext(ctx);
}
return (context: StoryKind) => wrapperWithContext(context);
};
}
const withScreenshot: WithScreenshot = (withScreenshotDecorator || withScreenshotLegacy) as any;
export { withScreenshot };