-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-spy.test.js
385 lines (275 loc) · 8.27 KB
/
create-spy.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import test from "ava";
import { spyFactory } from "./create-spy.js";
import { handleOptions } from "./handle-options.js";
test("return a spy function", (t) => {
const spy = spyFactory(handleOptions());
t.notThrows(spy); // should be true
});
test("tracks if spy has been called at least once", (t) => {
const spy = spyFactory(handleOptions());
spy();
t.true(spy.hasBeenCalled); // should be true
});
test("can set and get name", (t) => {
const spy = spyFactory(handleOptions());
t.is(spy.spyName, "");
spy.setSpyName("black-venus");
t.is(spy.spyName, "black-venus");
spy.setSpyName("josephine");
t.is(spy.spyName, "josephine");
});
test("Use given fake function given via the 'fakeFunction' method", (t) => {
const spy = spyFactory(handleOptions());
spy.fakeFunction(() => 7000);
const sut = spy();
t.is(sut, 7000);
});
test("Return given fake value", (t) => {
const spy = spyFactory(handleOptions());
spy.fakeValue(17);
const sut = spy();
t.is(sut, 17);
});
test("Record call arguments", (t) => {
const spy = spyFactory(handleOptions());
spy(7, 10, 17, 27);
spy(34, 49);
t.deepEqual(spy.calls, [
[7, 10, 17, 27],
[34, 49],
]);
});
test("Use given name", (t) => {
const spy = spyFactory(
handleOptions({ fakeFunction: (i) => i + 10, spyName: "josephine" })
);
t.is(spy.spyName, "josephine");
});
test("Use given fake function passed as an option", (t) => {
const spy = spyFactory(
handleOptions({ fakeFunction: (i) => i + 10, name: "" })
);
spy(7);
t.is(spy.results[0], 17);
});
test("Allow method chaining", (t) => {
const spy = spyFactory(handleOptions());
spy.setSpyName("josephine").fakeValue(17)();
t.is(spy.results[0], 17);
t.is(spy.spyName, "josephine");
});
test("capture 'this' value during bind operation", (t) => {
const spy = spyFactory(handleOptions());
const bound = spy.bind({ that: "this" });
t.notThrows(spy);
t.notThrows(bound);
t.deepEqual(spy.calls, [[]]);
t.deepEqual(bound.calls, [[]]);
t.deepEqual(spy.instances, [{ that: "this" }]);
});
test("Add custom properties", (t) => {
const spy = spyFactory(handleOptions());
spy.constants.PI = 3.14;
t.notThrows(spy);
t.truthy(spy.fakeFunction);
t.notThrows(spy.constants);
t.truthy(spy.constants.fakeFunction);
t.is(spy.constants.PI, 3.14);
});
test("spyFactory create multiple different spies", (t) => {
const spy1 = spyFactory(handleOptions());
const spy2 = spyFactory(handleOptions());
spy1();
spy1();
spy2();
spy1.setSpyName("Harriet Tubman");
spy2.setSpyName("Josephine");
t.notDeepEqual(spy1, spy2);
t.not(spy1.calls.length, spy2.calls.length);
t.not(spy1.spyName, spy2.spyName);
});
test("Count how many times a property has been accessed", (t) => {
const spy = spyFactory(handleOptions());
spy.key;
spy.key.subKey;
spy.key.subKey.routine();
t.is(spy.getReadCount("key"), 3);
t.is(spy.key.getReadCount("subKey"), 2);
t.is(spy.key.subKey.getReadCount("routine"), 1);
});
test("Create spy with new keyword", (t) => {
const spy = spyFactory(handleOptions());
const result = new spy();
t.truthy(result.fakeFunction);
t.is(result, spy.instances[0]);
});
test("Return spy by default", (t) => {
const spy = spyFactory(handleOptions());
const trainedSpy = spy();
const nestedSpy = spy.nested;
t.truthy(nestedSpy.fakeFunction);
t.truthy(trainedSpy.fakeFunction);
});
test("Use fakeValueOnce 3 times", (t) => {
const spy = spyFactory(handleOptions());
spy();
spy.fakeValueOnce(7).fakeValueOnce(10).fakeValueOnce(17).fakeValue(27);
spy();
spy();
spy();
spy();
const results = spy.results.filter((_, index) => index !== 0);
t.truthy(spy.results[0].fakeFunction);
t.deepEqual(results, [7, 10, 17, 27]);
});
test("Use fakeFunctionOnce 3 times", (t) => {
const spy = spyFactory(handleOptions());
spy();
spy
.fakeFunctionOnce(() => 7)
.fakeFunctionOnce(() => 10)
.fakeFunctionOnce(() => 17)
.fakeFunction(() => 27);
spy();
spy();
spy();
spy();
const results = spy.results.filter((_, index) => index !== 0);
t.truthy(spy.results[0].fakeFunction);
t.deepEqual(results, [7, 10, 17, 27]);
});
test("Create rehearsals with given and return methods", (t) => {
const spy = spyFactory(handleOptions());
spy
.planRehearsals({ given: [7, 10, 10], returns: 27 })
.planRehearsals([{ given: [17, 17], returns: 34 }]);
const result = spy(7, 10, 10);
spy(17, 17);
spy();
t.is(result, 27);
t.is(spy.results[1], 34);
t.truthy(spy.results[2].fakeFunction);
});
test("Create rehearsals with returns being a function", (t) => {
const spy = spyFactory(handleOptions());
spy.planRehearsals({ given: [30, 30], returns: (i, j) => i + j });
const result = spy(30, 30);
t.is(result, 60);
});
test("Create rehearsals with given and resolves methods", async (t) => {
const spy = spyFactory(handleOptions());
spy
.planRehearsals({ given: [7, 10, 10], resolves: 27 })
.planRehearsals([{ given: [17, 17], resolves: 34 }]);
const result1 = await spy(7, 10, 10);
const reuslt2 = await spy(17, 17);
spy();
t.is(result1, 27);
t.is(reuslt2, 34);
t.truthy(spy.results[2].fakeFunction);
});
test("Create rehearsals with resolves as functions", async (t) => {
const spy = spyFactory(handleOptions());
spy.planRehearsals({ given: [50, 5], resolves: async (i, j) => i + j });
const result1 = await spy(50, 5);
t.is(result1, 55);
});
test("Resolve a promise value", async (t) => {
const spy = spyFactory(handleOptions());
spy.fakeResolvedValue(44);
const result = await spy();
t.is(result, 44);
});
test("Return a promise function", async (t) => {
const spy = spyFactory(handleOptions());
spy.fakeFunction(async () => 444);
const result = await spy();
t.is(result, 444);
});
test("Resolve a promise value once", async (t) => {
const spy = spyFactory(handleOptions());
spy.fakeResolvedValueOnce(4444);
const result1 = await spy();
const result2 = await spy();
t.is(result1, 4444);
t.truthy(result2.fakeFunction);
});
test("Return a promise function once", async (t) => {
const spy = spyFactory(handleOptions());
spy.fakeFunctionOnce(async () => 777);
const result1 = await spy();
const result2 = await spy();
t.is(result1, 777);
t.truthy(result2.fakeFunction);
});
test("Reject a value", async (t) => {
const spy = spyFactory(handleOptions());
spy.fakeRejectedValue(new Error("Espionage failed"));
const result1 = await t.throwsAsync(spy);
t.is(result1.message, "Espionage failed");
});
test("Reject a value once", async (t) => {
const spy = spyFactory(handleOptions());
spy.fakeRejectedValueOnce(new Error("Espionage failed"));
const result1 = await t.throwsAsync(spy);
t.is(result1.message, "Espionage failed");
});
test("Throws an error", async (t) => {
const spy = spyFactory(handleOptions());
spy.throws(new TypeError("Espionage failed"));
t.throws(spy, { instanceOf: TypeError });
});
test("Throws an error with specified message", async (t) => {
const spy = spyFactory(handleOptions());
spy.throws("Espionage failed");
t.throws(spy, { message: "Espionage failed" });
});
// test("record created keys in a map", async (t) => {
// const spy = spyFactory(handleOptions());
// spy.setSpyName("josephine");
// spy.key1;
// spy.key2;
// spy.key3;
// spy.key3.key4;
// spy.key3.key5;
// spy.key3.key4.key6;
// spy.key3.key4.key7;
// t.deepEqual(spy.fakeKeyMap, {
// key1: [],
// key2: [],
// key3: [],
// key4: ["key3"],
// key5: ["key3"],
// key6: ["key3", "key4"],
// key7: ["key3", "key4"],
// });
// });
// test("record created keys in a list", async (t) => {
// const spy = spyFactory(handleOptions());
// spy.key1;
// spy.key2;
// spy.key3;
// spy.key3.key4;
// spy.key3.key5;
// spy.key3.key4.key6;
// spy.key3.key4.key7;
// t.deepEqual(spy.fakeKeys, [
// "key1",
// "key2",
// "key3",
// "key4",
// "key5",
// "key6",
// "key7",
// ]);
// });
// test("fakeKeyMap and fakeKeys do not interact between spies", async (t) => {
// const spy1 = spyFactory(handleOptions());
// const spy2 = spyFactory(handleOptions());
// spy1.prop1;
// spy1.prop2;
// spy2.prop1;
// spy2.prop2;
// t.not(spy1.fakeKeyMap, spy2.fakeKeyMap);
// t.not(spy1.fakeKeys, spy2.fakeKeys);
// });