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.

post.test.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict';
  2. const assert = require('assert');
  3. const Kareem = require('../');
  4. describe('execPost', function() {
  5. var hooks;
  6. beforeEach(function() {
  7. hooks = new Kareem();
  8. });
  9. it('handles errors', function(done) {
  10. hooks.post('cook', function(eggs, callback) {
  11. callback('error!');
  12. });
  13. hooks.execPost('cook', null, [4], function(error, eggs) {
  14. assert.equal('error!', error);
  15. assert.ok(!eggs);
  16. done();
  17. });
  18. });
  19. it('unshift', function() {
  20. var f1 = function() {};
  21. var f2 = function() {};
  22. hooks.post('cook', f1);
  23. hooks.post('cook', f2, true);
  24. assert.strictEqual(hooks._posts.get('cook')[0].fn, f2);
  25. assert.strictEqual(hooks._posts.get('cook')[1].fn, f1);
  26. });
  27. it('arbitrary options', function() {
  28. const f1 = function() {};
  29. const f2 = function() {};
  30. hooks.post('cook', { foo: 'bar' }, f1);
  31. hooks.post('cook', { bar: 'baz' }, f2, true);
  32. assert.equal(hooks._posts.get('cook')[1].foo, 'bar');
  33. assert.equal(hooks._posts.get('cook')[0].bar, 'baz');
  34. });
  35. it('multiple posts', function(done) {
  36. hooks.post('cook', function(eggs, callback) {
  37. setTimeout(
  38. function() {
  39. callback();
  40. },
  41. 5);
  42. });
  43. hooks.post('cook', function(eggs, callback) {
  44. setTimeout(
  45. function() {
  46. callback();
  47. },
  48. 5);
  49. });
  50. hooks.execPost('cook', null, [4], function(error, eggs) {
  51. assert.ifError(error);
  52. assert.equal(4, eggs);
  53. done();
  54. });
  55. });
  56. it('error posts', function(done) {
  57. var called = {};
  58. hooks.post('cook', function(eggs, callback) {
  59. called.first = true;
  60. callback();
  61. });
  62. hooks.post('cook', function(eggs, callback) {
  63. called.second = true;
  64. callback(new Error('fail'));
  65. });
  66. hooks.post('cook', function(eggs, callback) {
  67. assert.ok(false);
  68. });
  69. hooks.post('cook', function(error, eggs, callback) {
  70. called.fourth = true;
  71. assert.equal(error.message, 'fail');
  72. callback(new Error('fourth'));
  73. });
  74. hooks.post('cook', function(error, eggs, callback) {
  75. called.fifth = true;
  76. assert.equal(error.message, 'fourth');
  77. callback(new Error('fifth'));
  78. });
  79. hooks.execPost('cook', null, [4], function(error, eggs) {
  80. assert.ok(error);
  81. assert.equal(error.message, 'fifth');
  82. assert.deepEqual(called, {
  83. first: true,
  84. second: true,
  85. fourth: true,
  86. fifth: true
  87. });
  88. done();
  89. });
  90. });
  91. it('error posts with initial error', function(done) {
  92. var called = {};
  93. hooks.post('cook', function(eggs, callback) {
  94. assert.ok(false);
  95. });
  96. hooks.post('cook', function(error, eggs, callback) {
  97. called.second = true;
  98. assert.equal(error.message, 'fail');
  99. callback(new Error('second'));
  100. });
  101. hooks.post('cook', function(error, eggs, callback) {
  102. called.third = true;
  103. assert.equal(error.message, 'second');
  104. callback(new Error('third'));
  105. });
  106. hooks.post('cook', function(error, eggs, callback) {
  107. called.fourth = true;
  108. assert.equal(error.message, 'third');
  109. callback();
  110. });
  111. var options = { error: new Error('fail') };
  112. hooks.execPost('cook', null, [4], options, function(error, eggs) {
  113. assert.ok(error);
  114. assert.equal(error.message, 'third');
  115. assert.deepEqual(called, {
  116. second: true,
  117. third: true,
  118. fourth: true
  119. });
  120. done();
  121. });
  122. });
  123. it('supports returning a promise', function(done) {
  124. var calledPost = 0;
  125. hooks.post('cook', function() {
  126. return new Promise(resolve => {
  127. setTimeout(() => {
  128. ++calledPost;
  129. resolve();
  130. }, 100);
  131. });
  132. });
  133. hooks.execPost('cook', null, [], {}, function(error) {
  134. assert.ifError(error);
  135. assert.equal(calledPost, 1);
  136. done();
  137. });
  138. });
  139. });
  140. describe('execPostSync', function() {
  141. var hooks;
  142. beforeEach(function() {
  143. hooks = new Kareem();
  144. });
  145. it('executes hooks synchronously', function() {
  146. var execed = {};
  147. hooks.post('cook', function() {
  148. execed.first = true;
  149. });
  150. hooks.post('cook', function() {
  151. execed.second = true;
  152. });
  153. hooks.execPostSync('cook', null);
  154. assert.ok(execed.first);
  155. assert.ok(execed.second);
  156. });
  157. it('works with no hooks specified', function() {
  158. assert.doesNotThrow(function() {
  159. hooks.execPostSync('cook', null);
  160. });
  161. });
  162. });