service.spec.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import {expect} from 'chai';
  2. import {Service} from './service.js';
  3. describe('Service', function () {
  4. describe('constructor', function () {
  5. it('sets an empty service map by default', function () {
  6. const service = new Service();
  7. expect(service._services).to.be.instanceof(Map);
  8. expect(service._services).to.be.empty;
  9. });
  10. it('sets a given service map', function () {
  11. const map = new Map();
  12. const service = new Service(map);
  13. expect(service._services).to.be.eq(map);
  14. });
  15. });
  16. describe('get', function () {
  17. it('returns a new service from a given constructor as a singleton', function () {
  18. let executed = 0;
  19. class MyService extends Service {
  20. constructor() {
  21. super();
  22. ++executed;
  23. }
  24. }
  25. const service = new Service();
  26. const myService1 = service.get(MyService);
  27. const myService2 = service.get(MyService);
  28. expect(myService1).to.be.instanceof(MyService);
  29. expect(myService2).to.be.instanceof(MyService);
  30. expect(myService1).to.be.eq(myService2);
  31. expect(executed).to.be.eq(1);
  32. });
  33. });
  34. });