/
wrap-method-test.js
282 lines (225 loc) · 9.08 KB
/
wrap-method-test.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"use strict";
var referee = require("@sinonjs/referee");
var wrapMethod = require("../../../lib/sinon/util/core/wrap-method");
var createSpy = require("../../../lib/sinon/spy");
var createStub = require("../../../lib/sinon/stub");
var assert = referee.assert;
var refute = referee.refute;
describe("util/core/wrapMethod", function () {
beforeEach(function () {
this.method = function () {};
this.getter = function () {};
this.setter = function () {};
this.object = {method: this.method};
Object.defineProperty(this.object, "property", {
get: this.getter,
set: this.setter,
configurable: true
});
});
it("is function", function () {
assert.isFunction(wrapMethod);
});
it("throws if first argument is not object", function () {
assert.exception(function () {
wrapMethod();
}, "TypeError");
});
it("throws if object defines property but is not function", function () {
this.object.prop = 42;
var object = this.object;
assert.exception(function () {
wrapMethod(object, "prop", function () {});
}, "TypeError");
});
it("throws Symbol() if object defines property but is not function", function () {
if (typeof Symbol === "function") {
var symbol = Symbol();
var object = {};
object[symbol] = 42;
assert.exception(function () {
wrapMethod(object, symbol, function () {});
}, function (err) {
return err.message === "Attempted to wrap number property Symbol() as function";
});
}
});
it("throws if object does not define property", function () {
var object = this.object;
assert.exception(function () {
wrapMethod(object, "prop", function () {});
});
assert.exception(
function () {
wrapMethod(object, "prop", function () {});
},
{
message: /Attempted to wrap .* property .* as function/
}
);
});
it("throws if third argument is missing", function () {
var object = this.object;
assert.exception(function () {
wrapMethod(object, "method");
}, "TypeError");
});
it("throws if third argument is not a function or a property descriptor", function () {
var object = this.object;
assert.exception(function () {
wrapMethod(object, "method", 1);
}, "TypeError");
});
it("replaces object method", function () {
wrapMethod(this.object, "method", function () {});
refute.same(this.method, this.object.method);
assert.isFunction(this.object.method);
});
it("replaces getter", function () {
wrapMethod(this.object, "property", { get: function () {} });
refute.same(this.getter, Object.getOwnPropertyDescriptor(this.object, "property").get);
assert.isFunction(Object.getOwnPropertyDescriptor(this.object, "property").get);
});
it("replaces setter", function () {
wrapMethod(this.object, "property", { // eslint-disable-line accessor-pairs
set: function () {}
});
refute.same(this.setter, Object.getOwnPropertyDescriptor(this.object, "property").set);
assert.isFunction(Object.getOwnPropertyDescriptor(this.object, "property").set);
});
it("throws if method is already wrapped", function () {
wrapMethod(this.object, "method", function () {});
assert.exception(function () {
wrapMethod(this.object, "method", function () {});
}, "TypeError");
});
it("throws Symbol if method is already wrapped", function () {
if (typeof Symbol === "function") {
var symbol = Symbol();
var object = {};
object[symbol] = function () {};
wrapMethod(object, symbol, function () {});
assert.exception(function () {
wrapMethod(object, symbol, function () {});
}, function (err) {
return err.message === "Attempted to wrap Symbol() which is already wrapped";
});
}
});
it("throws if property descriptor is already wrapped", function () {
wrapMethod(this.object, "property", { get: function () {} });
assert.exception(function () {
wrapMethod(this.object, "property", { get: function () {} });
}, "TypeError");
});
it("throws if method is already a spy", function () {
var object = { method: createSpy() };
assert.exception(function () {
wrapMethod(object, "method", function () {});
}, "TypeError");
});
it("throws if Symbol method is already a spy", function () {
if (typeof Symbol === "function") {
var symbol = Symbol();
var object = {};
object[symbol] = createSpy();
assert.exception(function () {
wrapMethod(object, symbol, function () {});
}, function (err) {
return err.message === "Attempted to wrap Symbol() which is already spied on";
});
}
});
var overridingErrorAndTypeError = (function () {
return !(typeof navigator === "object" && /PhantomJS/.test(navigator.userAgent));
}());
if (overridingErrorAndTypeError) {
describe("originating stack traces", function () {
beforeEach(function () {
this.oldError = Error;
this.oldTypeError = TypeError;
var i = 0;
Error = TypeError = function () { // eslint-disable-line no-native-reassign, no-undef
this.stack = ":STACK" + ++i + ":";
};
});
afterEach(function () {
Error = this.oldError; // eslint-disable-line no-native-reassign, no-undef
TypeError = this.oldTypeError; // eslint-disable-line no-native-reassign, no-undef
});
it("throws with stack trace showing original wrapMethod call", function () {
var object = { method: function () {} };
wrapMethod(object, "method", function () {
return "original";
});
assert.exception(
function () {
wrapMethod(object, "method", function () {});
},
{
stack: ":STACK2:\n--------------\n:STACK1:"
}
);
});
});
}
if (typeof window !== "undefined") {
describe("in browser", function () {
it("does not throw if object is window object", function () {
window.sinonTestMethod = function () {};
refute.exception(function () {
wrapMethod(window, "sinonTestMethod", function () {});
});
});
});
}
it("mirrors function properties", function () {
var object = { method: function () {} };
object.method.prop = 42;
wrapMethod(object, "method", function () {});
assert.equals(object.method.prop, 42);
});
it("does not mirror and overwrite existing properties", function () {
var object = { method: function () {} };
object.method.called = 42;
createStub(object, "method");
assert.isFalse(object.method.called);
});
describe("wrapped method", function () {
beforeEach(function () {
this.method = function () {};
this.object = { method: this.method };
});
it("defines restore method", function () {
wrapMethod(this.object, "method", function () {});
assert.isFunction(this.object.method.restore);
});
it("returns wrapper", function () {
var wrapper = wrapMethod(this.object, "method", function () {});
assert.same(this.object.method, wrapper);
});
it("restore brings back original method", function () {
wrapMethod(this.object, "method", function () {});
this.object.method.restore();
assert.same(this.object.method, this.method);
});
});
describe("wrapped prototype method", function () {
beforeEach(function () {
this.type = function () {};
this.type.prototype.method = function () {};
this.object = new this.type(); //eslint-disable-line new-cap
});
it("wrap adds owned property", function () {
var wrapper = wrapMethod(this.object, "method", function () {});
assert.same(this.object.method, wrapper);
assert(this.object.hasOwnProperty("method"));
});
it("restore removes owned property", function () {
wrapMethod(this.object, "method", function () {});
this.object.method.restore();
assert.same(this.object.method, this.type.prototype.method);
assert.isFalse(this.object.hasOwnProperty("method"));
});
});
});