import { App } from './app';
import { Utility } from './core/utility';

@Utility({ selector: 'util1' })
class TestUtil1 { }

@Utility({ selector: 'util2' })
class TestUtil2 { }

const TEST_UTILS = [
  TestUtil1,
  TestUtil2,
];

describe('App', () => {
  let app;

  beforeEach(() => {
    app = new App();
  });

  it('should create', () => {
    expect(app).toBeTruthy();
  });

  it('should setup all utlites when page is done loading', () => {
    spyOn(app.utilRegistry, 'setupAll');
    document.dispatchEvent(new Event('DOMContentLoaded'));
    expect(app.utilRegistry.setupAll).toHaveBeenCalled();
  });

  describe('provides services', () => {
    it('HttpClient as httpClient', () => {
      expect(app.httpClient).toBeTruthy();
    });

    it('HtmlHelpers as htmlHelpers', () => {
      expect(app.htmlHelpers).toBeTruthy();
    });

    it('I18n as i18n', () => {
      expect(app.i18n).toBeTruthy();
    });

    it('UtilRegistry as utilRegistry', () => {
      expect(app.utilRegistry).toBeTruthy();
    });
  });

  describe('registerUtilities()', () => {
    it('should register the given utilities', () => {
      spyOn(app.utilRegistry, 'register');
      app.registerUtilities(TEST_UTILS);
      expect(app.utilRegistry.register.calls.count()).toBe(TEST_UTILS.length);
      expect(app.utilRegistry.register.calls.argsFor(0)).toEqual([TEST_UTILS[0]]);
      expect(app.utilRegistry.register.calls.argsFor(1)).toEqual([TEST_UTILS[1]]);
    });

    it('should throw an error if not passed an array of utilities', () => {
      expect(() => {
        app.registerUtilities({});
      }).toThrow();
    });
  });
});
