hook-registry.spec.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import {expect} from '../chai.js';
  2. import {format} from '@e22m4u/js-format';
  3. import {HookName} from './hook-registry.js';
  4. import {HookRegistry} from './hook-registry.js';
  5. describe('HookRegistry', function () {
  6. describe('addHook', function () {
  7. it('requires the parameter "name" to be a non-empty String', function () {
  8. const s = new HookRegistry();
  9. const throwable = v => () => s.addHook(v, () => undefined);
  10. const error = v => format('The hook name is required, but %s given.', v);
  11. expect(throwable('')).to.throw(error('""'));
  12. expect(throwable(10)).to.throw(error('10'));
  13. expect(throwable(0)).to.throw(error('0'));
  14. expect(throwable(true)).to.throw(error('true'));
  15. expect(throwable(false)).to.throw(error('false'));
  16. expect(throwable(null)).to.throw(error('null'));
  17. expect(throwable({})).to.throw(error('Object'));
  18. expect(throwable([])).to.throw(error('Array'));
  19. expect(throwable(undefined)).to.throw(error('undefined'));
  20. expect(throwable(() => undefined)).to.throw(error('Function'));
  21. throwable(HookName.PRE_HANDLER)();
  22. });
  23. it('requires the parameter "hook" to be a Function', function () {
  24. const s = new HookRegistry();
  25. const throwable = v => () => s.addHook(HookName.PRE_HANDLER, v);
  26. const error = v =>
  27. format('The hook "preHandler" should be a Function, but %s given.', v);
  28. expect(throwable('str')).to.throw(error('"str"'));
  29. expect(throwable('')).to.throw(error('""'));
  30. expect(throwable(10)).to.throw(error('10'));
  31. expect(throwable(0)).to.throw(error('0'));
  32. expect(throwable(true)).to.throw(error('true'));
  33. expect(throwable(false)).to.throw(error('false'));
  34. expect(throwable(null)).to.throw(error('null'));
  35. expect(throwable({})).to.throw(error('Object'));
  36. expect(throwable([])).to.throw(error('Array'));
  37. expect(throwable(undefined)).to.throw(error('undefined'));
  38. throwable(() => undefined)();
  39. });
  40. it('requires the parameter "name" to be a supported hook', function () {
  41. const s = new HookRegistry();
  42. const hook = () => undefined;
  43. Object.values(HookName).forEach(name => s.addHook(name, hook));
  44. const throwable = () => s.addHook('unknown', hook);
  45. expect(throwable).to.throw('The hook name "unknown" is not supported.');
  46. });
  47. it('sets the given function to the map array by the hook name', function () {
  48. const s = new HookRegistry();
  49. const name = HookName.PRE_HANDLER;
  50. const hook = () => undefined;
  51. s.addHook(name, hook);
  52. expect(s._hooks.get(name)).to.include(hook);
  53. });
  54. it('returns this', function () {
  55. const s = new HookRegistry();
  56. const hook = () => undefined;
  57. const name = HookName.PRE_HANDLER;
  58. const res = s.addHook(name, hook);
  59. expect(res).to.be.eq(s);
  60. });
  61. });
  62. describe('hasHook', function () {
  63. it('requires the parameter "name" to be a non-empty String', function () {
  64. const s = new HookRegistry();
  65. const throwable = v => () => s.hasHook(v, () => undefined);
  66. const error = v => format('The hook name is required, but %s given.', v);
  67. expect(throwable('')).to.throw(error('""'));
  68. expect(throwable(10)).to.throw(error('10'));
  69. expect(throwable(0)).to.throw(error('0'));
  70. expect(throwable(true)).to.throw(error('true'));
  71. expect(throwable(false)).to.throw(error('false'));
  72. expect(throwable(null)).to.throw(error('null'));
  73. expect(throwable({})).to.throw(error('Object'));
  74. expect(throwable([])).to.throw(error('Array'));
  75. expect(throwable(undefined)).to.throw(error('undefined'));
  76. expect(throwable(() => undefined)).to.throw(error('Function'));
  77. throwable(HookName.PRE_HANDLER)();
  78. });
  79. it('requires the parameter "hook" to be a Function', function () {
  80. const s = new HookRegistry();
  81. const throwable = v => () => s.hasHook(HookName.PRE_HANDLER, v);
  82. const error = v =>
  83. format('The hook "preHandler" should be a Function, but %s given.', v);
  84. expect(throwable('str')).to.throw(error('"str"'));
  85. expect(throwable('')).to.throw(error('""'));
  86. expect(throwable(10)).to.throw(error('10'));
  87. expect(throwable(0)).to.throw(error('0'));
  88. expect(throwable(true)).to.throw(error('true'));
  89. expect(throwable(false)).to.throw(error('false'));
  90. expect(throwable(null)).to.throw(error('null'));
  91. expect(throwable({})).to.throw(error('Object'));
  92. expect(throwable([])).to.throw(error('Array'));
  93. expect(throwable(undefined)).to.throw(error('undefined'));
  94. throwable(() => undefined)();
  95. });
  96. it('requires the parameter "name" to be a supported hook', function () {
  97. const s = new HookRegistry();
  98. const hook = () => undefined;
  99. Object.values(HookName).forEach(name => s.hasHook(name, hook));
  100. const throwable = () => s.hasHook('unknown', hook);
  101. expect(throwable).to.throw('The hook name "unknown" is not supported.');
  102. });
  103. it('returns true if the given hook is set or false', function () {
  104. const s = new HookRegistry();
  105. const name = HookName.PRE_HANDLER;
  106. const hook = () => undefined;
  107. expect(s.hasHook(name, hook)).to.be.false;
  108. s.addHook(name, hook);
  109. expect(s.hasHook(name, hook)).to.be.true;
  110. });
  111. });
  112. describe('getHooks', function () {
  113. it('requires the parameter "name" to be a non-empty String', function () {
  114. const s = new HookRegistry();
  115. const throwable = v => () => s.getHooks(v);
  116. const error = v => format('The hook name is required, but %s given.', v);
  117. expect(throwable('')).to.throw(error('""'));
  118. expect(throwable(10)).to.throw(error('10'));
  119. expect(throwable(0)).to.throw(error('0'));
  120. expect(throwable(true)).to.throw(error('true'));
  121. expect(throwable(false)).to.throw(error('false'));
  122. expect(throwable(null)).to.throw(error('null'));
  123. expect(throwable({})).to.throw(error('Object'));
  124. expect(throwable([])).to.throw(error('Array'));
  125. expect(throwable(undefined)).to.throw(error('undefined'));
  126. expect(throwable(() => undefined)).to.throw(error('Function'));
  127. throwable(HookName.PRE_HANDLER)();
  128. });
  129. it('requires the parameter "name" to be a supported hook', function () {
  130. const s = new HookRegistry();
  131. Object.values(HookName).forEach(name => s.getHooks(name));
  132. const throwable = () => s.getHooks('unknown');
  133. expect(throwable).to.throw('The hook name "unknown" is not supported.');
  134. });
  135. it('returns existing hooks', function () {
  136. const s = new HookRegistry();
  137. const hook = () => undefined;
  138. const name = HookName.PRE_HANDLER;
  139. const res1 = s.getHooks(name);
  140. expect(res1).to.be.eql([]);
  141. s.addHook(name, hook);
  142. const res2 = s.getHooks(name);
  143. expect(res2).to.have.length(1);
  144. expect(res2[0]).to.be.eq(hook);
  145. });
  146. it('returns an empty array if no hook exists', function () {
  147. const s = new HookRegistry();
  148. const res = s.getHooks(HookName.PRE_HANDLER);
  149. expect(res).to.be.eql([]);
  150. });
  151. });
  152. });