/*
 * Copyright (C) 2018 Robert Bosch GmbH Copyright (C) 2018 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { TestBed } from '@angular/core/testing';
import { WidgetDefinition } from '../models/widget-definition';
import { CatalogConf } from '../models/catalog-conf';
import { TestTextContentComponent, TestTextWidgetUserSetting } from '../widget/test-text-widget-component';

import { WidgetDefinitionService } from './widget-definition.service';

describe('WidgetDefinitionService', () => {
  let service: WidgetDefinitionService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [WidgetDefinitionService],
    });

    service = TestBed.inject(WidgetDefinitionService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('#defineWidget should save WidgetDefinitions', () => {
    const def = new WidgetDefinition<TestTextContentComponent, TestTextWidgetUserSetting, any>(
      'id1',
      {} as CatalogConf,
      'TextWidget'
    );
    const oldLength = service.getWidgetDefinitions().length;
    service.defineWidget(def);
    const currentDefs = service.getWidgetDefinitions();
    const newLength = currentDefs.length;
    expect(newLength).toBe(oldLength + 1);
    currentDefs.forEach((value) => {
      if (value === def) {
        expect(value).toBe(def);
      }
    });
  });

  // it('#getWidgetDefinitions should return all WidgetDefinitions', () => {
  //   const def = new WidgetDefinition<TestTextContentComponent, TestTextWidgetUserSetting, any>('id1', {} as CatalogConf, 'TextWidget');
  //   const def1 = new WidgetDefinition<HighchartsContentComponent, any, any>('id2', {} as CatalogConf, 'HighchartsConfig');
  //   const oldLength = service.getWidgetDefinitions().length;
  //   service.defineWidget(def);
  //   service.defineWidget(def1);
  //   const res = service.getWidgetDefinitions();
  //   const newLength = res.length;
  //   expect(newLength).toBe(oldLength + 2);
  // });

  it('#getWidgetDefinitions should return a single WidgetDefinition', () => {
    const def = new WidgetDefinition<TestTextContentComponent, TestTextWidgetUserSetting, any>(
      'id1',
      {} as CatalogConf,
      'TextWidget'
    );
    service.defineWidget(def);
    const res = service.getWidgetDefinitions('id1');
    res.forEach((value) => {
      expect(value).toBe(def);
    });
  });
});
