-
Notifications
You must be signed in to change notification settings - Fork 93
/
spy.spec.ts
286 lines (222 loc) · 6.97 KB
/
spy.spec.ts
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
283
284
285
286
import {capture, reset, spy, verify, when} from "../src/ts-mockito";
describe("spying on a real object", () => {
class Real {
public b = 11;
get baz() {
return 3;
}
public foo(a: number) {
return a;
}
public bar() {
return 2;
}
}
function RealFn() {
}
describe("calling a mocked method", () => {
it("delegates a call to the mock", () => {
// given
const foo = new Real();
const spiedFoo = spy(foo);
// when
when(spiedFoo.bar()).thenReturn(3);
// then
expect(foo.bar()).toBe(3);
});
it("executes the real method if arguments don't match", () => {
// given
const foo = new Real();
const spiedFoo = spy(foo);
// when
when(spiedFoo.foo(1)).thenReturn(42);
// then
expect(foo.foo(2)).toBe(2);
});
});
describe("calling a real method", () => {
it("executes the instance method", () => {
// given
const foo = new Real();
// when
spy(foo);
// then
expect(foo.bar()).toBe(2);
});
});
describe("calling an object's own method", () => {
it("delegates a call to the mock", () => {
// given
const foo = {
bar: () => 3,
};
const spiedFoo = spy(foo);
// when
when(spiedFoo.bar()).thenReturn(42);
// then
expect(foo.bar()).toBe(42);
});
});
describe("spying functions", () => {
it("should not mock function.prototype methods", () => {
// when
spy(RealFn);
expect(RealFn.bind).toBe(Function.prototype.bind);
expect(RealFn.apply).toBe(Function.prototype.apply);
});
});
describe("access to a real object property", () => {
it("get instance property", () => {
// given
const foo = new Real();
// when
spy(foo);
// then
expect(foo.b).toBe(11);
});
});
describe("capturing", () => {
it("captures a call to the real method", () => {
// given
const foo = new Real();
const spiedFoo = spy(foo);
// when
foo.bar();
// then
expect(capture(spiedFoo.bar).last()).toBeDefined();
});
it("captures the call arguments", () => {
// given
const foo = new Real();
const spiedFoo = spy(foo);
// when
foo.foo(42);
// then
expect(capture<number>(spiedFoo.foo).last()).toEqual([42]);
});
it("captures a call to the own property", () => {
// given
const foo = {
bar: a => a,
};
const spiedFoo = spy(foo);
// when
foo.bar(42);
// then
expect(capture<number>(spiedFoo.bar).last()).toEqual([42]);
});
});
describe("verifying calls", () => {
it("throws an error if number of calls doesn't match", () => {
// given
const foo = new Real();
const spiedFoo = spy(foo);
// when
foo.bar();
foo.bar();
// then
expect(() => verify(spiedFoo.bar()).once()).toThrow();
});
describe("when foo() is called before bar()", () => {
it("throws an error if expected foo() to have been called after bar()", () => {
// given
const foo = new Real();
const spiedFoo = spy(foo);
// when
foo.foo(1);
foo.bar();
// then
expect(() => verify(spiedFoo.foo(1)).calledAfter(spiedFoo.bar())).toThrow();
});
it("passes if expected foo() to have been before after bar()", () => {
// given
const foo = new Real();
const spiedFoo = spy(foo);
// when
foo.foo(1);
foo.bar();
// then
expect(() => verify(spiedFoo.foo(1)).calledBefore(spiedFoo.bar())).not.toThrow();
});
});
});
describe("resetting", () => {
it("restores a call to the real method", () => {
// given
const foo = new Real();
const spiedFoo = spy(foo);
// when
when(spiedFoo.bar()).thenReturn(3);
reset(spiedFoo);
// then
expect(foo.bar()).toBe(2);
});
it("cleans up not owned property descriptors", () => {
// given
const foo = new Real();
const spiedFoo = spy(foo);
// when
when(spiedFoo.baz).thenReturn(42);
reset(spiedFoo);
// then
expect(Object.getOwnPropertyDescriptor(foo, "baz")).not.toBeDefined();
});
it("restores getter properties", () => {
// given
const foo = new Real();
const spiedFoo = spy(foo);
// when
when(spiedFoo.baz).thenReturn(42);
reset(spiedFoo);
// then
expect(foo.baz).toBe(3);
});
});
describe("spying on object which doesn't inherit from anything", () => {
let bareObject;
beforeEach(() => {
bareObject = Object.create(null, {
someMethod: {
writable: true,
configurable: true,
value: () => 1,
},
otherMethod: {
writable: true,
configurable: true,
value: () => 2,
},
});
});
it("can be spied (doesn't throw an exception)", () => {
// given
// when
spy(bareObject);
// then
});
it("executes the instance method", () => {
// given
// when
spy(bareObject);
// then
expect(bareObject.otherMethod()).toBe(2);
});
it("delegates a call to the mock", () => {
// given
const spiedObject = spy(bareObject);
// when
when(spiedObject.someMethod()).thenReturn(2);
// then
expect(bareObject.someMethod()).toBe(2);
});
it("delegates a call to the mock for dynamically created function", () => {
// given
bareObject.newMethod = () => true;
const spiedObject = spy(bareObject);
// when
when(spiedObject.newMethod()).thenReturn(false);
// then
expect(bareObject.newMethod()).toBeFalsy();
});
});
});