1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.Stories2SnapsConverter = void 0;
|
7 | const path_1 = __importDefault(require("path"));
|
8 | const ts_dedent_1 = __importDefault(require("ts-dedent"));
|
9 | const defaultOptions = {
|
10 | snapshotsDirName: '__snapshots__',
|
11 | snapshotExtension: '.storyshot',
|
12 | storiesExtensions: ['.js', '.jsx', '.ts', '.tsx', '.mdx'],
|
13 | };
|
14 | class Stories2SnapsConverter {
|
15 | constructor(options = {}) {
|
16 | this.getSnapshotExtension = () => this.options.snapshotExtension;
|
17 | this.options = Object.assign(Object.assign({}, defaultOptions), options);
|
18 | }
|
19 | getStoryshotFile(fileName) {
|
20 | const { dir, name } = path_1.default.parse(fileName);
|
21 | const { snapshotsDirName, snapshotExtension } = this.options;
|
22 | return path_1.default.format({ dir: path_1.default.join(dir, snapshotsDirName), name, ext: snapshotExtension });
|
23 | }
|
24 | getSnapshotFileName(context) {
|
25 | const { fileName, kind } = context;
|
26 | if (!fileName) {
|
27 |
|
28 | console.warn(ts_dedent_1.default `
|
29 | Storybook was unable to detect filename for stories of kind "${kind}".
|
30 | To fix it, add following to your jest.config.js:
|
31 | transform: {
|
32 | // should be above any other js transform like babel-jest
|
33 | '^.+\\.stories\\.js$': '@storybook/addon-storyshots/injectFileName',
|
34 | }
|
35 | `);
|
36 | return null;
|
37 | }
|
38 | return this.getStoryshotFile(fileName);
|
39 | }
|
40 | getPossibleStoriesFiles(storyshotFile) {
|
41 | const { dir, name } = path_1.default.parse(storyshotFile);
|
42 | const { storiesExtensions } = this.options;
|
43 | return storiesExtensions.map((ext) => path_1.default.format({
|
44 | dir: path_1.default.dirname(dir),
|
45 | name,
|
46 | ext,
|
47 | }));
|
48 | }
|
49 | }
|
50 | exports.Stories2SnapsConverter = Stories2SnapsConverter;
|