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.stub = exports.mockFn = exports.mockDeep = exports.mockReset = exports.mockClear = exports.JestMockExtended = void 0;
|
7 | const CalledWithFn_1 = __importDefault(require("./CalledWithFn"));
|
8 | const DEFAULT_CONFIG = {
|
9 | ignoreProps: ['then'],
|
10 | };
|
11 | let GLOBAL_CONFIG = DEFAULT_CONFIG;
|
12 | exports.JestMockExtended = {
|
13 | DEFAULT_CONFIG,
|
14 | configure: (config) => {
|
15 |
|
16 | GLOBAL_CONFIG = Object.assign(Object.assign({}, DEFAULT_CONFIG), config);
|
17 | },
|
18 | resetConfig: () => {
|
19 | GLOBAL_CONFIG = DEFAULT_CONFIG;
|
20 | },
|
21 | };
|
22 | const mockClear = (mock) => {
|
23 | for (let key of Object.keys(mock)) {
|
24 | if (mock[key] === null || mock[key] === undefined) {
|
25 | continue;
|
26 | }
|
27 | if (mock[key]._isMockObject) {
|
28 | (0, exports.mockClear)(mock[key]);
|
29 | }
|
30 | if (mock[key]._isMockFunction) {
|
31 | mock[key].mockClear();
|
32 | }
|
33 | }
|
34 |
|
35 | if (!mock._isMockObject) {
|
36 | return mock.mockClear();
|
37 | }
|
38 | };
|
39 | exports.mockClear = mockClear;
|
40 | const mockReset = (mock) => {
|
41 | for (let key of Object.keys(mock)) {
|
42 | if (mock[key] === null || mock[key] === undefined) {
|
43 | continue;
|
44 | }
|
45 | if (mock[key]._isMockObject) {
|
46 | (0, exports.mockReset)(mock[key]);
|
47 | }
|
48 | if (mock[key]._isMockFunction) {
|
49 | mock[key].mockReset();
|
50 | }
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 | if (!mock._isMockObject) {
|
56 | return mock.mockReset();
|
57 | }
|
58 | };
|
59 | exports.mockReset = mockReset;
|
60 | function mockDeep(arg1, arg2) {
|
61 | const [opts, mockImplementation] = typeof arg1 === 'object' && (typeof arg1.fallbackMockImplementation === 'function' || arg1.funcPropSupport === true)
|
62 | ? [arg1, arg2]
|
63 | : [{}, arg1];
|
64 | return mock(mockImplementation, { deep: true, fallbackMockImplementation: opts.fallbackMockImplementation });
|
65 | }
|
66 | exports.mockDeep = mockDeep;
|
67 | const overrideMockImp = (obj, opts) => {
|
68 | const proxy = new Proxy(obj, handler(opts));
|
69 | for (let name of Object.keys(obj)) {
|
70 | if (typeof obj[name] === 'object' && obj[name] !== null) {
|
71 | proxy[name] = overrideMockImp(obj[name], opts);
|
72 | }
|
73 | else {
|
74 | proxy[name] = obj[name];
|
75 | }
|
76 | }
|
77 | return proxy;
|
78 | };
|
79 | const handler = (opts) => ({
|
80 | ownKeys(target) {
|
81 | return Reflect.ownKeys(target);
|
82 | },
|
83 | set: (obj, property, value) => {
|
84 |
|
85 | obj[property] = value;
|
86 | return true;
|
87 | },
|
88 | get: (obj, property) => {
|
89 | var _a;
|
90 | let fn = (0, CalledWithFn_1.default)({ fallbackMockImplementation: opts === null || opts === void 0 ? void 0 : opts.fallbackMockImplementation });
|
91 |
|
92 | if (!(property in obj)) {
|
93 | if ((_a = GLOBAL_CONFIG.ignoreProps) === null || _a === void 0 ? void 0 : _a.includes(property)) {
|
94 | return undefined;
|
95 | }
|
96 |
|
97 | if (property === Symbol.iterator) {
|
98 |
|
99 | return obj[property];
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | if ((opts === null || opts === void 0 ? void 0 : opts.deep) && property !== 'calls') {
|
106 |
|
107 | obj[property] = new Proxy(fn, handler(opts));
|
108 |
|
109 | obj[property]._isMockObject = true;
|
110 | }
|
111 | else {
|
112 |
|
113 | obj[property] = (0, CalledWithFn_1.default)({ fallbackMockImplementation: opts === null || opts === void 0 ? void 0 : opts.fallbackMockImplementation });
|
114 | }
|
115 | }
|
116 |
|
117 | if (obj instanceof Date && typeof obj[property] === 'function') {
|
118 |
|
119 | return obj[property].bind(obj);
|
120 | }
|
121 |
|
122 | return obj[property];
|
123 | },
|
124 | });
|
125 | const mock = (mockImplementation = {}, opts) => {
|
126 |
|
127 | mockImplementation._isMockObject = true;
|
128 | return overrideMockImp(mockImplementation, opts);
|
129 | };
|
130 | const mockFn = () => {
|
131 |
|
132 | return (0, CalledWithFn_1.default)();
|
133 | };
|
134 | exports.mockFn = mockFn;
|
135 | const stub = () => {
|
136 | return new Proxy({}, {
|
137 | get: (obj, property) => {
|
138 | if (property in obj) {
|
139 |
|
140 | return obj[property];
|
141 | }
|
142 | return jest.fn();
|
143 | },
|
144 | });
|
145 | };
|
146 | exports.stub = stub;
|
147 | exports.default = mock;
|