Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pre.test.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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('arbitrary options', function() {
  55. const f1 = function() {};
  56. const f2 = function() {};
  57. hooks.pre('cook', { foo: 'bar' }, f1);
  58. hooks.pre('cook', { bar: 'baz' }, f2, null, true);
  59. assert.equal(hooks._pres.get('cook')[1].foo, 'bar');
  60. assert.equal(hooks._pres.get('cook')[0].bar, 'baz');
  61. });
  62. it('handles async errors', function(done) {
  63. var execed = {};
  64. hooks.pre('cook', true, function(next, done) {
  65. execed.first = true;
  66. setTimeout(
  67. function() {
  68. done('error!');
  69. },
  70. 5);
  71. next();
  72. });
  73. hooks.pre('cook', true, function(next, done) {
  74. execed.second = true;
  75. setTimeout(
  76. function() {
  77. done('other error!');
  78. },
  79. 10);
  80. next();
  81. });
  82. hooks.execPre('cook', null, function(err) {
  83. assert.equal('error!', err);
  84. assert.equal(2, Object.keys(execed).length);
  85. assert.ok(execed.first);
  86. assert.ok(execed.second);
  87. done();
  88. });
  89. });
  90. it('handles async errors in next()', function(done) {
  91. var execed = {};
  92. hooks.pre('cook', true, function(next, done) {
  93. execed.first = true;
  94. setTimeout(
  95. function() {
  96. done('other error!');
  97. },
  98. 15);
  99. next();
  100. });
  101. hooks.pre('cook', true, function(next, done) {
  102. execed.second = true;
  103. setTimeout(
  104. function() {
  105. next('error!');
  106. done('another error!');
  107. },
  108. 5);
  109. });
  110. hooks.execPre('cook', null, function(err) {
  111. assert.equal('error!', err);
  112. assert.equal(2, Object.keys(execed).length);
  113. assert.ok(execed.first);
  114. assert.ok(execed.second);
  115. done();
  116. });
  117. });
  118. it('handles async errors in next() when already done', function(done) {
  119. var execed = {};
  120. hooks.pre('cook', true, function(next, done) {
  121. execed.first = true;
  122. setTimeout(
  123. function() {
  124. done('other error!');
  125. },
  126. 5);
  127. next();
  128. });
  129. hooks.pre('cook', true, function(next, done) {
  130. execed.second = true;
  131. setTimeout(
  132. function() {
  133. next('error!');
  134. done('another error!');
  135. },
  136. 25);
  137. });
  138. hooks.execPre('cook', null, function(err) {
  139. assert.equal('other error!', err);
  140. assert.equal(2, Object.keys(execed).length);
  141. assert.ok(execed.first);
  142. assert.ok(execed.second);
  143. done();
  144. });
  145. });
  146. it('async pres with clone()', function(done) {
  147. var execed = false;
  148. hooks.pre('cook', true, function(next, done) {
  149. execed = true;
  150. setTimeout(
  151. function() {
  152. done();
  153. },
  154. 5);
  155. next();
  156. });
  157. hooks.clone().execPre('cook', null, function(err) {
  158. assert.ifError(err);
  159. assert.ok(execed);
  160. done();
  161. });
  162. });
  163. it('returns correct error when async pre errors', function(done) {
  164. var execed = {};
  165. hooks.pre('cook', true, function(next, done) {
  166. execed.first = true;
  167. setTimeout(
  168. function() {
  169. done('other error!');
  170. },
  171. 5);
  172. next();
  173. });
  174. hooks.pre('cook', function(next) {
  175. execed.second = true;
  176. setTimeout(
  177. function() {
  178. next('error!');
  179. },
  180. 15);
  181. });
  182. hooks.execPre('cook', null, function(err) {
  183. assert.equal('other error!', err);
  184. assert.equal(2, Object.keys(execed).length);
  185. assert.ok(execed.first);
  186. assert.ok(execed.second);
  187. done();
  188. });
  189. });
  190. it('lets async pres run when fully sync pres are done', function(done) {
  191. var execed = {};
  192. hooks.pre('cook', true, function(next, done) {
  193. execed.first = true;
  194. setTimeout(
  195. function() {
  196. done();
  197. },
  198. 5);
  199. next();
  200. });
  201. hooks.pre('cook', function() {
  202. execed.second = true;
  203. });
  204. hooks.execPre('cook', null, function(err) {
  205. assert.ifError(err);
  206. assert.equal(2, Object.keys(execed).length);
  207. assert.ok(execed.first);
  208. assert.ok(execed.second);
  209. done();
  210. });
  211. });
  212. it('allows passing arguments to the next pre', function(done) {
  213. var execed = {};
  214. hooks.pre('cook', function(next) {
  215. execed.first = true;
  216. next(null, 'test');
  217. });
  218. hooks.pre('cook', function(next, p) {
  219. execed.second = true;
  220. assert.equal(p, 'test');
  221. next();
  222. });
  223. hooks.pre('cook', function(next, p) {
  224. execed.third = true;
  225. assert.ok(!p);
  226. next();
  227. });
  228. hooks.execPre('cook', null, function(err) {
  229. assert.ifError(err);
  230. assert.equal(3, Object.keys(execed).length);
  231. assert.ok(execed.first);
  232. assert.ok(execed.second);
  233. assert.ok(execed.third);
  234. done();
  235. });
  236. });
  237. });
  238. describe('execPreSync', function() {
  239. var hooks;
  240. beforeEach(function() {
  241. hooks = new Kareem();
  242. });
  243. it('executes hooks synchronously', function() {
  244. var execed = {};
  245. hooks.pre('cook', function() {
  246. execed.first = true;
  247. });
  248. hooks.pre('cook', function() {
  249. execed.second = true;
  250. });
  251. hooks.execPreSync('cook', null);
  252. assert.ok(execed.first);
  253. assert.ok(execed.second);
  254. });
  255. it('works with no hooks specified', function() {
  256. assert.doesNotThrow(function() {
  257. hooks.execPreSync('cook', null);
  258. });
  259. });
  260. });