wrap.test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. var assert = require('assert');
  2. var Kareem = require('../');
  3. describe('wrap()', function() {
  4. var hooks;
  5. beforeEach(function() {
  6. hooks = new Kareem();
  7. });
  8. it('handles pre errors', function(done) {
  9. hooks.pre('cook', function(done) {
  10. done('error!');
  11. });
  12. hooks.post('cook', function(obj) {
  13. obj.tofu = 'no';
  14. });
  15. var obj = { bacon: 0, eggs: 0 };
  16. var args = [obj];
  17. args.push(function(error, result) {
  18. assert.equal('error!', error);
  19. assert.ok(!result);
  20. assert.equal(undefined, obj.tofu);
  21. done();
  22. });
  23. hooks.wrap(
  24. 'cook',
  25. function(o, callback) {
  26. // Should never get called
  27. assert.ok(false);
  28. callback(null, o);
  29. },
  30. obj,
  31. args);
  32. });
  33. it('handles pre errors when no callback defined', function(done) {
  34. hooks.pre('cook', function(done) {
  35. done('error!');
  36. });
  37. hooks.post('cook', function(obj) {
  38. obj.tofu = 'no';
  39. });
  40. var obj = { bacon: 0, eggs: 0 };
  41. var args = [obj];
  42. hooks.wrap(
  43. 'cook',
  44. function(o, callback) {
  45. // Should never get called
  46. assert.ok(false);
  47. callback(null, o);
  48. },
  49. obj,
  50. args);
  51. setTimeout(
  52. function() {
  53. done();
  54. },
  55. 25);
  56. });
  57. it('handles errors in wrapped function', function(done) {
  58. hooks.pre('cook', function(done) {
  59. done();
  60. });
  61. hooks.post('cook', function(obj) {
  62. obj.tofu = 'no';
  63. });
  64. var obj = { bacon: 0, eggs: 0 };
  65. var args = [obj];
  66. args.push(function(error, result) {
  67. assert.equal('error!', error);
  68. assert.ok(!result);
  69. assert.equal(undefined, obj.tofu);
  70. done();
  71. });
  72. hooks.wrap(
  73. 'cook',
  74. function(o, callback) {
  75. callback('error!');
  76. },
  77. obj,
  78. args);
  79. });
  80. it('handles errors in post', function(done) {
  81. hooks.pre('cook', function(done) {
  82. done();
  83. });
  84. hooks.post('cook', function(obj, callback) {
  85. obj.tofu = 'no';
  86. callback('error!');
  87. });
  88. var obj = { bacon: 0, eggs: 0 };
  89. var args = [obj];
  90. args.push(function(error, result) {
  91. assert.equal('error!', error);
  92. assert.ok(!result);
  93. assert.equal('no', obj.tofu);
  94. done();
  95. });
  96. hooks.wrap(
  97. 'cook',
  98. function(o, callback) {
  99. callback(null, o);
  100. },
  101. obj,
  102. args);
  103. });
  104. it('defers errors to post hooks if enabled', function(done) {
  105. hooks.pre('cook', function(done) {
  106. done(new Error('fail'));
  107. });
  108. hooks.post('cook', function(error, res, callback) {
  109. callback(new Error('another error occurred'));
  110. });
  111. var args = [];
  112. args.push(function(error) {
  113. assert.equal(error.message, 'another error occurred');
  114. done();
  115. });
  116. hooks.wrap(
  117. 'cook',
  118. function(callback) {
  119. assert.ok(false);
  120. callback();
  121. },
  122. null,
  123. args,
  124. { useErrorHandlers: true, numCallbackParams: 1 });
  125. });
  126. it('error handlers with no callback', function(done) {
  127. hooks.pre('cook', function(done) {
  128. done(new Error('fail'));
  129. });
  130. hooks.post('cook', function(error, callback) {
  131. assert.equal(error.message, 'fail');
  132. done();
  133. });
  134. var args = [];
  135. hooks.wrap(
  136. 'cook',
  137. function(callback) {
  138. assert.ok(false);
  139. callback();
  140. },
  141. null,
  142. args,
  143. { useErrorHandlers: true });
  144. });
  145. it('error handlers with no error', function(done) {
  146. hooks.post('cook', function(error, callback) {
  147. callback(new Error('another error occurred'));
  148. });
  149. var args = [];
  150. args.push(function(error) {
  151. assert.ifError(error);
  152. done();
  153. });
  154. hooks.wrap(
  155. 'cook',
  156. function(callback) {
  157. callback();
  158. },
  159. null,
  160. args,
  161. { useErrorHandlers: true });
  162. });
  163. it('works with no args', function(done) {
  164. hooks.pre('cook', function(done) {
  165. done();
  166. });
  167. hooks.post('cook', function(callback) {
  168. obj.tofu = 'no';
  169. callback();
  170. });
  171. var obj = { bacon: 0, eggs: 0 };
  172. var args = [];
  173. hooks.wrap(
  174. 'cook',
  175. function(callback) {
  176. callback(null);
  177. },
  178. obj,
  179. args);
  180. setTimeout(
  181. function() {
  182. assert.equal('no', obj.tofu);
  183. done();
  184. },
  185. 25);
  186. });
  187. it('handles pre errors with no args', function(done) {
  188. hooks.pre('cook', function(done) {
  189. done('error!');
  190. });
  191. hooks.post('cook', function(callback) {
  192. obj.tofu = 'no';
  193. callback();
  194. });
  195. var obj = { bacon: 0, eggs: 0 };
  196. var args = [];
  197. hooks.wrap(
  198. 'cook',
  199. function(callback) {
  200. callback(null);
  201. },
  202. obj,
  203. args);
  204. setTimeout(
  205. function() {
  206. assert.equal(undefined, obj.tofu);
  207. done();
  208. },
  209. 25);
  210. });
  211. it('handles wrapped function errors with no args', function(done) {
  212. hooks.pre('cook', function(done) {
  213. obj.waffles = false;
  214. done();
  215. });
  216. hooks.post('cook', function(callback) {
  217. obj.tofu = 'no';
  218. callback();
  219. });
  220. var obj = { bacon: 0, eggs: 0 };
  221. var args = [];
  222. hooks.wrap(
  223. 'cook',
  224. function(callback) {
  225. callback('error!');
  226. },
  227. obj,
  228. args);
  229. setTimeout(
  230. function() {
  231. assert.equal(false, obj.waffles);
  232. assert.equal(undefined, obj.tofu);
  233. done();
  234. },
  235. 25);
  236. });
  237. it('handles post errors with no args', function(done) {
  238. hooks.pre('cook', function(done) {
  239. obj.waffles = false;
  240. done();
  241. });
  242. hooks.post('cook', function(callback) {
  243. obj.tofu = 'no';
  244. callback('error!');
  245. });
  246. var obj = { bacon: 0, eggs: 0 };
  247. var args = [];
  248. hooks.wrap(
  249. 'cook',
  250. function(callback) {
  251. callback();
  252. },
  253. obj,
  254. args);
  255. setTimeout(
  256. function() {
  257. assert.equal(false, obj.waffles);
  258. assert.equal('no', obj.tofu);
  259. done();
  260. },
  261. 25);
  262. });
  263. it('sync wrappers', function() {
  264. var calledPre = 0;
  265. var calledFn = 0;
  266. var calledPost = 0;
  267. hooks.pre('cook', function() {
  268. ++calledPre;
  269. });
  270. hooks.post('cook', function() {
  271. ++calledPost;
  272. });
  273. var wrapper = hooks.createWrapperSync('cook', function() { ++calledFn; });
  274. wrapper();
  275. assert.equal(calledPre, 1);
  276. assert.equal(calledFn, 1);
  277. assert.equal(calledPost, 1);
  278. });
  279. });