-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
shared-spy-stub-everything-tests.js
201 lines (167 loc) · 4.99 KB
/
shared-spy-stub-everything-tests.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
/* eslint-env mocha */
/* eslint-disable jsdoc/require-jsdoc */
"use strict";
var referee = require("@sinonjs/referee");
var assert = referee.assert;
var refute = referee.refute;
module.exports = function shared(createSpyOrStub) {
it("replaces all methods of an object when no property is given", function () {
var obj = {
func1: function () {
return;
},
func2: function () {
return;
},
func3: function () {
return;
},
};
createSpyOrStub(obj);
assert.isFunction(obj.func1.restore);
assert.isFunction(obj.func2.restore);
assert.isFunction(obj.func3.restore);
});
it("replaces prototype methods", function () {
function Obj() {
return;
}
Obj.prototype.func1 = function () {
return;
};
var obj = new Obj();
createSpyOrStub(obj);
assert.isFunction(obj.func1.restore);
});
it("returns object", function () {
var object = {
func1: function () {
return;
},
};
assert.same(createSpyOrStub(object), object);
});
it("only replaces functions", function () {
var object = {
foo: "bar",
baz: function () {
return;
},
};
createSpyOrStub(object);
assert.equals(object.foo, "bar");
});
it("handles non-enumerable properties", function () {
var obj = {
func1: function () {
return;
},
func2: function () {
return;
},
};
Object.defineProperty(obj, "func3", {
value: function () {
return;
},
writable: true,
configurable: true,
});
createSpyOrStub(obj);
assert.isFunction(obj.func1.restore);
assert.isFunction(obj.func2.restore);
assert.isFunction(obj.func3.restore);
});
it("handles non-enumerable properties on prototypes", function () {
function Obj() {
return;
}
Object.defineProperty(Obj.prototype, "func1", {
value: function () {
return;
},
writable: true,
configurable: true,
});
var obj = new Obj();
createSpyOrStub(obj);
assert.isFunction(obj.func1.restore);
});
it("does not replace non-enumerable properties from Object.prototype", function () {
var obj = {
noop: function () {
return;
},
};
createSpyOrStub(obj);
refute.isFunction(obj.toString.restore);
refute.isFunction(obj.toLocaleString.restore);
refute.isFunction(obj.propertyIsEnumerable.restore);
});
it("does not fail on overrides", function () {
var parent = {
func: function () {
return;
},
};
var child = Object.create(parent);
child.func = function () {
return;
};
refute.exception(function () {
createSpyOrStub(child);
});
});
it("throws on non-existent property", function () {
var myObj = {
ignoreme: function () {
return;
},
};
assert.exception(function () {
createSpyOrStub(myObj, "ouch");
});
assert.isUndefined(myObj.ouch);
});
it("throws on data property descriptors that are not writable or configurable", function () {
var myObj = {};
Object.defineProperty(myObj, "ignoreme", {
writable: false,
configurable: false,
});
assert.exception(function () {
createSpyOrStub(myObj, "ignoreme");
}, new TypeError(
"Descriptor for property ignoreme is non-configurable and non-writable"
));
});
it("throws on accessor property descriptors that are not configurable", function () {
var myObj = {};
Object.defineProperty(myObj, "ignoreme", {
get: function (key) {
return this[key];
},
set: function (key, val) {
this[key] = val;
},
configurable: false,
});
assert.exception(function () {
createSpyOrStub(myObj, "ignoreme");
}, new TypeError(
"Descriptor for accessor property ignoreme is non-configurable"
));
});
it("throws on data descriptors that are not stubbable", function () {
var myObj = {};
Object.defineProperty(myObj, "ignoreme", {
writable: false,
configurable: false,
});
assert.exception(function () {
createSpyOrStub(myObj, "ignoreme");
}, new TypeError(
"Descriptor for data property ignoreme is non-writable"
));
});
};