Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50650 views
1
'use strict';
2
3
Object.defineProperty(exports, "__esModule", {
4
value: true
5
});
6
exports.spyOn = exports.createSpy = exports.restoreSpies = exports.isSpy = undefined;
7
8
var _defineProperties = require('define-properties');
9
10
var _assert = require('./assert');
11
12
var _assert2 = _interopRequireDefault(_assert);
13
14
var _TestUtils = require('./TestUtils');
15
16
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } /*eslint-disable prefer-rest-params, no-underscore-dangle*/
19
20
21
var noop = function noop() {};
22
23
var supportsConfigurableFnLength = _defineProperties.supportsDescriptors && Object.getOwnPropertyDescriptor(function () {}, 'length').configurable;
24
25
var isSpy = exports.isSpy = function isSpy(object) {
26
return object && object.__isSpy === true;
27
};
28
29
var spies = [];
30
31
var restoreSpies = exports.restoreSpies = function restoreSpies() {
32
for (var i = spies.length - 1; i >= 0; i--) {
33
spies[i].restore();
34
}spies = [];
35
};
36
37
var createSpy = exports.createSpy = function createSpy(fn) {
38
var restore = arguments.length <= 1 || arguments[1] === undefined ? noop : arguments[1];
39
40
if (fn == null) fn = noop;
41
42
(0, _assert2.default)((0, _TestUtils.isFunction)(fn), 'createSpy needs a function');
43
44
var targetFn = void 0,
45
thrownValue = void 0,
46
returnValue = void 0,
47
spy = void 0;
48
49
function spyLogic() {
50
spy.calls.push({
51
context: this,
52
arguments: Array.prototype.slice.call(arguments, 0)
53
});
54
55
if (targetFn) return targetFn.apply(this, arguments);
56
57
if (thrownValue) throw thrownValue;
58
59
return returnValue;
60
}
61
62
if (supportsConfigurableFnLength) {
63
spy = Object.defineProperty(spyLogic, 'length', { value: fn.length, writable: false, enumerable: false, configurable: true });
64
} else {
65
spy = new Function('spy', 'return function(' + // eslint-disable-line no-new-func
66
[].concat(_toConsumableArray(Array(fn.length))).map(function (_, i) {
67
return '_' + i;
68
}).join(',') + ') {\n return spy.apply(this, arguments)\n }')(spyLogic);
69
}
70
71
spy.calls = [];
72
73
spy.andCall = function (otherFn) {
74
targetFn = otherFn;
75
return spy;
76
};
77
78
spy.andCallThrough = function () {
79
return spy.andCall(fn);
80
};
81
82
spy.andThrow = function (value) {
83
thrownValue = value;
84
return spy;
85
};
86
87
spy.andReturn = function (value) {
88
returnValue = value;
89
return spy;
90
};
91
92
spy.getLastCall = function () {
93
return spy.calls[spy.calls.length - 1];
94
};
95
96
spy.reset = function () {
97
spy.calls = [];
98
};
99
100
spy.restore = spy.destroy = restore;
101
102
spy.__isSpy = true;
103
104
spies.push(spy);
105
106
return spy;
107
};
108
109
var spyOn = exports.spyOn = function spyOn(object, methodName) {
110
var original = object[methodName];
111
112
if (!isSpy(original)) {
113
(0, _assert2.default)((0, _TestUtils.isFunction)(original), 'Cannot spyOn the %s property; it is not a function', methodName);
114
115
object[methodName] = createSpy(original, function () {
116
object[methodName] = original;
117
});
118
}
119
120
return object[methodName];
121
};
122