/
collection.js
137 lines (107 loc) · 3.22 KB
/
collection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"use strict";
var sinonSpy = require("./spy");
var sinonStub = require("./stub");
var sinonMock = require("./mock");
var collectOwnMethods = require("./collect-own-methods");
var valueToString = require("./util/core/value-to-string");
var push = Array.prototype.push;
var filter = Array.prototype.filter;
function getFakes(fakeCollection) {
if (!fakeCollection.fakes) {
fakeCollection.fakes = [];
}
return fakeCollection.fakes;
}
function each(fakeCollection, method) {
var fakes = getFakes(fakeCollection);
var matchingFakes = filter.call(fakes, function (fake) {
return typeof fake[method] === "function";
});
matchingFakes.forEach(function (fake) {
fake[method]();
});
}
var collection = {
verify: function verify() {
each(this, "verify");
},
restore: function restore() {
each(this, "restore");
this.fakes = [];
},
reset: function reset() {
each(this, "reset");
},
resetBehavior: function resetBehavior() {
each(this, "resetBehavior");
},
resetHistory: function resetHistory() {
getFakes(this).forEach(function (fake) {
var method = fake.resetHistory || fake.reset;
if (method) {
method.call(fake);
}
});
},
verifyAndRestore: function verifyAndRestore() {
var exception;
try {
this.verify();
} catch (e) {
exception = e;
}
this.restore();
if (exception) {
throw exception;
}
},
add: function add(fake) {
push.call(getFakes(this), fake);
return fake;
},
addUsingPromise: function (fake) {
fake.usingPromise(this.promiseLibrary);
return fake;
},
spy: function spy() {
return this.add(sinonSpy.apply(sinonSpy, arguments));
},
stub: function stub(object, property) {
if (object && typeof property !== "undefined"
&& !(property in object)) {
throw new TypeError("Cannot stub non-existent own property " + valueToString(property));
}
var stubbed = sinonStub.apply(null, arguments);
var isStubbingEntireObject = typeof property === "undefined" && typeof object === "object";
if (isStubbingEntireObject) {
var ownMethods = collectOwnMethods(stubbed);
ownMethods.forEach(this.add.bind(this));
if (this.promiseLibrary) {
ownMethods.forEach(this.addUsingPromise.bind(this));
}
} else {
this.add(stubbed);
if (this.promiseLibrary) {
stubbed.usingPromise(this.promiseLibrary);
}
}
return stubbed;
},
mock: function mock() {
return this.add(sinonMock.apply(null, arguments));
},
inject: function inject(obj) {
var col = this;
obj.spy = function () {
return col.spy.apply(col, arguments);
};
obj.stub = function () {
return col.stub.apply(col, arguments);
};
obj.mock = function () {
return col.mock.apply(col, arguments);
};
return obj;
}
};
module.exports = collection;