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

import { waitForAsync } from '@angular/core/testing';
import Feature from 'ol/Feature';
import { Entity, FeatureEntityMap } from './feature-entity-map';

describe('FeatureEntityMap', () => {
  const createFeature = (id: string) => {
    const feature = new Feature();
    feature.setId(id);
    return feature;
  };

  let component: FeatureEntityMap<Entity>;
  const entity = <Entity>{ id: 'e1' };
  const features = [createFeature('e1f1'), createFeature('e1f2')];

  const entity2 = <Entity>{ id: 'e2' };
  const features2 = [createFeature('e2f1')];

  beforeEach(
    waitForAsync(() => {
      component = new FeatureEntityMap();
      component.set(features, entity);
      component.set(features2, entity2);
    })
  );

  // eslint-disable-next-line no-empty-function
  beforeEach(() => {});

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

  it('should be set mapping both ways', () => {
    expect(component.getEntityOfFeature(features[0])).toEqual(entity);
    expect(component.getEntityOfFeature(features[1])).toEqual(entity);
    expect(component.getFeaturesOfEntity(entity)).toEqual(features);
  });

  it('should be replace mapping both ways', () => {
    const featuresNew = [createFeature('e1ft1'), createFeature('e1ft2')];
    component.set(featuresNew, entity);

    expect(component.getEntityOfFeature(featuresNew[0])).toEqual(entity);
    expect(component.getEntityOfFeature(featuresNew[1])).toEqual(entity);
    expect(component.getFeaturesOfEntity(entity)).toEqual(featuresNew);
    expect(component.getEntityOfFeature(features[0])).toBeUndefined();
    expect(component.getEntityOfFeature(features[1])).toBeUndefined();
  });

  it('should clear mapping both ways', () => {
    component.clearFeatures([features[0]]);
    expect(component.getEntityOfFeature(features[0])).toBeUndefined();
    expect(component.getEntityOfFeature(features[1])).toEqual(entity);
    expect(component.getFeaturesOfEntity(entity)).toEqual([features[1]]);
  });

  it('should return entity of feature', () => {
    expect(component.getEntityOfFeature(features[0])).toEqual(entity);
  });

  it('should return empty feature list for unknown entity', () => {
    expect(component.getFeaturesOfEntity(<Entity>{ id: 'unknown' })).toEqual([]);
  });

  it('should return features of entity', () => {
    expect(component.getFeaturesOfEntity(entity)).toEqual(features);
  });

  it('should return distinct entities of features', () => {
    expect(component.getEntitiesOfFeatures(features.concat(features2))).toEqual([entity, entity2]);
  });
});
