basic-test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. var test = require("tap").test, hooks, Creature, larry;
  2. test("load the prototype-hooks module", function (t) {
  3. hooks = require('../');
  4. t.equal(typeof hooks, 'function', 'required hooks object')
  5. t.pass('loaded prototype-hooks module');
  6. t.end();
  7. });
  8. test("apply hooks to prototype of existing class", function (t) {
  9. Creature = function (opts) {
  10. this.name = opts.name;
  11. };
  12. Creature.prototype.talk = function (data, cb) {
  13. cb(null, this.name + ' says ' + data.text);
  14. };
  15. Creature.prototype.errTalk = function (data, cb) {
  16. cb(new Error('talking error'));
  17. };
  18. hooks(Creature);
  19. t.equal(typeof Creature.prototype.before, 'function', 'found new Creature.prototype.before function');
  20. t.equal(typeof Creature.prototype.after, 'function', 'found new Creature.prototype.after function');
  21. t.equal(Array.isArray(Creature.prototype.talk.before), true, 'found before array');
  22. t.equal(Array.isArray(Creature.prototype.talk.after), true, 'found after array');
  23. t.equal(Creature.prototype.talk.before.length, 0, 'no items in before array');
  24. t.equal(Creature.prototype.talk.after.length, 0, 'no items in after array');
  25. t.pass('applied hooks to prototype');
  26. t.end();
  27. });
  28. test("attempt to add before and after hooks to Creature.talk", function (t) {
  29. larry = new Creature({ name: "Larry" });
  30. larry.before('talk', function(data, next) {
  31. data.text = data.text + "!";
  32. next(null, data);
  33. });
  34. larry.after('talk', function(text, next) {
  35. text = text + ' ... ';
  36. next(null, text);
  37. });
  38. t.equal(larry.talk.before.length, 1, '1 item in before array');
  39. t.equal(larry.talk.after.length, 1, '1 item in after array');
  40. t.end();
  41. });
  42. test("attempt to fire talk with basic before and after hooks", function (t) {
  43. larry.talk({ text: 'hi'}, function (err, result){
  44. t.error(err, 'did not error');
  45. t.equal(result, 'Larry says hi! ... ');
  46. t.pass('fired talk method');
  47. t.end();
  48. })
  49. });
  50. test("attempt to add error returning before hooks to Creature.talk", function (t) {
  51. larry.before('talk', function(data, next) {
  52. next(new Error('forced before error'));
  53. });
  54. larry.talk({ text: 'hi'}, function (err, result){
  55. console.log('eee', err, result)
  56. t.equal(typeof err, 'object', 'found error object');
  57. t.equal(err.message, 'forced before error');
  58. t.pass('errored on talk method');
  59. t.end();
  60. })
  61. });
  62. test("attempt to add error returning after hooks to Creature.talk", function (t) {
  63. var david = new Creature({ name: "David" });
  64. david.after('talk', function(text, next) {
  65. next(new Error(text.message + ' forced after error'));
  66. });
  67. david.talk({ text: 'hi'}, function (err, result){
  68. console.log('eee', err, result)
  69. t.equal(typeof err, 'object', 'found error object');
  70. t.equal(err.message, 'forced before error');
  71. t.pass('errored on talk method');
  72. t.end();
  73. })
  74. });
  75. test("return an in error in Creature.talk", function (t) {
  76. larry.after('talk', function(text, next) {
  77. next(new Error(text.message + ' forced after error'));
  78. });
  79. larry.errTalk({ text: 'hi'}, function (err, result){
  80. t.equal(typeof err, 'object', 'found error object');
  81. t.equal(err.message, 'talking error');
  82. t.pass('errored on talk method');
  83. t.end();
  84. })
  85. });