service.spec.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {expect} from 'chai';
  2. import {Service} from './service.js';
  3. import {ServiceContainer} from './service-container.js';
  4. describe('Service', function () {
  5. describe('constructor', function () {
  6. it('instantiates with a services container', function () {
  7. const service = new Service();
  8. expect(service.container).to.be.instanceof(ServiceContainer);
  9. });
  10. it('sets a given service container', function () {
  11. const container = new ServiceContainer();
  12. const service = new Service(container);
  13. expect(service.container).to.be.eq(container);
  14. });
  15. });
  16. describe('getService', function () {
  17. it('calls the container "get" method', function () {
  18. const service = new Service();
  19. service.container.get = function (ctor, ...args) {
  20. expect(ctor).to.be.eq(Date);
  21. expect(args).to.be.eql(['foo', 'bar', 'baz']);
  22. return 'OK';
  23. };
  24. const res = service.getService(Date, 'foo', 'bar', 'baz');
  25. expect(res).to.be.eq('OK');
  26. });
  27. });
  28. describe('hasService', function () {
  29. it('calls the container "has" method', function () {
  30. const service = new Service();
  31. service.container.has = function (ctor) {
  32. expect(ctor).to.be.eq(Date);
  33. return 'OK';
  34. };
  35. const res = service.hasService(Date);
  36. expect(res).to.be.eq('OK');
  37. });
  38. });
  39. describe('addService', function () {
  40. it('calls the container "add" method', function () {
  41. const service = new Service();
  42. service.container.add = function (ctor, ...args) {
  43. expect(ctor).to.be.eq(Date);
  44. expect(args).to.be.eql(['foo', 'bar', 'baz']);
  45. };
  46. const res = service.addService(Date, 'foo', 'bar', 'baz');
  47. expect(res).to.be.eq(service);
  48. });
  49. });
  50. describe('useService', function () {
  51. it('calls the container "use" method', function () {
  52. const service = new Service();
  53. service.container.use = function (ctor, ...args) {
  54. expect(ctor).to.be.eq(Date);
  55. expect(args).to.be.eql(['foo', 'bar', 'baz']);
  56. };
  57. const res = service.addService(Date, 'foo', 'bar', 'baz');
  58. expect(res).to.be.eq(service);
  59. });
  60. });
  61. describe('setService', function () {
  62. it('calls the container "set" method', function () {
  63. const service = new Service();
  64. const date = new Date();
  65. service.container.set = function (ctor, input) {
  66. expect(ctor).to.be.eq(Date);
  67. expect(input).to.be.eq(date);
  68. };
  69. const res = service.addService(Date, date);
  70. expect(res).to.be.eq(service);
  71. });
  72. });
  73. });