All files / platform/core/src/services ServicesManager.test.js

0% Statements 0/46
100% Branches 0/0
0% Functions 0/12
0% Lines 0/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                                                                                                                                                                                   
import ServicesManager from './ServicesManager';
import log from '../log';
 
jest.mock('./../log');
 
describe('ServicesManager', () => {
  let servicesManager, commandsManager;
 
  beforeEach(() => {
    commandsManager = {
      createContext: jest.fn(),
      getContext: jest.fn(),
      registerCommand: jest.fn(),
    };
    servicesManager = new ServicesManager(commandsManager);
    log.warn.mockClear();
    jest.clearAllMocks();
  });
 
  describe('registerServices()', () => {
    it('calls registerService() for each service', () => {
      servicesManager.registerService = jest.fn();
 
      servicesManager.registerServices([
        { name: 'UINotificationTestService', create: jest.fn() },
        { name: 'UIModalTestService', create: jest.fn() },
      ]);
 
      expect(servicesManager.registerService.mock.calls.length).toBe(2);
    });
 
    it('calls registerService() for each service passing its configuration if tuple', () => {
      servicesManager.registerService = jest.fn();
      const fakeConfiguration = { testing: true };
 
      servicesManager.registerServices([
        { name: 'UINotificationTestService', create: jest.fn() },
        [{ name: 'UIModalTestService', create: jest.fn() }, fakeConfiguration],
      ]);
 
      expect(servicesManager.registerService.mock.calls[1][1]).toEqual(fakeConfiguration);
    });
  });
 
  describe('registerService()', () => {
    const fakeService = { name: 'UINotificationService', create: jest.fn() };
 
    it('logs a warning if the service is null or undefined', () => {
      const undefinedService = undefined;
      const nullService = null;
 
      servicesManager.registerService(undefinedService);
      servicesManager.registerService(nullService);
 
      expect(log.warn.mock.calls.length).toBe(2);
    });
 
    it('logs a warning if the service does not have a name', () => {
      const serviceWithEmptyName = { name: '', create: jest.fn() };
      const serviceWithoutName = { create: jest.fn() };
 
      servicesManager.registerService(serviceWithEmptyName);
      servicesManager.registerService(serviceWithoutName);
 
      expect(log.warn.mock.calls.length).toBe(2);
    });
 
    it('logs a warning if the service does not have a create factory function', () => {
      const serviceWithoutCreate = { name: 'UINotificationService' };
 
      servicesManager.registerService(serviceWithoutCreate);
 
      expect(log.warn.mock.calls.length).toBe(1);
    });
 
    it('tracks which services have been registered', () => {
      servicesManager.registerService(fakeService);
 
      expect(servicesManager.registeredServiceNames).toContain(fakeService.name);
    });
 
    it('logs a warning if the service has an name that has already been registered', () => {
      servicesManager.registerService(fakeService);
      servicesManager.registerService(fakeService);
 
      expect(log.warn.mock.calls.length).toBe(1);
    });
 
    it('pass dependencies and configuration to service create factory function', () => {
      const configuration = { config: 'Some configuration' };
 
      servicesManager.registerService(fakeService, configuration);
 
      expect(fakeService.create.mock.calls[0][0].configuration.config).toBe(configuration.config);
    });
  });
});