UNPKG

3.25 kBJavaScriptView Raw
1"use strict";
2var __spreadArrays = (this && this.__spreadArrays) || function () {
3 for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
4 for (var r = Array(s), k = 0, i = 0; i < il; i++)
5 for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
6 r[k] = a[j];
7 return r;
8};
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.stubInterface = exports.stubConstructor = exports.stubObject = void 0;
11var sinon = require("sinon");
12function stubObject(object, methods) {
13 var stubObject = Object.assign({}, object);
14 var objectMethods = getObjectMethods(object);
15 var excludedMethods = [
16 '__defineGetter__', '__defineSetter__', 'hasOwnProperty',
17 '__lookupGetter__', '__lookupSetter__', 'propertyIsEnumerable',
18 'toString', 'valueOf', '__proto__', 'toLocaleString', 'isPrototypeOf'
19 ];
20 for (var method in object) {
21 if (typeof object[method] == "function") {
22 objectMethods.push(method);
23 }
24 }
25 for (var _i = 0, objectMethods_1 = objectMethods; _i < objectMethods_1.length; _i++) {
26 var method = objectMethods_1[_i];
27 if (!excludedMethods.includes(method)) {
28 stubObject[method] = object[method];
29 }
30 }
31 if (Array.isArray(methods)) {
32 for (var _a = 0, methods_1 = methods; _a < methods_1.length; _a++) {
33 var method = methods_1[_a];
34 stubObject[method] = sinon.stub();
35 }
36 }
37 else if (typeof methods == "object") {
38 for (var method in methods) {
39 stubObject[method] = sinon.stub();
40 stubObject[method].returns(methods[method]);
41 }
42 }
43 else {
44 for (var _b = 0, objectMethods_2 = objectMethods; _b < objectMethods_2.length; _b++) {
45 var method = objectMethods_2[_b];
46 if (typeof object[method] == "function" && method !== "constructor") {
47 stubObject[method] = sinon.stub();
48 }
49 }
50 }
51 return stubObject;
52}
53exports.stubObject = stubObject;
54function stubConstructor(constructor) {
55 var constructorArgs = [];
56 for (var _i = 1; _i < arguments.length; _i++) {
57 constructorArgs[_i - 1] = arguments[_i];
58 }
59 return stubObject(new (constructor.bind.apply(constructor, __spreadArrays([void 0], constructorArgs)))());
60}
61exports.stubConstructor = stubConstructor;
62function stubInterface(methods) {
63 if (methods === void 0) { methods = {}; }
64 var object = stubObject({}, methods);
65 return new Proxy(object, {
66 get: function (target, name) {
67 if (!target.hasOwnProperty(name) && name !== 'then') {
68 target[name] = sinon.stub();
69 }
70 return target[name];
71 }
72 });
73}
74exports.stubInterface = stubInterface;
75function getObjectMethods(object) {
76 var methods = [];
77 while ((object = Reflect.getPrototypeOf(object))) {
78 var keys = Reflect.ownKeys(object);
79 keys.forEach(function (key) {
80 if (typeof key === 'string') {
81 methods.push(key);
82 }
83 });
84 }
85 return methods;
86}
87sinon['stubObject'] = stubObject;
88sinon['stubInterface'] = stubInterface;
89exports.default = sinon;