service.spec.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {expect} from 'chai';
  2. import {Service} from './service.js';
  3. import {SERVICE_CLASS_NAME} from './service.js';
  4. import {ServiceContainer} from './service-container.js';
  5. import {SERVICE_CONTAINER_CLASS_NAME} from './service-container.js';
  6. describe('Service', function () {
  7. it('exposes static property "kinds"', function () {
  8. expect(Service.kinds).to.be.eql([SERVICE_CLASS_NAME]);
  9. const MyService = class extends Service {};
  10. expect(MyService.kinds).to.be.eql([SERVICE_CLASS_NAME]);
  11. });
  12. describe('constructor', function () {
  13. it('instantiates with a services container', function () {
  14. const service = new Service();
  15. expect(service.container).to.be.instanceof(ServiceContainer);
  16. });
  17. it('sets a given service container', function () {
  18. const container = new ServiceContainer();
  19. const service = new Service(container);
  20. expect(service.container).to.be.eq(container);
  21. });
  22. it('sets a given object as service container by the kinds property', function () {
  23. class MyServiceContainer {
  24. static kinds = [SERVICE_CONTAINER_CLASS_NAME];
  25. }
  26. const container = new MyServiceContainer();
  27. const service = new Service(container);
  28. expect(service.container).to.be.eq(container);
  29. });
  30. });
  31. describe('getService', function () {
  32. it('calls the container "get" method', function () {
  33. const service = new Service();
  34. service.container.get = function (ctor, ...args) {
  35. expect(ctor).to.be.eq(Date);
  36. expect(args).to.be.eql(['foo', 'bar', 'baz']);
  37. return 'OK';
  38. };
  39. const res = service.getService(Date, 'foo', 'bar', 'baz');
  40. expect(res).to.be.eq('OK');
  41. });
  42. });
  43. describe('getRegisteredService', function () {
  44. it('calls the container "getRegisteredService" method', function () {
  45. const service = new Service();
  46. service.container.getRegistered = function (ctor, ...args) {
  47. expect(ctor).to.be.eq(Date);
  48. expect(args).to.be.eql(['foo', 'bar', 'baz']);
  49. return 'OK';
  50. };
  51. const res = service.getRegisteredService(Date, 'foo', 'bar', 'baz');
  52. expect(res).to.be.eq('OK');
  53. });
  54. });
  55. describe('hasService', function () {
  56. it('calls the container "has" method', function () {
  57. const service = new Service();
  58. service.container.has = function (ctor) {
  59. expect(ctor).to.be.eq(Date);
  60. return 'OK';
  61. };
  62. const res = service.hasService(Date);
  63. expect(res).to.be.eq('OK');
  64. });
  65. });
  66. describe('addService', function () {
  67. it('calls the container "add" method', function () {
  68. const service = new Service();
  69. service.container.add = function (ctor, ...args) {
  70. expect(ctor).to.be.eq(Date);
  71. expect(args).to.be.eql(['foo', 'bar', 'baz']);
  72. };
  73. const res = service.addService(Date, 'foo', 'bar', 'baz');
  74. expect(res).to.be.eq(service);
  75. });
  76. });
  77. describe('useService', function () {
  78. it('calls the container "use" method', function () {
  79. const service = new Service();
  80. service.container.use = function (ctor, ...args) {
  81. expect(ctor).to.be.eq(Date);
  82. expect(args).to.be.eql(['foo', 'bar', 'baz']);
  83. };
  84. const res = service.addService(Date, 'foo', 'bar', 'baz');
  85. expect(res).to.be.eq(service);
  86. });
  87. });
  88. describe('setService', function () {
  89. it('calls the container "set" method', function () {
  90. const service = new Service();
  91. const date = new Date();
  92. service.container.set = function (ctor, input) {
  93. expect(ctor).to.be.eq(Date);
  94. expect(input).to.be.eq(date);
  95. };
  96. const res = service.addService(Date, date);
  97. expect(res).to.be.eq(service);
  98. });
  99. });
  100. });