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.

examples.test.js 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. var assert = require('assert');
  2. var Kareem = require('../');
  3. /* Much like [hooks](https://npmjs.org/package/hooks), kareem lets you define
  4. * pre and post hooks: pre hooks are called before a given function executes.
  5. * Unlike hooks, kareem stores hooks and other internal state in a separate
  6. * object, rather than relying on inheritance. Furthermore, kareem exposes
  7. * an `execPre()` function that allows you to execute your pre hooks when
  8. * appropriate, giving you more fine-grained control over your function hooks.
  9. */
  10. describe('pre hooks', function() {
  11. var hooks;
  12. beforeEach(function() {
  13. hooks = new Kareem();
  14. });
  15. it('runs without any hooks specified', function(done) {
  16. hooks.execPre('cook', null, function() {
  17. done();
  18. });
  19. });
  20. /* pre hook functions take one parameter, a "done" function that you execute
  21. * when your pre hook is finished.
  22. */
  23. it('runs basic serial pre hooks', function(done) {
  24. var count = 0;
  25. hooks.pre('cook', function(done) {
  26. ++count;
  27. done();
  28. });
  29. hooks.execPre('cook', null, function() {
  30. assert.equal(1, count);
  31. done();
  32. });
  33. });
  34. it('can run multipe pre hooks', function(done) {
  35. var count1 = 0;
  36. var count2 = 0;
  37. hooks.pre('cook', function(done) {
  38. ++count1;
  39. done();
  40. });
  41. hooks.pre('cook', function(done) {
  42. ++count2;
  43. done();
  44. });
  45. hooks.execPre('cook', null, function() {
  46. assert.equal(1, count1);
  47. assert.equal(1, count2);
  48. done();
  49. });
  50. });
  51. /* If your pre hook function takes no parameters, its assumed to be
  52. * fully synchronous.
  53. */
  54. it('can run fully synchronous pre hooks', function(done) {
  55. var count1 = 0;
  56. var count2 = 0;
  57. hooks.pre('cook', function() {
  58. ++count1;
  59. });
  60. hooks.pre('cook', function() {
  61. ++count2;
  62. });
  63. hooks.execPre('cook', null, function(error) {
  64. assert.equal(null, error);
  65. assert.equal(1, count1);
  66. assert.equal(1, count2);
  67. done();
  68. });
  69. });
  70. /* Pre save hook functions are bound to the second parameter to `execPre()`
  71. */
  72. it('properly attaches context to pre hooks', function(done) {
  73. hooks.pre('cook', function(done) {
  74. this.bacon = 3;
  75. done();
  76. });
  77. hooks.pre('cook', function(done) {
  78. this.eggs = 4;
  79. done();
  80. });
  81. var obj = { bacon: 0, eggs: 0 };
  82. // In the pre hooks, `this` will refer to `obj`
  83. hooks.execPre('cook', obj, function(error) {
  84. assert.equal(null, error);
  85. assert.equal(3, obj.bacon);
  86. assert.equal(4, obj.eggs);
  87. done();
  88. });
  89. });
  90. /* Like the hooks module, you can declare "async" pre hooks - these take two
  91. * parameters, the functions `next()` and `done()`. `next()` passes control to
  92. * the next pre hook, but the underlying function won't be called until all
  93. * async pre hooks have called `done()`.
  94. */
  95. it('can execute parallel (async) pre hooks', function(done) {
  96. hooks.pre('cook', true, function(next, done) {
  97. this.bacon = 3;
  98. next();
  99. setTimeout(function() {
  100. done();
  101. }, 5);
  102. });
  103. hooks.pre('cook', true, function(next, done) {
  104. next();
  105. var _this = this;
  106. setTimeout(function() {
  107. _this.eggs = 4;
  108. done();
  109. }, 10);
  110. });
  111. hooks.pre('cook', function(next) {
  112. this.waffles = false;
  113. next();
  114. });
  115. var obj = { bacon: 0, eggs: 0 };
  116. hooks.execPre('cook', obj, function() {
  117. assert.equal(3, obj.bacon);
  118. assert.equal(4, obj.eggs);
  119. assert.equal(false, obj.waffles);
  120. done();
  121. });
  122. });
  123. /* You can also return a promise from your pre hooks instead of calling
  124. * `next()`. When the returned promise resolves, kareem will kick off the
  125. * next middleware.
  126. */
  127. it('supports returning a promise', function(done) {
  128. hooks.pre('cook', function() {
  129. return new Promise(resolve => {
  130. setTimeout(() => {
  131. this.bacon = 3;
  132. resolve();
  133. }, 100);
  134. });
  135. });
  136. var obj = { bacon: 0 };
  137. hooks.execPre('cook', obj, function() {
  138. assert.equal(3, obj.bacon);
  139. done();
  140. });
  141. });
  142. });
  143. describe('post hooks', function() {
  144. var hooks;
  145. beforeEach(function() {
  146. hooks = new Kareem();
  147. });
  148. it('runs without any hooks specified', function(done) {
  149. hooks.execPost('cook', null, [1], function(error, eggs) {
  150. assert.ifError(error);
  151. assert.equal(1, eggs);
  152. done();
  153. });
  154. });
  155. it('executes with parameters passed in', function(done) {
  156. hooks.post('cook', function(eggs, bacon, callback) {
  157. assert.equal(1, eggs);
  158. assert.equal(2, bacon);
  159. callback();
  160. });
  161. hooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {
  162. assert.ifError(error);
  163. assert.equal(1, eggs);
  164. assert.equal(2, bacon);
  165. done();
  166. });
  167. });
  168. it('can use synchronous post hooks', function(done) {
  169. var execed = {};
  170. hooks.post('cook', function(eggs, bacon) {
  171. execed.first = true;
  172. assert.equal(1, eggs);
  173. assert.equal(2, bacon);
  174. });
  175. hooks.post('cook', function(eggs, bacon, callback) {
  176. execed.second = true;
  177. assert.equal(1, eggs);
  178. assert.equal(2, bacon);
  179. callback();
  180. });
  181. hooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {
  182. assert.ifError(error);
  183. assert.equal(2, Object.keys(execed).length);
  184. assert.ok(execed.first);
  185. assert.ok(execed.second);
  186. assert.equal(1, eggs);
  187. assert.equal(2, bacon);
  188. done();
  189. });
  190. });
  191. });
  192. describe('wrap()', function() {
  193. var hooks;
  194. beforeEach(function() {
  195. hooks = new Kareem();
  196. });
  197. it('wraps pre and post calls into one call', function(done) {
  198. hooks.pre('cook', true, function(next, done) {
  199. this.bacon = 3;
  200. next();
  201. setTimeout(function() {
  202. done();
  203. }, 5);
  204. });
  205. hooks.pre('cook', true, function(next, done) {
  206. next();
  207. var _this = this;
  208. setTimeout(function() {
  209. _this.eggs = 4;
  210. done();
  211. }, 10);
  212. });
  213. hooks.pre('cook', function(next) {
  214. this.waffles = false;
  215. next();
  216. });
  217. hooks.post('cook', function(obj) {
  218. obj.tofu = 'no';
  219. });
  220. var obj = { bacon: 0, eggs: 0 };
  221. var args = [obj];
  222. args.push(function(error, result) {
  223. assert.ifError(error);
  224. assert.equal(null, error);
  225. assert.equal(3, obj.bacon);
  226. assert.equal(4, obj.eggs);
  227. assert.equal(false, obj.waffles);
  228. assert.equal('no', obj.tofu);
  229. assert.equal(obj, result);
  230. done();
  231. });
  232. hooks.wrap(
  233. 'cook',
  234. function(o, callback) {
  235. assert.equal(3, obj.bacon);
  236. assert.equal(4, obj.eggs);
  237. assert.equal(false, obj.waffles);
  238. assert.equal(undefined, obj.tofu);
  239. callback(null, o);
  240. },
  241. obj,
  242. args);
  243. });
  244. });
  245. describe('createWrapper()', function() {
  246. var hooks;
  247. beforeEach(function() {
  248. hooks = new Kareem();
  249. });
  250. it('wraps wrap() into a callable function', function(done) {
  251. hooks.pre('cook', true, function(next, done) {
  252. this.bacon = 3;
  253. next();
  254. setTimeout(function() {
  255. done();
  256. }, 5);
  257. });
  258. hooks.pre('cook', true, function(next, done) {
  259. next();
  260. var _this = this;
  261. setTimeout(function() {
  262. _this.eggs = 4;
  263. done();
  264. }, 10);
  265. });
  266. hooks.pre('cook', function(next) {
  267. this.waffles = false;
  268. next();
  269. });
  270. hooks.post('cook', function(obj) {
  271. obj.tofu = 'no';
  272. });
  273. var obj = { bacon: 0, eggs: 0 };
  274. var cook = hooks.createWrapper(
  275. 'cook',
  276. function(o, callback) {
  277. assert.equal(3, obj.bacon);
  278. assert.equal(4, obj.eggs);
  279. assert.equal(false, obj.waffles);
  280. assert.equal(undefined, obj.tofu);
  281. callback(null, o);
  282. },
  283. obj);
  284. cook(obj, function(error, result) {
  285. assert.ifError(error);
  286. assert.equal(3, obj.bacon);
  287. assert.equal(4, obj.eggs);
  288. assert.equal(false, obj.waffles);
  289. assert.equal('no', obj.tofu);
  290. assert.equal(obj, result);
  291. done();
  292. });
  293. });
  294. });
  295. describe('clone()', function() {
  296. it('clones a Kareem object', function() {
  297. var k1 = new Kareem();
  298. k1.pre('cook', function() {});
  299. k1.post('cook', function() {});
  300. var k2 = k1.clone();
  301. assert.deepEqual(Array.from(k2._pres.keys()), ['cook']);
  302. assert.deepEqual(Array.from(k2._posts.keys()), ['cook']);
  303. });
  304. });
  305. describe('merge()', function() {
  306. it('pulls hooks from another Kareem object', function() {
  307. var k1 = new Kareem();
  308. var test1 = function() {};
  309. k1.pre('cook', test1);
  310. k1.post('cook', function() {});
  311. var k2 = new Kareem();
  312. var test2 = function() {};
  313. k2.pre('cook', test2);
  314. var k3 = k2.merge(k1);
  315. assert.equal(k3._pres.get('cook').length, 2);
  316. assert.equal(k3._pres.get('cook')[0].fn, test2);
  317. assert.equal(k3._pres.get('cook')[1].fn, test1);
  318. assert.equal(k3._posts.get('cook').length, 1);
  319. });
  320. });