-
Notifications
You must be signed in to change notification settings - Fork 29
/
chai-spies.js
759 lines (659 loc) · 21.3 KB
/
chai-spies.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, (function() {if (!global.chai) throw new Error("global.chai is not defined");global.chai.use(factory());})());
}(this, (function () { 'use strict';
/*!
* chai-spies :: a chai plugin
* Copyright (c) 2012 Jake Luer <[email protected]>
* MIT Licensed
*/
/*!
* We are going to export a function that can be used through chai
*/
var spy = function (chai, _) {
// Easy access
var Assertion = chai.Assertion
, flag = _.flag
, i = _.inspect
, STATE_KEY = typeof Symbol === 'undefined' ? '__state' : Symbol('state')
, spyAmount = 0
, DEFAULT_SANDBOX = new Sandbox()
, noop = function () {};
/**
* # Sandbox constructor (function)
*
* Initialize new Sandbox instance
*
* @returns new sandbox
* @api private
*/
function Sandbox() {
this[STATE_KEY] = {};
}
/**
* # Sandbox.on (function)
*
* Wraps an object method into spy assigned to sandbox. All calls will
* pass through to the original function.
*
* var spy = chai.spy.sandbox();
* var isArray = spy.on(Array, 'isArray');
*
* const array = []
* const spy = chai.spy.sandbox();
* const [push, pop] = spy.on(array, ['push', 'pop']);
*
* spy.on(array, 'push', returns => 1)
*
* @param {Object} object
* @param {String|String[]} method name or methods names to spy on
* @param {Function} [fn] mock implementation
* @returns created spy or created spies
* @api public
*/
Sandbox.prototype.on = function (object, methodName, fn) {
if (Array.isArray(methodName)) {
return methodName.map(function (name) {
return this.on(object, name, fn);
}, this);
}
var isMethod = typeof object[methodName] === 'function';
if (methodName in object && !isMethod) {
throw new Error([
'Unable to spy property "', methodName,
'". Only methods and non-existing properties can be spied.'
].join(''))
}
if (isMethod && object[methodName].__spy) {
throw new Error('"' + methodName + '" is already a spy')
}
var method = chai.spy('object.' + methodName, fn || object[methodName]);
var trackingId = ++spyAmount;
this[STATE_KEY][trackingId] = method;
method.__spy.tracked = {
object: object
, methodName: methodName
, originalMethod: object[methodName]
, isOwnMethod: Object.hasOwnProperty.call(object, methodName)
};
object[methodName] = method;
return method;
};
/**
* # Sandbox.restore (function)
*
* Restores previously wrapped object's method.
* Restores all spied objects of a sandbox if called without parameters.
*
* var spy = chai.spy.sandbox();
* var object = spy.on(Array, 'isArray');
* spy.restore(Array, 'isArray'); // or spy.restore();
*
* @param {Object} [object]
* @param {String|String[]} [methods] method name or method names
* @return {Sandbox} Sandbox instance
* @api public
*/
Sandbox.prototype.restore = function (object, methods) {
var hasFilter = Boolean(object && methods);
var sandbox = this;
if (methods && !Array.isArray(methods)) {
methods = [methods];
}
Object.keys(this[STATE_KEY]).some(function (spyId) {
var spy = sandbox[STATE_KEY][spyId];
var tracked = spy.__spy.tracked;
var isObjectSpied = !object || object === tracked.object;
var isMethodSpied = !methods || methods.indexOf(tracked.methodName) !== -1;
delete sandbox[STATE_KEY][spyId];
if (!isObjectSpied && !isMethodSpied) {
return false;
}
sandbox.restoreTrackedObject(spy);
if (hasFilter) {
return true;
}
});
return this;
};
/**
* # Sandbox.restoreTrackedObject (function)
*
* Restores tracked object's method
*
* var spy = chai.spy.sandbox();
* var isArray = spy.on(Array, 'isArray');
* spy.restoreTrackedObject(isArray);
*
* @param {Spy} spy
* @api private
*/
Sandbox.prototype.restoreTrackedObject = function (spy) {
var tracked = spy.__spy.tracked;
if (!tracked) {
throw new Error('It is not possible to restore a non-tracked spy.')
}
if (tracked.isOwnMethod) {
tracked.object[tracked.methodName] = tracked.originalMethod;
} else {
delete tracked.object[tracked.methodName];
}
spy.__spy.tracked = null;
};
/**
* # chai.spy (function)
*
* Wraps a function in a proxy function. All calls will
* pass through to the original function.
*
* function original() {}
* var spy = chai.spy(original)
* , e_spy = chai.spy();
*
* @param {Function} function to spy on
* @returns function to actually call
* @api public
*/
chai.spy = function (name, fn) {
if (typeof name === 'function') {
fn = name;
name = undefined;
}
fn = fn || noop;
function makeProxy (length, fn) {
switch (length) {
case 0 : return function () { return fn.apply(this, arguments); };
case 1 : return function (a) { return fn.apply(this, arguments); };
case 2 : return function (a,b) { return fn.apply(this, arguments); };
case 3 : return function (a,b,c) { return fn.apply(this, arguments); };
case 4 : return function (a,b,c,d) { return fn.apply(this, arguments); };
case 5 : return function (a,b,c,d,e) { return fn.apply(this, arguments); };
case 6 : return function (a,b,c,d,e,f) { return fn.apply(this, arguments); };
case 7 : return function (a,b,c,d,e,f,g) { return fn.apply(this, arguments); };
case 8 : return function (a,b,c,d,e,f,g,h) { return fn.apply(this, arguments); };
case 9 : return function (a,b,c,d,e,f,g,h,i) { return fn.apply(this, arguments); };
default : return function (a,b,c,d,e,f,g,h,i,j) { return fn.apply(this, arguments); };
}
}
var proxy = makeProxy(fn.length, function () {
var args = Array.prototype.slice.call(arguments);
proxy.__spy.calls.push(args);
proxy.__spy.called = true;
return fn.apply(this, args);
});
proxy.prototype = fn.prototype;
proxy.toString = function toString() {
var state = this.__spy;
var l = state.calls.length;
var s = "{ Spy";
if (state.name)
s += " '" + state.name + "'";
if (l > 0)
s += ", " + l + " call" + (l > 1 ? 's' : '');
s += " }";
return s + (fn !== noop ? "\n" + fn.toString() : '');
};
proxy.__spy = {
calls: []
, called: false
, name: name
};
return proxy;
};
/**
* # chai.spy.sandbox (function)
*
* Creates sandbox which allow to restore spied objects with spy.on.
* All calls will pass through to the original function.
*
* var spy = chai.spy.sandbox();
* var isArray = spy.on(Array, 'isArray');
*
* @param {Object} object
* @param {String} method name to spy on
* @returns passed object
* @api public
*/
chai.spy.sandbox = function () {
return new Sandbox()
};
/**
* # chai.spy.on (function)
*
* The same as Sandbox.on.
* Assignes newly created spy to DEFAULT sandbox
*
* var isArray = chai.spy.on(Array, 'isArray');
*
* @see Sandbox.on
* @api public
*/
chai.spy.on = function () {
return DEFAULT_SANDBOX.on.apply(DEFAULT_SANDBOX, arguments)
};
/**
* # chai.spy.interface (function)
*
* Creates an object interface with spied methods.
*
* var events = chai.spy.interface('Events', ['trigger', 'on']);
*
* var array = chai.spy.interface({
* push(item) {
* this.items = this.items || [];
* return this.items.push(item);
* }
* });
*
* @param {String|Object} name object or object name
* @param {String[]} [methods] method names
* @returns object with spied methods
* @api public
*/
chai.spy.interface = function (name, methods) {
var defs = {};
if (name && typeof name === 'object') {
methods = Object.keys(name);
defs = name;
name = 'mock';
}
return methods.reduce(function (object, methodName) {
object[methodName] = chai.spy(name + '.' + methodName, defs[methodName]);
return object;
}, {});
};
/**
* # chai.spy.restore (function)
*
* The same as Sandbox.restore.
* Restores spy assigned to DEFAULT sandbox
*
* var array = []
* chai.spy.on(array, 'push');
* expect(array.push).to.be.spy // true
*
* chai.spy.restore()
* expect(array.push).to.be.spy // false
*
* @see Sandbox.restore
* @api public
*/
chai.spy.restore = function () {
return DEFAULT_SANDBOX.restore.apply(DEFAULT_SANDBOX, arguments)
};
/**
* # chai.spy.returns (function)
*
* Creates a spy which returns static value.
*
* var method = chai.spy.returns(true);
*
* @param {*} value static value which is returned by spy
* @returns new spy function which returns static value
* @api public
*/
chai.spy.returns = function (value) {
return chai.spy(function () {
return value;
});
};
/**
* # spy
*
* Assert the the object in question is an chai.spy
* wrapped function by looking for internals.
*
* expect(spy).to.be.spy;
* spy.should.be.spy;
*
* @api public
*/
Assertion.addProperty('spy', function () {
this.assert(
'undefined' !== typeof this._obj.__spy
, 'expected ' + this._obj + ' to be a spy'
, 'expected ' + this._obj + ' to not be a spy');
return this;
});
/**
* # .called
*
* Assert that a spy has been called. Does not negate to allow for
* pass through language.
*
* @api public
*/
function assertCalled (n) {
new Assertion(this._obj).to.be.spy;
var spy = this._obj.__spy;
if (n) {
this.assert(
spy.calls.length === n
, 'expected ' + this._obj + ' to have been called #{exp} but got #{act}'
, 'expected ' + this._obj + ' to have not been called #{exp}'
, n
, spy.calls.length
);
} else {
this.assert(
spy.called === true
, 'expected ' + this._obj + ' to have been called'
, 'expected ' + this._obj + ' to not have been called'
);
}
}
function assertCalledChain () {
new Assertion(this._obj).to.be.spy;
}
Assertion.addChainableMethod('called', assertCalled, assertCalledChain);
/**
* # once
*
* Assert that a spy has been called exactly once
*
* @api public
*/
Assertion.addProperty('once', function () {
new Assertion(this._obj).to.be.spy;
this.assert(
this._obj.__spy.calls.length === 1
, 'expected ' + this._obj + ' to have been called once but got #{act}'
, 'expected ' + this._obj + ' to not have been called once'
, 1
, this._obj.__spy.calls.length );
});
/**
* # twice
*
* Assert that a spy has been called exactly twice.
*
* @api public
*/
Assertion.addProperty('twice', function () {
new Assertion(this._obj).to.be.spy;
this.assert(
this._obj.__spy.calls.length === 2
, 'expected ' + this._obj + ' to have been called twice but got #{act}'
, 'expected ' + this._obj + ' to not have been called twice'
, 2
, this._obj.__spy.calls.length
);
});
/**
* # nth call (spy, n, arguments)
*
* Asserts that the nth call of the spy has been called with
*
*/
function nthCallWith(spy, n, expArgs) {
if (spy.calls.length < n) return false;
var actArgs = spy.calls[n].slice()
, passed = 0;
expArgs.forEach(function (expArg) {
for (var i = 0; i < actArgs.length; i++) {
if (_.eql(actArgs[i], expArg)) {
passed++;
actArgs.splice(i, 1);
break;
}
}
});
return passed === expArgs.length
}
function numberOfCallsWith(spy, expArgs) {
var found = 0
, calls = spy.calls;
for (var i = 0; i < calls.length; i++) {
if (nthCallWith(spy, i, expArgs)) {
found++;
}
}
return found;
}
Assertion.addProperty('first', function () {
if ('undefined' !== this._obj.__spy) {
_.flag(this, 'spy nth call with', 1);
}
});
Assertion.addProperty('second', function () {
if ('undefined' !== this._obj.__spy) {
_.flag(this, 'spy nth call with', 2);
}
});
Assertion.addProperty('third', function () {
if ('undefined' !== this._obj.__spy) {
_.flag(this, 'spy nth call with', 3);
}
});
Assertion.addProperty('on');
Assertion.addChainableMethod('nth', function (n) {
if ('undefined' !== this._obj.__spy) {
_.flag(this, 'spy nth call with', n);
}
});
function generateOrdinalNumber(n) {
if (n === 1) return 'first';
if (n === 2) return 'second';
if (n === 3) return 'third';
return n + 'th';
}
/**
* ### .with
*
*/
function assertWith() {
new Assertion(this._obj).to.be.spy;
var expArgs = [].slice.call(arguments, 0)
, spy = this._obj.__spy
, calls = spy.calls
, always = _.flag(this, 'spy always')
, nthCall = _.flag(this, 'spy nth call with');
if (always) {
var passed = numberOfCallsWith(spy, expArgs);
this.assert(
passed === calls.length
, 'expected ' + this._obj + ' to have been always called with #{exp} but got ' + passed + ' out of ' + calls.length
, 'expected ' + this._obj + ' to have not always been called with #{exp}'
, expArgs
);
} else if (nthCall) {
var ordinalNumber = generateOrdinalNumber(nthCall),
actArgs = calls[nthCall - 1];
new Assertion(this._obj).to.be.have.been.called.min(nthCall);
this.assert(
nthCallWith(spy, nthCall - 1, expArgs)
, 'expected ' + this._obj + ' to have been called at the ' + ordinalNumber + ' time with #{exp} but got #{act}'
, 'expected ' + this._obj + ' to have not been called at the ' + ordinalNumber + ' time with #{exp}'
, expArgs
, actArgs
);
} else {
var passed = numberOfCallsWith(spy, expArgs);
this.assert(
passed > 0
, 'expected ' + this._obj + ' to have been called with #{exp}'
, 'expected ' + this._obj + ' to have not been called with #{exp} but got ' + passed + ' times'
, expArgs
);
}
}
function assertWithChain () {
if ('undefined' !== this._obj.__spy) {
_.flag(this, 'spy with', true);
}
}
Assertion.addChainableMethod('with', assertWith, assertWithChain);
Assertion.addProperty('always', function () {
if ('undefined' !== this._obj.__spy) {
_.flag(this, 'spy always', true);
}
});
/**
* # exactly (n)
*
* Assert that a spy has been called exactly `n` times.
*
* @param {Number} n times
* @api public
*/
Assertion.addMethod('exactly', function () {
new Assertion(this._obj).to.be.spy;
var always = _.flag(this, 'spy always')
, _with = _.flag(this, 'spy with')
, args = [].slice.call(arguments, 0)
, calls = this._obj.__spy.calls
, nthCall = _.flag(this, 'spy nth call with')
, passed;
if (always && _with) {
passed = 0;
calls.forEach(function (call) {
if (call.length !== args.length) return;
if (_.eql(call, args)) passed++;
});
this.assert(
passed === calls.length
, 'expected ' + this._obj + ' to have been always called with exactly #{exp} but got ' + passed + ' out of ' + calls.length
, 'expected ' + this._obj + ' to have not always been called with exactly #{exp}'
, args
);
} else if(_with && nthCall) {
var ordinalNumber = generateOrdinalNumber(nthCall),
actArgs = calls[nthCall - 1];
new Assertion(this._obj).to.be.have.been.called.min(nthCall);
this.assert(
_.eql(actArgs, args)
, 'expected ' + this._obj + ' to have been called at the ' + ordinalNumber + ' time with exactly #{exp} but got #{act}'
, 'expected ' + this._obj + ' to have not been called at the ' + ordinalNumber + ' time with exactly #{exp}'
, args
, actArgs
);
} else if (_with) {
passed = 0;
calls.forEach(function (call) {
if (call.length !== args.length) return;
if (_.eql(call, args)) passed++;
});
this.assert(
passed > 0
, 'expected ' + this._obj + ' to have been called with exactly #{exp}'
, 'expected ' + this._obj + ' to not have been called with exactly #{exp} but got ' + passed + ' times'
, args
);
} else {
this.assert(
this._obj.__spy.calls.length === args[0]
, 'expected ' + this._obj + ' to have been called #{exp} times but got #{act}'
, 'expected ' + this._obj + ' to not have been called #{exp} times'
, args[0]
, this._obj.__spy.calls.length
);
}
});
/**
* # gt (n)
*
* Assert that a spy has been called more than `n` times.
*
* @param {Number} n times
* @api public
*/
function above (_super) {
return function (n) {
if ('undefined' !== typeof this._obj.__spy) {
new Assertion(this._obj).to.be.spy;
this.assert(
this._obj.__spy.calls.length > n
, 'expected ' + this._obj + ' to have been called more than #{exp} times but got #{act}'
, 'expected ' + this._obj + ' to have been called at most #{exp} times but got #{act}'
, n
, this._obj.__spy.calls.length
);
} else {
_super.apply(this, arguments);
}
}
}
Assertion.overwriteMethod('above', above);
Assertion.overwriteMethod('gt', above);
/**
* # lt (n)
*
* Assert that a spy has been called less than `n` times.
*
* @param {Number} n times
* @api public
*/
function below (_super) {
return function (n) {
if ('undefined' !== typeof this._obj.__spy) {
new Assertion(this._obj).to.be.spy;
this.assert(
this._obj.__spy.calls.length < n
, 'expected ' + this._obj + ' to have been called fewer than #{exp} times but got #{act}'
, 'expected ' + this._obj + ' to have been called at least #{exp} times but got #{act}'
, n
, this._obj.__spy.calls.length
);
} else {
_super.apply(this, arguments);
}
}
}
Assertion.overwriteMethod('below', below);
Assertion.overwriteMethod('lt', below);
/**
* # min (n)
*
* Assert that a spy has been called `n` or more times.
*
* @param {Number} n times
* @api public
*/
function min (_super) {
return function (n) {
if ('undefined' !== typeof this._obj.__spy) {
new Assertion(this._obj).to.be.spy;
this.assert(
this._obj.__spy.calls.length >= n
, 'expected ' + this._obj + ' to have been called at least #{exp} times but got #{act}'
, 'expected ' + this._obj + ' to have been called fewer than #{exp} times but got #{act}'
, n
, this._obj.__spy.calls.length
);
} else {
_super.apply(this, arguments);
}
}
}
Assertion.overwriteMethod('min', min);
Assertion.overwriteMethod('least', min);
/**
* # max (n)
*
* Assert that a spy has been called `n` or fewer times.
*
* @param {Number} n times
* @api public
*/
function max (_super) {
return function (n) {
if ('undefined' !== typeof this._obj.__spy) {
new Assertion(this._obj).to.be.spy;
this.assert(
this._obj.__spy.calls.length <= n
, 'expected ' + this._obj + ' to have been called at most #{exp} times but got #{act}'
, 'expected ' + this._obj + ' to have been called more than #{exp} times but got #{act}'
, n
, this._obj.__spy.calls.length
);
} else {
_super.apply(this, arguments);
}
}
}
Assertion.overwriteMethod('max', max);
Assertion.overwriteMethod('most', max);
};
return spy;
})));