adapter.spec.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import {expect} from 'chai';
  2. import {chai} from '../chai.js';
  3. import {Schema} from '../schema.js';
  4. import {Adapter} from './adapter.js';
  5. import {Service} from '@e22m4u/js-service';
  6. import {ServiceContainer} from '@e22m4u/js-service';
  7. import {InclusionDecorator} from './decorator/index.js';
  8. import {DefaultValuesDecorator} from './decorator/index.js';
  9. import {DataValidationDecorator} from './decorator/index.js';
  10. import {DataSanitizingDecorator} from './decorator/index.js';
  11. import {FieldsFilteringDecorator} from './decorator/index.js';
  12. import {DataTransformationDecorator} from './decorator/index.js';
  13. import {PropertyUniquenessDecorator} from './decorator/index.js';
  14. const sandbox = chai.spy.sandbox();
  15. describe('Adapter', function () {
  16. it('exposes kind getter', function () {
  17. const adapter = new Adapter();
  18. expect(adapter.kind).to.be.eq('Adapter');
  19. expect(Adapter.prototype.kind).to.be.eq('Adapter');
  20. });
  21. describe('constructor', function () {
  22. afterEach(function () {
  23. sandbox.restore();
  24. });
  25. it('inherits from the Service class', function () {
  26. const adapter = new Adapter();
  27. expect(adapter).to.be.instanceof(Service);
  28. });
  29. it('sets given service container and settings', function () {
  30. const container = new ServiceContainer();
  31. const settings = {};
  32. const adapter = new Adapter(container, settings);
  33. expect(adapter.container).to.be.eq(container);
  34. expect(adapter._settings).to.be.eq(settings);
  35. });
  36. it('decorates only extended adapter', function () {
  37. const schema = new Schema();
  38. const dec1 = schema.getService(DataSanitizingDecorator);
  39. const dec2 = schema.getService(DefaultValuesDecorator);
  40. const dec3 = schema.getService(DataTransformationDecorator);
  41. const dec4 = schema.getService(DataValidationDecorator);
  42. const dec5 = schema.getService(PropertyUniquenessDecorator);
  43. const dec6 = schema.getService(FieldsFilteringDecorator);
  44. const dec7 = schema.getService(InclusionDecorator);
  45. const order = [];
  46. const decorate = function (ctx) {
  47. expect(ctx).to.be.instanceof(Adapter);
  48. order.push(this);
  49. };
  50. sandbox.on(dec1, 'decorate', decorate);
  51. sandbox.on(dec2, 'decorate', decorate);
  52. sandbox.on(dec3, 'decorate', decorate);
  53. sandbox.on(dec4, 'decorate', decorate);
  54. sandbox.on(dec5, 'decorate', decorate);
  55. sandbox.on(dec6, 'decorate', decorate);
  56. sandbox.on(dec7, 'decorate', decorate);
  57. new Adapter(schema.container);
  58. expect(order).to.be.empty;
  59. expect(dec1.decorate).to.be.not.called;
  60. expect(dec2.decorate).to.be.not.called;
  61. expect(dec3.decorate).to.be.not.called;
  62. expect(dec4.decorate).to.be.not.called;
  63. expect(dec5.decorate).to.be.not.called;
  64. expect(dec6.decorate).to.be.not.called;
  65. expect(dec7.decorate).to.be.not.called;
  66. class ExtendedAdapter extends Adapter {}
  67. new ExtendedAdapter(schema.container);
  68. expect(order[0]).to.be.eql(dec1);
  69. expect(order[1]).to.be.eql(dec2);
  70. expect(order[2]).to.be.eql(dec3);
  71. expect(order[3]).to.be.eql(dec4);
  72. expect(order[4]).to.be.eql(dec5);
  73. expect(order[5]).to.be.eql(dec6);
  74. expect(order[6]).to.be.eql(dec7);
  75. expect(dec1.decorate).to.be.called.once;
  76. expect(dec2.decorate).to.be.called.once;
  77. expect(dec3.decorate).to.be.called.once;
  78. expect(dec4.decorate).to.be.called.once;
  79. expect(dec5.decorate).to.be.called.once;
  80. expect(dec6.decorate).to.be.called.once;
  81. expect(dec7.decorate).to.be.called.once;
  82. });
  83. });
  84. describe('create', function () {
  85. it('throws the "Not implemented"', function () {
  86. const adapter = new Adapter();
  87. const throwable = () => adapter.create();
  88. expect(throwable).to.throw('Adapter.create is not implemented.');
  89. });
  90. });
  91. describe('replaceById', function () {
  92. it('throws the "Not implemented"', function () {
  93. const adapter = new Adapter();
  94. const throwable = () => adapter.replaceById();
  95. expect(throwable).to.throw('Adapter.replaceById is not implemented.');
  96. });
  97. });
  98. describe('replaceOrCreate', function () {
  99. it('throws the "Not implemented"', function () {
  100. const adapter = new Adapter();
  101. const throwable = () => adapter.replaceOrCreate();
  102. expect(throwable).to.throw('Adapter.replaceOrCreate is not implemented.');
  103. });
  104. });
  105. describe('patchById', function () {
  106. it('throws the "Not implemented"', function () {
  107. const adapter = new Adapter();
  108. const throwable = () => adapter.patchById();
  109. expect(throwable).to.throw('Adapter.patchById is not implemented.');
  110. });
  111. });
  112. describe('find', function () {
  113. it('throws the "Not implemented"', function () {
  114. const adapter = new Adapter();
  115. const throwable = () => adapter.find();
  116. expect(throwable).to.throw('Adapter.find is not implemented.');
  117. });
  118. });
  119. describe('findById', function () {
  120. it('throws the "Not implemented"', function () {
  121. const adapter = new Adapter();
  122. const throwable = () => adapter.findById();
  123. expect(throwable).to.throw('Adapter.findById is not implemented.');
  124. });
  125. });
  126. describe('delete', function () {
  127. it('throws the "Not implemented"', function () {
  128. const adapter = new Adapter();
  129. const throwable = () => adapter.delete();
  130. expect(throwable).to.throw('Adapter.delete is not implemented.');
  131. });
  132. });
  133. describe('deleteById', function () {
  134. it('throws the "Not implemented"', function () {
  135. const adapter = new Adapter();
  136. const throwable = () => adapter.deleteById();
  137. expect(throwable).to.throw('Adapter.deleteById is not implemented.');
  138. });
  139. });
  140. describe('exists', function () {
  141. it('throws the "Not implemented"', function () {
  142. const adapter = new Adapter();
  143. const throwable = () => adapter.exists();
  144. expect(throwable).to.throw('Adapter.exists is not implemented.');
  145. });
  146. });
  147. describe('count', function () {
  148. it('throws the "Not implemented"', function () {
  149. const adapter = new Adapter();
  150. const throwable = () => adapter.count();
  151. expect(throwable).to.throw('Adapter.count is not implemented.');
  152. });
  153. });
  154. });