service.spec.js 2.2 KB

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