UNPKG

2.49 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.snapshot = exports.renderOnly = exports.renderWithOptions = exports.shallowSnapshot = exports.multiSnapshotWithOptions = exports.snapshotWithOptions = void 0;
4require("jest-specific-snapshot");
5const isFunction = (obj) => !!(obj && obj.constructor && obj.call && obj.apply);
6const optionsOrCallOptions = (opts, story) => (isFunction(opts) ? opts(story) : opts);
7function 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.framework === '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}
35exports.snapshotWithOptions = snapshotWithOptions;
36function multiSnapshotWithOptions(options = {}) {
37 return ({ story, context, renderTree, stories2snapsConverter }) => {
38 const snapshotFileName = stories2snapsConverter.getSnapshotFileName(context);
39 return snapshotWithOptions(options)({ story, context, renderTree, snapshotFileName });
40 };
41}
42exports.multiSnapshotWithOptions = multiSnapshotWithOptions;
43exports.shallowSnapshot = ({ story, context, renderShallowTree, options = {}, }) => {
44 const result = renderShallowTree(story, context, options);
45 expect(result).toMatchSnapshot();
46};
47function renderWithOptions(options = {}) {
48 return ({ story, context, renderTree }) => {
49 const result = renderTree(story, context, options);
50 if (typeof result.then === 'function') {
51 return result;
52 }
53 return undefined;
54 };
55}
56exports.renderWithOptions = renderWithOptions;
57exports.renderOnly = renderWithOptions();
58exports.snapshot = snapshotWithOptions();