1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.calledWithFn = void 0;
|
4 | const Matchers_1 = require("./Matchers");
|
5 | function isJestAsymmetricMatcher(obj) {
|
6 | return !!obj && typeof obj === 'object' && 'asymmetricMatch' in obj && typeof obj.asymmetricMatch === 'function';
|
7 | }
|
8 | const checkCalledWith = (calledWithStack, actualArgs, fallbackMockImplementation) => {
|
9 | const calledWithInstance = calledWithStack.find((instance) => instance.args.every((matcher, i) => {
|
10 | if (matcher instanceof Matchers_1.Matcher) {
|
11 | return matcher.asymmetricMatch(actualArgs[i]);
|
12 | }
|
13 | if (isJestAsymmetricMatcher(matcher)) {
|
14 | return matcher.asymmetricMatch(actualArgs[i]);
|
15 | }
|
16 | return actualArgs[i] === matcher;
|
17 | }));
|
18 |
|
19 | return calledWithInstance
|
20 | ? calledWithInstance.calledWithFn(...actualArgs)
|
21 | : fallbackMockImplementation && fallbackMockImplementation(...actualArgs);
|
22 | };
|
23 | const calledWithFn = ({ fallbackMockImplementation, } = {}) => {
|
24 | const fn = jest.fn(fallbackMockImplementation);
|
25 | let calledWithStack = [];
|
26 | fn.calledWith = (...args) => {
|
27 |
|
28 |
|
29 | const calledWithFn = jest.fn(fallbackMockImplementation);
|
30 | const mockImplementation = fn.getMockImplementation();
|
31 | if (!mockImplementation || mockImplementation === fallbackMockImplementation) {
|
32 |
|
33 | fn.mockImplementation((...args) => checkCalledWith(calledWithStack, args, fallbackMockImplementation));
|
34 | calledWithStack = [];
|
35 | }
|
36 | calledWithStack.unshift({ args, calledWithFn });
|
37 | return calledWithFn;
|
38 | };
|
39 | return fn;
|
40 | };
|
41 | exports.calledWithFn = calledWithFn;
|
42 | exports.default = exports.calledWithFn;
|