-
Notifications
You must be signed in to change notification settings - Fork 31
/
test.js
718 lines (703 loc) · 19 KB
/
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
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
var hooks = require('./hooks')
, should = require('should')
, assert = require('assert')
, _ = require('underscore');
// TODO Add in test for making sure all pres get called if pre is defined directly on an instance.
// TODO Test for calling `done` twice or `next` twice in the same function counts only once
module.exports = {
'should be able to assign multiple hooks at once': function () {
var A = function () {};
_.extend(A, hooks);
A.hook({
hook1: function (a) {},
hook2: function (b) {}
});
var a = new A();
assert.equal(typeof a.hook1, 'function');
assert.equal(typeof a.hook2, 'function');
},
'should run without pres and posts when not present': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('save', function () {
this.value = 1;
});
var a = new A();
a.save();
a.value.should.equal(1);
},
'should run with pres when present': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('save', function () {
this.value = 1;
});
A.pre('save', function (next) {
this.preValue = 2;
next();
});
var a = new A();
a.save();
a.value.should.equal(1);
a.preValue.should.equal(2);
},
'should run with posts when present': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('save', function () {
this.value = 1;
});
A.post('save', function (next) {
this.value = 2;
next();
});
var a = new A();
a.save();
a.value.should.equal(2);
},
'should run pres and posts when present': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('save', function () {
this.value = 1;
});
A.pre('save', function (next) {
this.preValue = 2;
next();
});
A.post('save', function (next) {
this.value = 3;
next();
});
var a = new A();
a.save();
a.value.should.equal(3);
a.preValue.should.equal(2);
},
'should run posts after pres': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('save', function () {
this.value = 1;
});
A.pre('save', function (next) {
this.override = 100;
next();
});
A.post('save', function (next) {
this.override = 200;
next();
});
var a = new A();
a.save();
a.value.should.equal(1);
a.override.should.equal(200);
},
'should not run a hook if a pre fails': function () {
var A = function () {};
_.extend(A, hooks);
var counter = 0;
A.hook('save', function () {
this.value = 1;
}, function (err) {
counter++;
});
A.pre('save', true, function (next, done) {
next(new Error());
});
var a = new A();
a.save();
counter.should.equal(1);
assert.equal(typeof a.value, 'undefined');
},
'should be able to run multiple pres': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('save', function () {
this.value = 1;
});
A.pre('save', function (next) {
this.v1 = 1;
next();
}).pre('save', function (next) {
this.v2 = 2;
next();
});
var a = new A();
a.save();
a.v1.should.equal(1);
a.v2.should.equal(2);
},
'should run multiple pres until a pre fails and not call the hook': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('save', function () {
this.value = 1;
}, function (err) {});
A.pre('save', function (next) {
this.v1 = 1;
next();
}).pre('save', function (next) {
next(new Error());
}).pre('save', function (next) {
this.v3 = 3;
next();
});
var a = new A();
a.save();
a.v1.should.equal(1);
assert.equal(typeof a.v3, 'undefined');
assert.equal(typeof a.value, 'undefined');
},
'should be able to run multiple posts': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('save', function () {
this.value = 1;
});
A.post('save', function (next) {
this.value = 2;
next();
}).post('save', function (next) {
this.value = 3.14;
next();
}).post('save', function (next) {
this.v3 = 3;
next();
});
var a = new A();
a.save();
assert.equal(a.value, 3.14);
assert.equal(a.v3, 3);
},
'should run only posts up until an error': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('save', function () {
this.value = 1;
}, function (err) {});
A.post('save', function (next) {
this.value = 2;
next();
}).post('save', function (next) {
this.value = 3;
next(new Error());
}).post('save', function (next) {
this.value = 4;
next();
});
var a = new A();
a.save();
a.value.should.equal(3);
},
"should fall back first to the hook method's last argument as the error handler if it is a function of arity 1 or 2": function () {
var A = function () {};
_.extend(A, hooks);
var counter = 0;
A.hook('save', function (callback) {
this.value = 1;
});
A.pre('save', true, function (next, done) {
next(new Error());
});
var a = new A();
a.save( function (err) {
if (err instanceof Error) counter++;
});
counter.should.equal(1);
should.deepEqual(undefined, a.value);
},
'should fall back second to the default error handler if specified': function () {
var A = function () {};
_.extend(A, hooks);
var counter = 0;
A.hook('save', function (callback) {
this.value = 1;
}, function (err) {
if (err instanceof Error) counter++;
});
A.pre('save', true, function (next, done) {
next(new Error());
});
var a = new A();
a.save();
counter.should.equal(1);
should.deepEqual(undefined, a.value);
},
'fallback default error handler should scope to the object': function () {
var A = function () {
this.counter = 0;
};
_.extend(A, hooks);
var counter = 0;
A.hook('save', function (callback) {
this.value = 1;
}, function (err) {
if (err instanceof Error) this.counter++;
});
A.pre('save', true, function (next, done) {
next(new Error());
});
var a = new A();
a.save();
a.counter.should.equal(1);
should.deepEqual(undefined, a.value);
},
'should fall back last to throwing the error': function () {
var A = function () {};
_.extend(A, hooks);
var counter = 0;
A.hook('save', function (err) {
if (err instanceof Error) return counter++;
this.value = 1;
});
A.pre('save', true, function (next, done) {
next(new Error());
});
var a = new A();
var didCatch = false;
try {
a.save();
} catch (e) {
didCatch = true;
e.should.be.an.instanceof(Error);
counter.should.equal(0);
assert.equal(typeof a.value, 'undefined');
}
didCatch.should.be.true;
},
"should proceed without mutating arguments if `next(null|undefined)` is called in a serial pre, and the last argument of the target method is a callback with node-like signature function (err, obj) {...} or function (err) {...}": function () {
var A = function () {};
_.extend(A, hooks);
var counter = 0;
A.prototype.save = function (callback) {
this.value = 1;
callback();
};
A.pre('save', function (next) {
next(null);
});
A.pre('save', function (next) {
next(undefined);
});
var a = new A();
a.save( function (err) {
if (err instanceof Error) counter++;
else counter--;
});
counter.should.equal(-1);
a.value.should.eql(1);
},
"should proceed with mutating arguments if `next(null|undefined)` is callback in a serial pre, and the last argument of the target method is not a function": function () {
var A = function () {};
_.extend(A, hooks);
A.prototype.set = function (v) {
this.value = v;
};
A.pre('set', function (next) {
next(undefined);
});
A.pre('set', function (next) {
next(null);
});
var a = new A();
a.set(1);
should.strictEqual(null, a.value);
},
'should not run any posts if a pre fails': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('save', function () {
this.value = 2;
}, function (err) {});
A.pre('save', function (next) {
this.value = 1;
next(new Error());
}).post('save', function (next) {
this.value = 3;
next();
});
var a = new A();
a.save();
a.value.should.equal(1);
},
"can pass the hook's arguments verbatim to pres": function () {
var A = function () {};
_.extend(A, hooks);
A.hook('set', function (path, val) {
this[path] = val;
});
A.pre('set', function (next, path, val) {
path.should.equal('hello');
val.should.equal('world');
next();
});
var a = new A();
a.set('hello', 'world');
a.hello.should.equal('world');
},
// "can pass the hook's arguments as an array to pres": function () {
// // Great for dynamic arity - e.g., slice(...)
// var A = function () {};
// _.extend(A, hooks);
// A.hook('set', function (path, val) {
// this[path] = val;
// });
// A.pre('set', function (next, hello, world) {
// hello.should.equal('hello');
// world.should.equal('world');
// next();
// });
// var a = new A();
// a.set('hello', 'world');
// assert.equal(a.hello, 'world');
// },
"can pass the hook's arguments verbatim to posts": function () {
var A = function () {};
_.extend(A, hooks);
A.hook('set', function (path, val) {
this[path] = val;
});
A.post('set', function (next, path, val) {
path.should.equal('hello');
val.should.equal('world');
next();
});
var a = new A();
a.set('hello', 'world');
assert.equal(a.hello, 'world');
},
// "can pass the hook's arguments as an array to posts": function () {
// var A = function () {};
// _.extend(A, hooks);
// A.hook('set', function (path, val) {
// this[path] = val;
// });
// A.post('set', function (next, halt, args) {
// assert.equal(args[0], 'hello');
// assert.equal(args[1], 'world');
// next();
// });
// var a = new A();
// a.set('hello', 'world');
// assert.equal(a.hello, 'world');
// },
"pres should be able to modify and pass on a modified version of the hook's arguments": function () {
var A = function () {};
_.extend(A, hooks);
A.hook('set', function (path, val) {
this[path] = val;
assert.equal(arguments[2], 'optional');
});
A.pre('set', function (next, path, val) {
next('foo', 'bar');
});
A.pre('set', function (next, path, val) {
assert.equal(path, 'foo');
assert.equal(val, 'bar');
next('rock', 'says', 'optional');
});
A.pre('set', function (next, path, val, opt) {
assert.equal(path, 'rock');
assert.equal(val, 'says');
assert.equal(opt, 'optional');
next();
});
var a = new A();
a.set('hello', 'world');
assert.equal(typeof a.hello, 'undefined');
a.rock.should.equal('says');
},
'posts should see the modified version of arguments if the pres modified them': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('set', function (path, val) {
this[path] = val;
});
A.pre('set', function (next, path, val) {
next('foo', 'bar');
});
A.post('set', function (next, path, val) {
path.should.equal('foo');
val.should.equal('bar');
});
var a = new A();
a.set('hello', 'world');
assert.equal(typeof a.hello, 'undefined');
a.foo.should.equal('bar');
},
'should pad missing arguments (relative to expected arguments of the hook) with null': function () {
// Otherwise, with hookFn = function (a, b, next, ),
// if we use hookFn(a), then because the pre functions are of the form
// preFn = function (a, b, next, ), then it actually gets executed with
// preFn(a, next, ), so when we call next() from within preFn, we are actually
// calling ()
var A = function () {};
_.extend(A, hooks);
A.hook('set', function (path, val, opts) {
this[path] = val;
});
A.pre('set', function (next, path, val, opts) {
next('foo', 'bar');
assert.equal(typeof opts, 'undefined');
});
var a = new A();
a.set('hello', 'world');
},
'should not invoke the target method until all asynchronous middleware have invoked dones': function () {
var counter = 0;
var A = function () {};
_.extend(A, hooks);
A.hook('set', function (path, val) {
counter++;
this[path] = val;
counter.should.equal(7);
});
A.pre('set', function (next, path, val) {
counter++;
next();
});
A.pre('set', true, function (next, done, path, val) {
counter++;
setTimeout(function () {
counter++;
done();
}, 1000);
next();
});
A.pre('set', function (next, path, val) {
counter++;
next();
});
A.pre('set', true, function (next, done, path, val) {
counter++;
setTimeout(function () {
counter++;
done();
}, 500);
next();
});
var a = new A();
a.set('hello', 'world');
},
'invoking a method twice should run its async middleware twice': function () {
var counter = 0;
var A = function () {};
_.extend(A, hooks);
A.hook('set', function (path, val) {
this[path] = val;
if (path === 'hello') counter.should.equal(1);
if (path === 'foo') counter.should.equal(2);
});
A.pre('set', true, function (next, done, path, val) {
setTimeout(function () {
counter++;
done();
}, 1000);
next();
});
var a = new A();
a.set('hello', 'world');
a.set('foo', 'bar');
},
'calling the same done multiple times should have the effect of only calling it once': function () {
var A = function () {
this.acked = false;
};
_.extend(A, hooks);
A.hook('ack', function () {
console.log("UH OH, YOU SHOULD NOT BE SEEING THIS");
this.acked = true;
});
A.pre('ack', true, function (next, done) {
next();
done();
done();
});
A.pre('ack', true, function (next, done) {
next();
// Notice that done() is not invoked here
});
var a = new A();
a.ack();
setTimeout( function () {
a.acked.should.be.false;
}, 1000);
},
'calling the same next multiple times should have the effect of only calling it once': function (beforeExit) {
var A = function () {
this.acked = false;
};
_.extend(A, hooks);
A.hook('ack', function () {
console.log("UH OH, YOU SHOULD NOT BE SEEING THIS");
this.acked = true;
});
A.pre('ack', function (next) {
// force a throw to re-exec next()
try {
next(new Error('bam'));
} catch (err) {
next();
}
});
A.pre('ack', function (next) {
next();
});
var a = new A();
a.ack();
beforeExit( function () {
a.acked.should.be.false;
});
},
'asynchronous middleware should be able to pass an error via `done`, stopping the middleware chain': function () {
var counter = 0;
var A = function () {};
_.extend(A, hooks);
A.hook('set', function (path, val, fn) {
counter++;
this[path] = val;
fn(null);
});
A.pre('set', true, function (next, done, path, val, fn) {
setTimeout(function () {
counter++;
done(new Error);
}, 1000);
next();
});
var a = new A();
a.set('hello', 'world', function (err) {
err.should.be.an.instanceof(Error);
should.strictEqual(undefined, a['hello']);
counter.should.eql(1);
});
},
'should be able to remove a particular pre': function () {
var A = function () {}
, preTwo;
_.extend(A, hooks);
A.hook('save', function () {
this.value = 1;
});
A.pre('save', function (next) {
this.preValueOne = 2;
next();
});
A.pre('save', preTwo = function (next) {
this.preValueTwo = 4;
next();
});
A.removePre('save', preTwo);
var a = new A();
a.save();
a.value.should.equal(1);
a.preValueOne.should.equal(2);
should.strictEqual(undefined, a.preValueTwo);
},
'should be able to remove all pres associated with a hook': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('save', function () {
this.value = 1;
});
A.pre('save', function (next) {
this.preValueOne = 2;
next();
});
A.pre('save', function (next) {
this.preValueTwo = 4;
next();
});
A.removePre('save');
var a = new A();
a.save();
a.value.should.equal(1);
should.strictEqual(undefined, a.preValueOne);
should.strictEqual(undefined, a.preValueTwo);
},
'#pre should lazily make a method hookable': function () {
var A = function () {};
_.extend(A, hooks);
A.prototype.save = function () {
this.value = 1;
};
A.pre('save', function (next) {
this.preValue = 2;
next();
});
var a = new A();
a.save();
a.value.should.equal(1);
a.preValue.should.equal(2);
},
'#pre lazily making a method hookable should be able to provide a default errorHandler as the last argument': function () {
var A = function () {};
var preValue = "";
_.extend(A, hooks);
A.prototype.save = function () {
this.value = 1;
};
A.pre('save', function (next) {
next(new Error);
}, function (err) {
preValue = 'ERROR';
});
var a = new A();
a.save();
should.strictEqual(undefined, a.value);
preValue.should.equal('ERROR');
},
'#post should lazily make a method hookable': function () {
var A = function () {};
_.extend(A, hooks);
A.prototype.save = function () {
this.value = 1;
};
A.post('save', function (next) {
this.value = 2;
next();
});
var a = new A();
a.save();
a.value.should.equal(2);
},
"a lazy hooks setup should handle errors via a method's last argument, if it's a callback": function () {
var A = function () {};
_.extend(A, hooks);
A.prototype.save = function (fn) {};
A.pre('save', function (next) {
next(new Error("hi there"));
});
var a = new A();
a.save( function (err) {
err.should.be.an.instanceof(Error);
});
},
'should intercept method callbacks for post handlers': function () {
var A = function () {};
_.extend(A, hooks);
A.hook('save', function (val, callback) {
this.value = val;
callback();
});
A.post('save', function (next) {
assert.equal(a.value, 2);
this.value += 2;
setTimeout(next, 10);
}).post('save', function (next) {
assert.equal(a.value, 4);
this.value += 3;
setTimeout(next, 10);
}).post('save', function (next) {
assert.equal(a.value, 7);
this.value2 = 3;
setTimeout(next, 10);
});
var a = new A();
a.save(2, function(){
assert.equal(a.value, 7);
assert.equal(a.value2, 3);
});
}
};