post.test.js 6.1 KB

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