pre.test.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. 'use strict';
  2. const assert = require('assert');
  3. const Kareem = require('../');
  4. describe('execPre', function() {
  5. var hooks;
  6. beforeEach(function() {
  7. hooks = new Kareem();
  8. });
  9. it('handles errors with multiple pres', function(done) {
  10. var execed = {};
  11. hooks.pre('cook', function(done) {
  12. execed.first = true;
  13. done();
  14. });
  15. hooks.pre('cook', function(done) {
  16. execed.second = true;
  17. done('error!');
  18. });
  19. hooks.pre('cook', function(done) {
  20. execed.third = true;
  21. done();
  22. });
  23. hooks.execPre('cook', null, function(err) {
  24. assert.equal('error!', err);
  25. assert.equal(2, Object.keys(execed).length);
  26. assert.ok(execed.first);
  27. assert.ok(execed.second);
  28. done();
  29. });
  30. });
  31. it('sync errors', function(done) {
  32. var called = 0;
  33. hooks.pre('cook', function(next) {
  34. throw new Error('woops!');
  35. });
  36. hooks.pre('cook', function(next) {
  37. ++called;
  38. next();
  39. });
  40. hooks.execPre('cook', null, function(err) {
  41. assert.equal(err.message, 'woops!');
  42. assert.equal(called, 0);
  43. done();
  44. });
  45. });
  46. it('unshift', function() {
  47. var f1 = function() {};
  48. var f2 = function() {};
  49. hooks.pre('cook', false, f1);
  50. hooks.pre('cook', false, f2, null, true);
  51. assert.strictEqual(hooks._pres.get('cook')[0].fn, f2);
  52. assert.strictEqual(hooks._pres.get('cook')[1].fn, f1);
  53. });
  54. it('throws error if no function', function() {
  55. assert.throws(() => hooks.pre('test'), /got "undefined"/);
  56. });
  57. it('arbitrary options', function() {
  58. const f1 = function() {};
  59. const f2 = function() {};
  60. hooks.pre('cook', { foo: 'bar' }, f1);
  61. hooks.pre('cook', { bar: 'baz' }, f2, null, true);
  62. assert.equal(hooks._pres.get('cook')[1].foo, 'bar');
  63. assert.equal(hooks._pres.get('cook')[0].bar, 'baz');
  64. });
  65. it('handles async errors', function(done) {
  66. var execed = {};
  67. hooks.pre('cook', true, function(next, done) {
  68. execed.first = true;
  69. setTimeout(
  70. function() {
  71. done('error!');
  72. },
  73. 5);
  74. next();
  75. });
  76. hooks.pre('cook', true, function(next, done) {
  77. execed.second = true;
  78. setTimeout(
  79. function() {
  80. done('other error!');
  81. },
  82. 10);
  83. next();
  84. });
  85. hooks.execPre('cook', null, function(err) {
  86. assert.equal('error!', err);
  87. assert.equal(2, Object.keys(execed).length);
  88. assert.ok(execed.first);
  89. assert.ok(execed.second);
  90. done();
  91. });
  92. });
  93. it('handles async errors in next()', function(done) {
  94. var execed = {};
  95. hooks.pre('cook', true, function(next, done) {
  96. execed.first = true;
  97. setTimeout(
  98. function() {
  99. done('other error!');
  100. },
  101. 15);
  102. next();
  103. });
  104. hooks.pre('cook', true, function(next, done) {
  105. execed.second = true;
  106. setTimeout(
  107. function() {
  108. next('error!');
  109. done('another error!');
  110. },
  111. 5);
  112. });
  113. hooks.execPre('cook', null, function(err) {
  114. assert.equal('error!', err);
  115. assert.equal(2, Object.keys(execed).length);
  116. assert.ok(execed.first);
  117. assert.ok(execed.second);
  118. done();
  119. });
  120. });
  121. it('handles async errors in next() when already done', function(done) {
  122. var execed = {};
  123. hooks.pre('cook', true, function(next, done) {
  124. execed.first = true;
  125. setTimeout(
  126. function() {
  127. done('other error!');
  128. },
  129. 5);
  130. next();
  131. });
  132. hooks.pre('cook', true, function(next, done) {
  133. execed.second = true;
  134. setTimeout(
  135. function() {
  136. next('error!');
  137. done('another error!');
  138. },
  139. 25);
  140. });
  141. hooks.execPre('cook', null, function(err) {
  142. assert.equal('other error!', err);
  143. assert.equal(2, Object.keys(execed).length);
  144. assert.ok(execed.first);
  145. assert.ok(execed.second);
  146. done();
  147. });
  148. });
  149. it('async pres with clone()', function(done) {
  150. var execed = false;
  151. hooks.pre('cook', true, function(next, done) {
  152. execed = true;
  153. setTimeout(
  154. function() {
  155. done();
  156. },
  157. 5);
  158. next();
  159. });
  160. hooks.clone().execPre('cook', null, function(err) {
  161. assert.ifError(err);
  162. assert.ok(execed);
  163. done();
  164. });
  165. });
  166. it('returns correct error when async pre errors', function(done) {
  167. var execed = {};
  168. hooks.pre('cook', true, function(next, done) {
  169. execed.first = true;
  170. setTimeout(
  171. function() {
  172. done('other error!');
  173. },
  174. 5);
  175. next();
  176. });
  177. hooks.pre('cook', function(next) {
  178. execed.second = true;
  179. setTimeout(
  180. function() {
  181. next('error!');
  182. },
  183. 15);
  184. });
  185. hooks.execPre('cook', null, function(err) {
  186. assert.equal('other error!', err);
  187. assert.equal(2, Object.keys(execed).length);
  188. assert.ok(execed.first);
  189. assert.ok(execed.second);
  190. done();
  191. });
  192. });
  193. it('lets async pres run when fully sync pres are done', function(done) {
  194. var execed = {};
  195. hooks.pre('cook', true, function(next, done) {
  196. execed.first = true;
  197. setTimeout(
  198. function() {
  199. done();
  200. },
  201. 5);
  202. next();
  203. });
  204. hooks.pre('cook', function() {
  205. execed.second = true;
  206. });
  207. hooks.execPre('cook', null, function(err) {
  208. assert.ifError(err);
  209. assert.equal(2, Object.keys(execed).length);
  210. assert.ok(execed.first);
  211. assert.ok(execed.second);
  212. done();
  213. });
  214. });
  215. it('allows passing arguments to the next pre', function(done) {
  216. var execed = {};
  217. hooks.pre('cook', function(next) {
  218. execed.first = true;
  219. next(null, 'test');
  220. });
  221. hooks.pre('cook', function(next, p) {
  222. execed.second = true;
  223. assert.equal(p, 'test');
  224. next();
  225. });
  226. hooks.pre('cook', function(next, p) {
  227. execed.third = true;
  228. assert.ok(!p);
  229. next();
  230. });
  231. hooks.execPre('cook', null, function(err) {
  232. assert.ifError(err);
  233. assert.equal(3, Object.keys(execed).length);
  234. assert.ok(execed.first);
  235. assert.ok(execed.second);
  236. assert.ok(execed.third);
  237. done();
  238. });
  239. });
  240. it('handles sync errors in pre if there are more hooks', function(done) {
  241. var execed = {};
  242. hooks.pre('cook', function() {
  243. execed.first = true;
  244. throw new Error('Oops!');
  245. });
  246. hooks.pre('cook', function() {
  247. execed.second = true;
  248. });
  249. hooks.execPre('cook', null, function(err) {
  250. assert.ok(err);
  251. assert.ok(execed.first);
  252. assert.equal(err.message, 'Oops!');
  253. done();
  254. });
  255. });
  256. });
  257. describe('execPreSync', function() {
  258. var hooks;
  259. beforeEach(function() {
  260. hooks = new Kareem();
  261. });
  262. it('executes hooks synchronously', function() {
  263. var execed = {};
  264. hooks.pre('cook', function() {
  265. execed.first = true;
  266. });
  267. hooks.pre('cook', function() {
  268. execed.second = true;
  269. });
  270. hooks.execPreSync('cook', null);
  271. assert.ok(execed.first);
  272. assert.ok(execed.second);
  273. });
  274. it('works with no hooks specified', function() {
  275. assert.doesNotThrow(function() {
  276. hooks.execPreSync('cook', null);
  277. });
  278. });
  279. });