post.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. 'use strict';
  2. const assert = require('assert');
  3. const Kareem = require('../');
  4. describe('execPost', function() {
  5. var hooks;
  6. beforeEach(function() {
  7. hooks = new Kareem();
  8. });
  9. it('handles errors', function(done) {
  10. hooks.post('cook', function(eggs, callback) {
  11. callback('error!');
  12. });
  13. hooks.execPost('cook', null, [4], function(error, eggs) {
  14. assert.equal('error!', error);
  15. assert.ok(!eggs);
  16. done();
  17. });
  18. });
  19. it('unshift', function() {
  20. var f1 = function() {};
  21. var f2 = function() {};
  22. hooks.post('cook', f1);
  23. hooks.post('cook', f2, true);
  24. assert.strictEqual(hooks._posts.get('cook')[0].fn, f2);
  25. assert.strictEqual(hooks._posts.get('cook')[1].fn, f1);
  26. });
  27. it('arbitrary options', function() {
  28. const f1 = function() {};
  29. const f2 = function() {};
  30. hooks.post('cook', { foo: 'bar' }, f1);
  31. hooks.post('cook', { bar: 'baz' }, f2, true);
  32. assert.equal(hooks._posts.get('cook')[1].foo, 'bar');
  33. assert.equal(hooks._posts.get('cook')[0].bar, 'baz');
  34. });
  35. it('throws error if no function', function() {
  36. assert.throws(() => hooks.post('test'), /got "undefined"/);
  37. });
  38. it('multiple posts', function(done) {
  39. hooks.post('cook', function(eggs, callback) {
  40. setTimeout(
  41. function() {
  42. callback();
  43. },
  44. 5);
  45. });
  46. hooks.post('cook', function(eggs, callback) {
  47. setTimeout(
  48. function() {
  49. callback();
  50. },
  51. 5);
  52. });
  53. hooks.execPost('cook', null, [4], function(error, eggs) {
  54. assert.ifError(error);
  55. assert.equal(4, eggs);
  56. done();
  57. });
  58. });
  59. it('error posts', function(done) {
  60. var called = {};
  61. hooks.post('cook', function(eggs, callback) {
  62. called.first = true;
  63. callback();
  64. });
  65. hooks.post('cook', function(eggs, callback) {
  66. called.second = true;
  67. callback(new Error('fail'));
  68. });
  69. hooks.post('cook', function(eggs, callback) {
  70. assert.ok(false);
  71. });
  72. hooks.post('cook', function(error, eggs, callback) {
  73. called.fourth = true;
  74. assert.equal(error.message, 'fail');
  75. callback(new Error('fourth'));
  76. });
  77. hooks.post('cook', function(error, eggs, callback) {
  78. called.fifth = true;
  79. assert.equal(error.message, 'fourth');
  80. callback(new Error('fifth'));
  81. });
  82. hooks.execPost('cook', null, [4], function(error, eggs) {
  83. assert.ok(error);
  84. assert.equal(error.message, 'fifth');
  85. assert.deepEqual(called, {
  86. first: true,
  87. second: true,
  88. fourth: true,
  89. fifth: true
  90. });
  91. done();
  92. });
  93. });
  94. it('error posts with initial error', function(done) {
  95. var called = {};
  96. hooks.post('cook', function(eggs, callback) {
  97. assert.ok(false);
  98. });
  99. hooks.post('cook', function(error, eggs, callback) {
  100. called.second = true;
  101. assert.equal(error.message, 'fail');
  102. callback(new Error('second'));
  103. });
  104. hooks.post('cook', function(error, eggs, callback) {
  105. called.third = true;
  106. assert.equal(error.message, 'second');
  107. callback(new Error('third'));
  108. });
  109. hooks.post('cook', function(error, eggs, callback) {
  110. called.fourth = true;
  111. assert.equal(error.message, 'third');
  112. callback();
  113. });
  114. var options = { error: new Error('fail') };
  115. hooks.execPost('cook', null, [4], options, function(error, eggs) {
  116. assert.ok(error);
  117. assert.equal(error.message, 'third');
  118. assert.deepEqual(called, {
  119. second: true,
  120. third: true,
  121. fourth: true
  122. });
  123. done();
  124. });
  125. });
  126. it('supports returning a promise', function(done) {
  127. var calledPost = 0;
  128. hooks.post('cook', function() {
  129. return new Promise(resolve => {
  130. setTimeout(() => {
  131. ++calledPost;
  132. resolve();
  133. }, 100);
  134. });
  135. });
  136. hooks.execPost('cook', null, [], {}, function(error) {
  137. assert.ifError(error);
  138. assert.equal(calledPost, 1);
  139. done();
  140. });
  141. });
  142. });
  143. describe('execPostSync', function() {
  144. var hooks;
  145. beforeEach(function() {
  146. hooks = new Kareem();
  147. });
  148. it('executes hooks synchronously', function() {
  149. var execed = {};
  150. hooks.post('cook', function() {
  151. execed.first = true;
  152. });
  153. hooks.post('cook', function() {
  154. execed.second = true;
  155. });
  156. hooks.execPostSync('cook', null);
  157. assert.ok(execed.first);
  158. assert.ok(execed.second);
  159. });
  160. it('works with no hooks specified', function() {
  161. assert.doesNotThrow(function() {
  162. hooks.execPostSync('cook', null);
  163. });
  164. });
  165. });