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 | const isFunction = (obj) => !!(obj && obj.constructor && obj.call && obj.apply);
|
6 | const optionsOrCallOptions = (opts, story) => (isFunction(opts) ? opts(story) : opts);
|
7 | function snapshotWithOptions(options = {}) {
|
8 | return ({ story, context, renderTree, snapshotFileName }) => {
|
9 | const result = renderTree(story, context, optionsOrCallOptions(options, story));
|
10 | function match(tree) {
|
11 | let target = tree;
|
12 | const isReact = story.parameters.renderer === 'react';
|
13 | if (isReact && typeof tree.childAt === 'function') {
|
14 | target = tree.childAt(0);
|
15 | }
|
16 | if (isReact && Array.isArray(tree.children)) {
|
17 | [target] = tree.children;
|
18 | }
|
19 | if (snapshotFileName) {
|
20 | expect(target).toMatchSpecificSnapshot(snapshotFileName);
|
21 | }
|
22 | else {
|
23 | expect(target).toMatchSnapshot();
|
24 | }
|
25 | if (typeof tree.unmount === 'function') {
|
26 | tree.unmount();
|
27 | }
|
28 | }
|
29 | if (typeof result.then === 'function') {
|
30 | return result.then(match);
|
31 | }
|
32 | return match(result);
|
33 | };
|
34 | }
|
35 | exports.snapshotWithOptions = snapshotWithOptions;
|
36 | function multiSnapshotWithOptions(options = {}) {
|
37 | return ({ story, context, renderTree, stories2snapsConverter }) => {
|
38 | const snapshotFileName = stories2snapsConverter.getSnapshotFileName(context);
|
39 | return snapshotWithOptions(options)({ story, context, renderTree, snapshotFileName });
|
40 | };
|
41 | }
|
42 | exports.multiSnapshotWithOptions = multiSnapshotWithOptions;
|
43 | const shallowSnapshot = ({ story, context, renderShallowTree, options = {}, }) => {
|
44 | const result = renderShallowTree(story, context, options);
|
45 | expect(result).toMatchSnapshot();
|
46 | };
|
47 | exports.shallowSnapshot = shallowSnapshot;
|
48 | function renderWithOptions(options = {}) {
|
49 | return ({ story, context, renderTree }) => {
|
50 | const result = renderTree(story, context, options);
|
51 | if (typeof result.then === 'function') {
|
52 | return result;
|
53 | }
|
54 | return undefined;
|
55 | };
|
56 | }
|
57 | exports.renderWithOptions = renderWithOptions;
|
58 | exports.renderOnly = renderWithOptions();
|
59 | exports.snapshot = snapshotWithOptions();
|