adapter.spec.js 5.1 KB

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