-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
mock.js
200 lines (160 loc) · 5.68 KB
/
mock.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"use strict";
var arrayProto = require("@sinonjs/commons").prototypes.array;
var mockExpectation = require("./mock-expectation");
var spyCallToString = require("./call").toString;
var extend = require("./util/core/extend");
var deepEqual = require("@sinonjs/samsam").deepEqual;
var wrapMethod = require("./util/core/wrap-method");
var usePromiseLibrary = require("./util/core/use-promise-library");
var concat = arrayProto.concat;
var filter = arrayProto.filter;
var forEach = arrayProto.forEach;
var every = arrayProto.every;
var join = arrayProto.join;
var push = arrayProto.push;
var slice = arrayProto.slice;
var unshift = arrayProto.unshift;
function mock(object) {
if (!object || typeof object === "string") {
return mockExpectation.create(object ? object : "Anonymous mock");
}
return mock.create(object);
}
function each(collection, callback) {
var col = collection || [];
forEach(col, callback);
}
function arrayEquals(arr1, arr2, compareLength) {
if (compareLength && arr1.length !== arr2.length) {
return false;
}
return every(arr1, function(element, i) {
return deepEqual(arr2[i], element);
});
}
extend(mock, {
create: function create(object) {
if (!object) {
throw new TypeError("object is null");
}
var mockObject = extend.nonEnum({}, mock, { object: object });
delete mockObject.create;
return mockObject;
},
expects: function expects(method) {
if (!method) {
throw new TypeError("method is falsy");
}
if (!this.expectations) {
this.expectations = {};
this.proxies = [];
this.failures = [];
}
if (!this.expectations[method]) {
this.expectations[method] = [];
var mockObject = this;
wrapMethod(this.object, method, function() {
return mockObject.invokeMethod(method, this, arguments);
});
push(this.proxies, method);
}
var expectation = mockExpectation.create(method);
extend.nonEnum(expectation, this.object[method]);
push(this.expectations[method], expectation);
usePromiseLibrary(this.promiseLibrary, expectation);
return expectation;
},
restore: function restore() {
var object = this.object;
each(this.proxies, function(proxy) {
if (typeof object[proxy].restore === "function") {
object[proxy].restore();
}
});
},
verify: function verify() {
var expectations = this.expectations || {};
var messages = this.failures ? slice(this.failures) : [];
var met = [];
each(this.proxies, function(proxy) {
each(expectations[proxy], function(expectation) {
if (!expectation.met()) {
push(messages, String(expectation));
} else {
push(met, String(expectation));
}
});
});
this.restore();
if (messages.length > 0) {
mockExpectation.fail(join(concat(messages, met), "\n"));
} else if (met.length > 0) {
mockExpectation.pass(join(concat(messages, met), "\n"));
}
return true;
},
usingPromise: function usingPromise(promiseLibrary) {
this.promiseLibrary = promiseLibrary;
return this;
},
invokeMethod: function invokeMethod(method, thisValue, args) {
/* if we cannot find any matching files we will explicitly call mockExpection#fail with error messages */
/* eslint consistent-return: "off" */
var expectations = this.expectations && this.expectations[method] ? this.expectations[method] : [];
var currentArgs = args || [];
var available;
var expectationsWithMatchingArgs = filter(expectations, function(expectation) {
var expectedArgs = expectation.expectedArguments || [];
return arrayEquals(expectedArgs, currentArgs, expectation.expectsExactArgCount);
});
var expectationsToApply = filter(expectationsWithMatchingArgs, function(expectation) {
return !expectation.met() && expectation.allowsCall(thisValue, args);
});
if (expectationsToApply.length > 0) {
return expectationsToApply[0].apply(thisValue, args);
}
var messages = [];
var exhausted = 0;
forEach(expectationsWithMatchingArgs, function(expectation) {
if (expectation.allowsCall(thisValue, args)) {
available = available || expectation;
} else {
exhausted += 1;
}
});
if (available && exhausted === 0) {
return available.apply(thisValue, args);
}
forEach(expectations, function(expectation) {
push(messages, " " + String(expectation));
});
unshift(
messages,
"Unexpected call: " +
spyCallToString.call({
proxy: method,
args: args
})
);
var err = new Error();
if (!err.stack) {
// PhantomJS does not serialize the stack trace until the error has been thrown
try {
throw err;
} catch (e) {
/* empty */
}
}
push(
this.failures,
"Unexpected call: " +
spyCallToString.call({
proxy: method,
args: args,
stack: err.stack
})
);
mockExpectation.fail(join(messages, "\n"));
}
});
module.exports = mock;