1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.snapshot = exports.renderOnly = exports.renderWithOptions = exports.shallowSnapshot = exports.multiSnapshotWithOptions = exports.snapshotWithOptions = void 0;
|
4 | require("jest-specific-snapshot");
|
5 | var isFunction = function (obj) { return !!(obj && obj.constructor && obj.call && obj.apply); };
|
6 | var optionsOrCallOptions = function (opts, story) { return (isFunction(opts) ? opts(story) : opts); };
|
7 | function snapshotWithOptions(options) {
|
8 | if (options === void 0) { options = {}; }
|
9 | return function (_a) {
|
10 | var story = _a.story, context = _a.context, renderTree = _a.renderTree, snapshotFileName = _a.snapshotFileName;
|
11 | var result = renderTree(story, context, optionsOrCallOptions(options, story));
|
12 | function match(tree) {
|
13 | var target = tree;
|
14 | var isReact = story.parameters.framework === 'react';
|
15 | if (isReact && typeof tree.childAt === 'function') {
|
16 | target = tree.childAt(0);
|
17 | }
|
18 | if (isReact && Array.isArray(tree.children)) {
|
19 | target = tree.children[0];
|
20 | }
|
21 | if (snapshotFileName) {
|
22 | expect(target).toMatchSpecificSnapshot(snapshotFileName);
|
23 | }
|
24 | else {
|
25 | expect(target).toMatchSnapshot();
|
26 | }
|
27 | if (typeof tree.unmount === 'function') {
|
28 | tree.unmount();
|
29 | }
|
30 | }
|
31 | if (typeof result.then === 'function') {
|
32 | return result.then(match);
|
33 | }
|
34 | return match(result);
|
35 | };
|
36 | }
|
37 | exports.snapshotWithOptions = snapshotWithOptions;
|
38 | function multiSnapshotWithOptions(options) {
|
39 | if (options === void 0) { options = {}; }
|
40 | return function (_a) {
|
41 | var story = _a.story, context = _a.context, renderTree = _a.renderTree, stories2snapsConverter = _a.stories2snapsConverter;
|
42 | var snapshotFileName = stories2snapsConverter.getSnapshotFileName(context);
|
43 | return snapshotWithOptions(options)({ story: story, context: context, renderTree: renderTree, snapshotFileName: snapshotFileName });
|
44 | };
|
45 | }
|
46 | exports.multiSnapshotWithOptions = multiSnapshotWithOptions;
|
47 | exports.shallowSnapshot = function (_a) {
|
48 | var story = _a.story, context = _a.context, renderShallowTree = _a.renderShallowTree, _b = _a.options, options = _b === void 0 ? {} : _b;
|
49 | var result = renderShallowTree(story, context, options);
|
50 | expect(result).toMatchSnapshot();
|
51 | };
|
52 | function renderWithOptions(options) {
|
53 | if (options === void 0) { options = {}; }
|
54 | return function (_a) {
|
55 | var story = _a.story, context = _a.context, renderTree = _a.renderTree;
|
56 | var result = renderTree(story, context, options);
|
57 | if (typeof result.then === 'function') {
|
58 | return result;
|
59 | }
|
60 | return undefined;
|
61 | };
|
62 | }
|
63 | exports.renderWithOptions = renderWithOptions;
|
64 | exports.renderOnly = renderWithOptions();
|
65 | exports.snapshot = snapshotWithOptions();
|