UNPKG

22.6 kBJavaScriptView Raw
1if (!chai) {
2 var chai = require('chai')
3 , spies = require('..');
4 chai.use(spies);
5}
6
7var should = chai.should();
8
9describe('Chai Spies', function () {
10
11 describe('name', function() {
12 it('defaults to undefined', function() {
13 chai.expect(chai.spy().__spy.name).to.equal(undefined);
14 });
15
16 it('exposes the name', function() {
17 chai.expect(chai.spy('007').__spy.name).to.equal('007');
18 });
19
20 it('executes the function sent to the spy', function() {
21 var spy = chai.spy()
22 chai.spy('007', spy)();
23 spy.should.have.been.called.once
24 });
25 });
26
27 describe('textual representation', function() {
28
29 it('should print out nice', function() {
30 chai.spy().toString().should.equal("{ Spy }");
31 });
32
33 it('should show the name', function() {
34 chai.spy('Nikita').toString().should.equal("{ Spy 'Nikita' }");
35 });
36
37 it('should expose number of invokations', function() {
38 var spy = chai.spy()
39 spy(); // 1
40 spy(); // 2
41 spy.toString().should.equal("{ Spy, 2 calls }");
42 });
43
44 it('should expose name and number of invokations', function() {
45 var spy = chai.spy('Nikita')
46 spy(); // 1
47 spy.toString().should.equal("{ Spy 'Nikita', 1 call }");
48 });
49
50 it('should expose original function `toString` representation', function() {
51 function test(a, b, c) {
52 return a + b + c;
53 }
54
55 var spy = chai.spy(test);
56 spy.toString().should.equal("{ Spy }\n" + test.toString());
57 });
58 });
59
60 it('should return the value of the mock function', function() {
61 var spy = chai.spy(function() { return 'Jack Bauer'; });
62 var jack = spy();
63 chai.expect(jack).to.equal('Jack Bauer');
64 });
65
66 it('should invoke the function sent to the spy', function() {
67 var spy = chai.spy()
68 chai.spy(spy)()
69 spy.should.have.been.called.once
70 });
71
72 it('should know when obj is a spy', function () {
73 var spy = chai.spy();
74 spy.should.be.spy;
75
76 (function () {
77 'hello'.should.be.a.spy;
78 }).should.throw(chai.AssertionError);
79 });
80
81 it('should know when a spy has been called', function () {
82 var spy = chai.spy();
83 spy.should.be.spy;
84 spy.__spy.called.should.be.false;
85 spy();
86 spy.should.have.been.called();
87 (function () {
88 spy.should.have.not.been.called();
89 }).should.throw(chai.AssertionError);
90 });
91
92 it('should know when a spy has not been called', function () {
93 var spy = chai.spy();
94 spy.should.be.spy;
95 spy.should.have.not.been.called();
96 (function () {
97 spy.should.have.been.called();
98 }).should.throw(chai.AssertionError);
99 });
100
101 it('should know when a spy has been called once', function () {
102 var spy1 = chai.spy()
103 , spy2 = chai.spy();
104 spy1();
105 spy2();
106 spy2();
107 spy1.should.have.been.called.once;
108
109 (function () {
110 spy2.should.have.been.called.once;
111 }).should.throw(chai.AssertionError);
112
113 (function () {
114 spy1.should.have.not.been.called.once;
115 }).should.throw(chai.AssertionError);
116 });
117
118 it('should know when a spy has been called twice', function () {
119 var spy1 = chai.spy()
120 , spy2 = chai.spy();
121 spy1();
122 spy1();
123 spy2();
124 spy2();
125 spy2();
126 spy1.should.have.been.called.twice;
127 (function () {
128 spy2.should.have.been.called.twice;
129 }).should.throw(chai.AssertionError);
130 (function () {
131 spy1.should.have.not.been.called.twice;
132 }).should.throw(chai.AssertionError);
133 });
134
135 it('should know when a spy has been called exactly n times', function () {
136 var spy1 = chai.spy();
137 spy1();
138 spy1.should.have.been.called.exactly(1);
139 (function () {
140 spy1.should.have.been.called.exactly(2);
141 }).should.throw(chai.AssertionError);
142 (function () {
143 spy1.should.not.have.been.called.exactly(1);
144 }).should.throw(chai.AssertionError);
145 });
146
147 it('should know when a spy has been called above n times', function () {
148 var spy = chai.spy();
149 spy();
150 spy();
151 spy.should.have.been.called.above(1);
152 spy.should.have.been.called.gt(0);
153 (2).should.be.above(1);
154 (2).should.be.gt(1);
155 (function () {
156 spy.should.have.been.called.above(2);
157 }).should.throw(chai.AssertionError);
158 (function () {
159 spy.should.not.have.been.called.above(1);
160 }).should.throw(chai.AssertionError);
161 });
162
163 it('should know when a spy has been called below n times', function () {
164 var spy = chai.spy();
165 spy();
166 spy();
167 spy();
168 spy.should.have.been.called.below(4);
169 spy.should.have.not.been.called.lt(3);
170 (1).should.be.below(2);
171 (1).should.be.lt(2);
172 (function () {
173 spy.should.have.been.called.below(2);
174 }).should.throw(chai.AssertionError);
175 (function () {
176 spy.should.not.have.been.called.below(4);
177 }).should.throw(chai.AssertionError);
178 });
179
180 it('should know when a spy has been called at least n times', function () {
181 var spy = chai.spy();
182 spy();
183 spy();
184 spy.should.have.been.called.min(2);
185 spy.should.have.been.called.at.least(1);
186 (2).should.be.at.least(2);
187 (2).should.be.at.least(1);
188 (function () {
189 spy.should.have.been.called.min(3);
190 }).should.throw(chai.AssertionError);
191 (function () {
192 spy.should.not.have.been.called.above(1);
193 }).should.throw(chai.AssertionError);
194 });
195
196 it('should know when a spy has been called at most n times', function () {
197 var spy = chai.spy();
198 spy();
199 spy();
200 spy();
201 spy.should.have.been.called.max(3);
202 spy.should.have.been.called.at.most(4);
203 (1).should.be.at.most(3);
204 (1).should.be.at.most(4);
205 (function () {
206 spy.should.have.been.called.max(2);
207 }).should.throw(chai.AssertionError);
208 (function () {
209 spy.should.not.have.been.called.at.most(3);
210 }).should.throw(chai.AssertionError);
211 });
212
213 it('should understand length', function () {
214 var orig = function (a, b) {}
215 , spy = chai.spy(orig)
216 , spyClean = chai.spy();
217 orig.should.have.length(2);
218 spy.should.have.length(2);
219 spyClean.should.have.length(0);
220 });
221
222 it('should create spy which returns static value', function() {
223 var value = {};
224 var spy = chai.spy.returns(value);
225
226 spy.should.be.a.spy;
227 spy().should.equal(value);
228 });
229
230 describe('.with', function () {
231 it('should not interfere chai with' ,function () {
232 (1).should.be.with.a('number');
233 });
234 });
235
236 describe('.with(arg, ...)', function () {
237 it('should pass when called with an argument', function () {
238 var spy = chai.spy();
239 spy(1);
240 spy(2);
241 spy(3);
242 spy.should.have.been.called.with(1);
243 spy.should.have.been.called.with(2);
244 spy.should.have.been.called.with(3);
245 spy.should.not.have.been.called.with(4);
246 (function () {
247 spy.should.have.been.called.with(4);
248 }).should.throw(chai.AssertionError, /have been called with/);
249 (function () {
250 spy.should.have.not.been.called.with(1);
251 }).should.throw(chai.AssertionError, /have not been called with/);
252 });
253
254 it('should pass with called with multiple arguments', function () {
255 var spy = chai.spy();
256 spy(1,2,3);
257 spy(2,4,6);
258 spy(3,6,9);
259 spy.should.have.been.called.with(1,2);
260 spy.should.have.been.called.with(2,4);
261 spy.should.have.been.called.with(3,6);
262 spy.should.have.been.called.with(3,1,2);
263 spy.should.have.been.called.with(6,2,4);
264 spy.should.have.been.called.with(9,3,6);
265 spy.should.not.have.been.called.with(5);
266 spy.should.not.have.been.called.with(1,9);
267 spy.should.not.have.been.called.with(9,1,4);
268 (function () {
269 spy.should.have.been.called.with(1,2,5);
270 }).should.throw(chai.AssertionError, /have been called with/);
271 (function () {
272 spy.should.have.not.been.called.with(3,6,9);
273 }).should.throw(chai.AssertionError, /have not been called with/);
274 });
275
276 it('should pass when called with multiple identical arguments', function () {
277 var spy = chai.spy();
278 spy(1, 1);
279 spy.should.have.been.called.with(1);
280 spy.should.have.been.called.with(1, 1);
281 spy.should.not.have.been.called.with(1, 2);
282 spy.should.not.have.been.called.with(1, 1, 1);
283 });
284 });
285
286 describe('.first.called.with(arg, ...)', function() {
287 it('should pass only when called with the arguments the first time', function() {
288 var spy = chai.spy();
289 spy(1, 2, 3)
290 spy(3, 4, 5)
291 spy.should.have.been.first.called.with(3, 2, 1);
292 spy.should.have.been.first.called.with(1, 2, 3)
293 spy.should.have.been.first.called.with(1, 2);
294 spy.should.not.have.been.first.called.with(4);
295 (function () {
296 spy.should.have.been.first.called.with(1, 2, 4)
297 }).should.throw(chai.AssertionError, /have been called at the first time with/);
298 (function () {
299 spy.should.have.not.been.first.called.with(1, 2);
300 }).should.throw(chai.AssertionError, /have not been called at the first time with/);
301 });
302 });
303
304 describe('.second.called.with(arg, ...)', function() {
305 it('should pass only when called with the arguments the second time', function() {
306 var spy = chai.spy();
307 spy(1, 2, 3)
308 spy(3, 4, 5)
309 spy.should.have.been.second.called.with(3, 4, 5)
310 spy.should.have.been.second.called.with(4, 5);
311 spy.should.not.have.been.second.called.with(1);
312 (function () {
313 spy.should.have.been.second.called.with(3, 4, 1)
314 }).should.throw(chai.AssertionError, /have been called at the second time with/);
315 (function () {
316 spy.should.have.not.been.second.called.with(4, 5);
317 }).should.throw(chai.AssertionError, /have not been called at the second time with/);
318 });
319 });
320
321 describe('.third.called.with(arg, ...)', function() {
322 it('should pass only when called with the arguments the third time', function() {
323 var spy = chai.spy();
324 spy(1, 2, 3)
325 spy(3, 4, 5)
326 spy(5, 6, 7)
327 spy.should.have.been.third.called.with(5, 6, 7)
328 spy.should.have.been.third.called.with(6, 5);
329 spy.should.not.have.been.third.called.with(1);
330 (function () {
331 spy.should.have.been.third.called.with(5, 6, 1)
332 }).should.throw(chai.AssertionError, /have been called at the third time with/);
333 (function () {
334 spy.should.have.not.been.third.called.with(6, 5);
335 }).should.throw(chai.AssertionError, /have not been called at the third time with/);
336 });
337 });
338
339 describe('.nth(n).called.with(arg, ...)', function() {
340 it('should pass only when called with the arguments the nth time its called', function() {
341 var spy = chai.spy();
342 spy(0);
343 spy(1);
344 spy(2);
345 spy(3);
346 spy(4, 6, 7);
347 spy(5, 8, 9);
348 spy.should.on.nth(5).be.called.with(4);
349 spy.should.on.nth(6).be.called.with(8, 5);
350 spy.should.not.on.nth(5).be.called.with(3, 4);
351 (function () {
352 spy.should.on.nth(5).be.called.with(3);
353 }).should.throw(chai.AssertionError, /have been called at the 5th time with/);
354 (function () {
355 spy.should.not.on.nth(6).be.called.with(5);
356 }).should.throw(chai.AssertionError, /have not been called at the 6th time with/);
357 (function () {
358 spy.should.on.nth(7).be.called.with(10);
359 }).should.throw(chai.AssertionError, /to have been called at least 7 times but got 6/);
360 });
361 });
362
363 describe('.always.with(arg, ...)', function () {
364 it('should pass when called with an argument', function () {
365 var spy = chai.spy();
366 spy(1);
367 spy(1, 2);
368 spy(3, 1);
369 spy(4, 5, 1);
370 spy.should.have.been.always.called.with(1);
371 spy.should.not.always.have.been.called.with(2);
372 spy.should.not.always.have.been.called.with(8);
373 (function () {
374 spy.should.have.been.always.called.with(2);
375 }).should.throw(chai.AssertionError, /to have been always called with/);
376 (function () {
377 spy.should.not.have.been.always.called.with(1);
378 }).should.throw(chai.AssertionError, /to have not always been called with/);
379 });
380
381 it('should pass when called with multiple arguments', function () {
382 var spy = chai.spy();
383 spy(1,2);
384 spy(2,1);
385 spy(1,3,2);
386 spy(2,5,1);
387 spy.should.have.been.always.called.with(1,2);
388 spy.should.not.always.have.been.called.with(2,3);
389 spy.should.not.always.have.been.called.with(4,6);
390 (function () {
391 spy.should.have.been.always.called.with(2,3);
392 }).should.throw(chai.AssertionError, /to have been always called with/);
393 (function () {
394 spy.should.not.have.been.always.called.with(1,2);
395 }).should.throw(chai.AssertionError, /to have not always been called with/);
396 });
397
398 it('should pass when called with multiple identical arguments', function () {
399 var spy = chai.spy();
400 spy(1, 3, 1);
401 spy(1, 2, 1);
402 spy.should.have.always.been.called.with(1);
403 spy.should.have.always.been.called.with(1, 1);
404 spy.should.not.have.always.been.called.with(1, 2);
405 spy.should.not.have.always.been.called.with(1, 1, 1);
406 });
407 });
408
409 describe('.with.exactly(arg, ...)', function () {
410 it('should pass when called with an argument', function () {
411 var spy = chai.spy();
412 spy(1);
413 spy(1, 2);
414 spy.should.have.been.called.with.exactly(1);
415 spy.should.have.not.been.called.with.exactly(2);
416 (function () {
417 spy.should.have.been.called.with.exactly(2);
418 }).should.throw(chai.AssertionError, /to have been called with exactly/);
419 (function () {
420 spy.should.have.not.been.called.with.exactly(1);
421 }).should.throw(chai.AssertionError, /to not have been called with exactly/);
422 });
423
424 it('shoud pass when called with multiple arguments', function () {
425 var spy = chai.spy();
426 spy(1);
427 spy(3, 2);
428 spy.should.have.been.called.with.exactly(3,2);
429 spy.should.have.not.been.called.with.exactly(2,3);
430 (function () {
431 spy.should.have.been.called.with.exactly(2,3);
432 }).should.throw(chai.AssertionError, /to have been called with exactly/);
433 (function () {
434 spy.should.have.not.been.called.with.exactly(3,2);
435 }).should.throw(chai.AssertionError, /to not have been called with exactly/);
436 });
437
438 it('should pass when called with multiple identical arguments', function () {
439 var spy = chai.spy();
440 spy(1, 1);
441 spy.should.have.been.called.with.exactly(1, 1);
442 spy.should.not.have.been.called.with.exactly(1);
443 spy.should.not.have.been.called.with.exactly(1, 2);
444 spy.should.not.have.been.called.with.exactly(1, 1, 1);
445 });
446 });
447
448 describe('.nth(...).with.exactly(arg, ...)', function () {
449 it('Should work with the shorthand first for nth(1)', function() {
450 var spy = chai.spy();
451 spy(1, 2, 3);
452 spy(3, 4, 5);
453 spy.should.have.been.first.called.with.exactly(1, 2, 3);
454 spy.should.have.been.not.first.called.with.exactly(3, 4, 5);
455 spy.should.have.been.not.first.called.with.exactly(3);
456 (function() {
457 spy.should.have.been.first.called.with.exactly(3)
458 }).should.throw(chai.AssertionError);
459 (function() {
460 spy.should.have.not.been.first.called.with.exactly(1, 2, 3)
461 }).should.throw(chai.AssertionError);
462 });
463 it('Should work with the shorthand second for nth(2)', function() {
464 var spy = chai.spy();
465 spy(1, 2, 3);
466 spy(3, 4, 5);
467 spy.should.have.been.second.called.with.exactly(3, 4, 5);
468 spy.should.have.been.not.second.called.with.exactly(1, 2, 3);
469 spy.should.have.been.not.second.called.with.exactly(4);
470 (function() {
471 spy.should.have.been.second.called.with.exactly(4, 5)
472 }).should.throw(chai.AssertionError);
473 (function() {
474 spy.should.have.not.been.second.called.with.exactly(3, 4, 5)
475 }).should.throw(chai.AssertionError);
476 });
477 it('Should work with the shorthand third for nth(3)', function() {
478 var spy = chai.spy();
479 spy(1, 2, 3);
480 spy(3, 4, 5);
481 spy(5, 6, 7);
482 spy.should.have.been.third.called.with.exactly(5, 6, 7);
483 spy.should.have.been.not.third.called.with.exactly(5);
484 spy.should.have.been.not.third.called.with.exactly(6, 5, 7);
485 (function() {
486 spy.should.have.been.third.called.with.exactly(7, 6, 5)
487 }).should.throw(chai.AssertionError);
488 (function() {
489 spy.should.have.not.been.third.called.with.exactly(5, 6, 7)
490 }).should.throw(chai.AssertionError);
491 });
492 it('Should work with general nth(...) flag', function() {
493 var spy = chai.spy();
494 spy(1, 2, 3);
495 spy(3, 4, 5);
496 spy(5, 6, 7);
497 spy(7, 8, 9);
498 spy.should.on.nth(4).be.called.with.exactly(7, 8, 9);
499 spy.should.not.on.nth(4).be.called.with.exactly(9, 8, 7);
500 spy.should.not.on.nth(4).be.called.with.exactly(7, 8);
501 (function() {
502 spy.should.on.nth(4).be.called.with.exactly(7, 6, 5);
503 }).should.throw(chai.AssertionError);
504 (function() {
505 spy.should.not.on.nth(4).be.called.with.exactly(7, 8, 9);
506 }).should.throw(chai.AssertionError);
507 });
508 });
509
510 describe('.always.with.exactly(arg, ...)', function () {
511 it('should pass when called with an argument', function () {
512 var spy = chai.spy();
513 spy(3);
514 spy(3);
515 spy.should.have.always.been.called.with.exactly(3);
516
517 var spy2 = chai.spy();
518 spy2(3);
519 spy2(4);
520 spy2.should.have.not.always.been.called.with.exactly(3);
521
522 (function () {
523 spy2.should.have.been.always.called.with.exactly(3);
524 }).should.throw(chai.AssertionError, /to have been always called with exactly/);
525 });
526
527 it('should pass when called with multiple arguments', function () {
528 var spy = chai.spy();
529 spy(3,4);
530 spy(3,4);
531 spy.should.have.always.been.called.with.exactly(3,4);
532
533 var spy2 = chai.spy();
534 spy2(3);
535 spy2(4,4);
536 spy2.should.have.not.always.been.called.with.exactly(4,4);
537
538 (function () {
539 spy2.should.have.been.always.called.with.exactly(4,4);
540 }).should.throw(chai.AssertionError, /to have been always called with exactly/);
541 });
542
543 it('should pass when called with multiple identical arguments', function () {
544 var spy = chai.spy();
545 spy(1, 1);
546 spy(1, 1);
547 spy.should.have.always.been.called.with.exactly(1, 1);
548 spy.should.not.have.always.been.called.with.exactly(1);
549 spy.should.not.have.always.been.called.with.exactly(1, 2);
550 spy.should.not.have.always.been.called.with.exactly(1, 1, 1);
551 });
552 });
553
554 describe('spy on', function () {
555 var object;
556
557 beforeEach(function () {
558 object = []
559 });
560
561 it('should spy specified object method', function () {
562 chai.spy.on(object, 'push');
563 object.push(1, 2);
564
565 object.push.should.be.a.spy;
566 object.should.have.length(2);
567 });
568
569 it('should spy multiple object methods', function () {
570 chai.spy.on(object, ['push', 'pop']);
571
572 object.push.should.be.a.spy;
573 object.pop.should.be.a.spy;
574 });
575
576 it('should allow to create spy for non-existing property', function () {
577 chai.spy.on(object, 'nonExistingProperty');
578
579 object.nonExistingProperty.should.be.a.spy;
580 });
581
582 it('should throw if non function property is passed', function () {
583 (function () {
584 chai.spy.on(object, 'length');
585 }).should.throw(Error);
586 });
587
588 it('should throw if method is already a spy', function () {
589 object.push = chai.spy();
590
591 (function () {
592 chai.spy.on(object, 'push');
593 }).should.throw(Error)
594 });
595
596 it('should allow to overwrite method implementation', function () {
597 chai.spy.on(object, 'push', function() {
598 return 5;
599 });
600
601 object.push().should.equal(5);
602 });
603
604 it('should overwrite all methods with the same implementation', function () {
605 chai.spy.on(object, ['push', 'pop'], function() {
606 return 5;
607 });
608
609 object.push().should.equal(5);
610 object.pop().should.equal(5);
611 })
612
613 it('should work with a nullish prototype', function() {
614 var nullish = Object.create(null);
615 nullish.method = function() {
616 return 1;
617 };
618 chai.spy.on(nullish, 'method');
619 nullish.method().should.equal(1);
620 });
621
622 });
623
624 describe('spy interface', function () {
625
626 it('should create a spy object with specified method names', function () {
627 var array = chai.spy.interface('array', ['push', 'pop']);
628
629 array.push.should.be.a.spy;
630 array.pop.should.be.a.spy;
631 });
632
633 it('should wrap each method in spy', function () {
634 var array = [];
635 var object = chai.spy.interface({
636 push: function() {
637 return array.push.apply(array, arguments);
638 }
639 });
640
641 object.push(1, 2, 3);
642
643 object.push.should.be.a.spy;
644 array.should.have.length(3);
645 });
646
647 it('should return value from spied method', function () {
648 var object = chai.spy.interface({
649 push: function () {
650 return 'push';
651 }
652 });
653
654 object.push().should.equal('push');
655 });
656
657 it('should create a plain object', function () {
658 var object = chai.spy.interface('Object', ['method']);
659
660 object.should.be.an('object');
661 });
662 });
663
664 describe('spy restore', function () {
665 var array, array2;
666
667 beforeEach(function () {
668 array = [];
669 array2 = [];
670 chai.spy.on(array2, 'push');
671 chai.spy.on(array, 'shift');
672 chai.spy.on(array, 'push');
673 });
674
675 afterEach(function () {
676 chai.spy.restore();
677 });
678
679 it('should restore all methods of tracked objects', function () {
680 chai.spy.restore();
681
682 array.shift.should.not.be.spy;
683 array.push.should.not.be.spy;
684 array2.push.should.not.be.spy;
685 });
686
687 it('should restore all methods on an object', function () {
688 chai.spy.on(array, 'pop');
689 chai.spy.restore(array);
690
691 array.shift.should.not.be.spy;
692 array.push.should.not.be.spy;
693 array.pop.should.not.be.spy;
694 array2.push.should.be.spy;
695 });
696
697 it('should restore a particular method on an particular object', function () {
698 chai.spy.on(array, 'pop');
699 chai.spy.restore(array, 'push');
700
701 array.push.should.not.be.spy;
702 array.pop.should.be.spy;
703 array.shift.should.be.spy;
704 array2.push.should.be.spy;
705 });
706
707 it('should not throw if there are not tracked objects', function () {
708 chai.spy.restore();
709
710 chai.spy.restore.should.not.throw(Error);
711 });
712 });
713});