import { ComponentFixture, TestBed } from '@angular/core/testing';
import { HttpResponse } from '@angular/common/http';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { FormBuilder } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { of, Subject, from } from 'rxjs';

import { IMetadataProperty } from 'app/entities/metadata-property/metadata-property.model';
import { MetadataPropertyService } from 'app/entities/metadata-property/service/metadata-property.service';
import { OrganizationTypeService } from '../service/organization-type.service';
import { IOrganizationType } from '../organization-type.model';
import { OrganizationTypeFormService } from './organization-type-form.service';

import { OrganizationTypeUpdateComponent } from './organization-type-update.component';

describe('OrganizationType Management Update Component', () => {
  let comp: OrganizationTypeUpdateComponent;
  let fixture: ComponentFixture<OrganizationTypeUpdateComponent>;
  let activatedRoute: ActivatedRoute;
  let organizationTypeFormService: OrganizationTypeFormService;
  let organizationTypeService: OrganizationTypeService;
  let metadataPropertyService: MetadataPropertyService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule, OrganizationTypeUpdateComponent],
      providers: [
        FormBuilder,
        {
          provide: ActivatedRoute,
          useValue: {
            params: from([{}]),
          },
        },
      ],
    })
      .overrideTemplate(OrganizationTypeUpdateComponent, '')
      .compileComponents();

    fixture = TestBed.createComponent(OrganizationTypeUpdateComponent);
    activatedRoute = TestBed.inject(ActivatedRoute);
    organizationTypeFormService = TestBed.inject(OrganizationTypeFormService);
    organizationTypeService = TestBed.inject(OrganizationTypeService);
    metadataPropertyService = TestBed.inject(MetadataPropertyService);

    comp = fixture.componentInstance;
  });

  describe('ngOnInit', () => {
    it('Should call MetadataProperty query and add missing value', () => {
      const organizationType: IOrganizationType = { id: 456 };
      const metadataProperties: IMetadataProperty[] = [{ id: 4515 }];
      organizationType.metadataProperties = metadataProperties;

      const metadataPropertyCollection: IMetadataProperty[] = [{ id: 16236 }];
      jest.spyOn(metadataPropertyService, 'query').mockReturnValue(of(new HttpResponse({ body: metadataPropertyCollection })));
      const additionalMetadataProperties = [...metadataProperties];
      const expectedCollection: IMetadataProperty[] = [...additionalMetadataProperties, ...metadataPropertyCollection];
      jest.spyOn(metadataPropertyService, 'addMetadataPropertyToCollectionIfMissing').mockReturnValue(expectedCollection);

      activatedRoute.data = of({ organizationType });
      comp.ngOnInit();

      expect(metadataPropertyService.query).toHaveBeenCalled();
      expect(metadataPropertyService.addMetadataPropertyToCollectionIfMissing).toHaveBeenCalledWith(
        metadataPropertyCollection,
        ...additionalMetadataProperties.map(expect.objectContaining),
      );
      expect(comp.metadataPropertiesSharedCollection).toEqual(expectedCollection);
    });

    it('Should update editForm', () => {
      const organizationType: IOrganizationType = { id: 456 };
      const metadataProperties: IMetadataProperty = { id: 29219 };
      organizationType.metadataProperties = [metadataProperties];

      activatedRoute.data = of({ organizationType });
      comp.ngOnInit();

      expect(comp.metadataPropertiesSharedCollection).toContain(metadataProperties);
      expect(comp.organizationType).toEqual(organizationType);
    });
  });

  describe('save', () => {
    it('Should call update service on save for existing entity', () => {
      // GIVEN
      const saveSubject = new Subject<HttpResponse<IOrganizationType>>();
      const organizationType = { id: 123 };
      jest.spyOn(organizationTypeFormService, 'getOrganizationType').mockReturnValue(organizationType);
      jest.spyOn(organizationTypeService, 'update').mockReturnValue(saveSubject);
      jest.spyOn(comp, 'previousState');
      activatedRoute.data = of({ organizationType });
      comp.ngOnInit();

      // WHEN
      comp.save();
      expect(comp.isSaving).toEqual(true);
      saveSubject.next(new HttpResponse({ body: organizationType }));
      saveSubject.complete();

      // THEN
      expect(organizationTypeFormService.getOrganizationType).toHaveBeenCalled();
      expect(comp.previousState).toHaveBeenCalled();
      expect(organizationTypeService.update).toHaveBeenCalledWith(expect.objectContaining(organizationType));
      expect(comp.isSaving).toEqual(false);
    });

    it('Should call create service on save for new entity', () => {
      // GIVEN
      const saveSubject = new Subject<HttpResponse<IOrganizationType>>();
      const organizationType = { id: 123 };
      jest.spyOn(organizationTypeFormService, 'getOrganizationType').mockReturnValue({ id: null });
      jest.spyOn(organizationTypeService, 'create').mockReturnValue(saveSubject);
      jest.spyOn(comp, 'previousState');
      activatedRoute.data = of({ organizationType: null });
      comp.ngOnInit();

      // WHEN
      comp.save();
      expect(comp.isSaving).toEqual(true);
      saveSubject.next(new HttpResponse({ body: organizationType }));
      saveSubject.complete();

      // THEN
      expect(organizationTypeFormService.getOrganizationType).toHaveBeenCalled();
      expect(organizationTypeService.create).toHaveBeenCalled();
      expect(comp.isSaving).toEqual(false);
      expect(comp.previousState).toHaveBeenCalled();
    });

    it('Should set isSaving to false on error', () => {
      // GIVEN
      const saveSubject = new Subject<HttpResponse<IOrganizationType>>();
      const organizationType = { id: 123 };
      jest.spyOn(organizationTypeService, 'update').mockReturnValue(saveSubject);
      jest.spyOn(comp, 'previousState');
      activatedRoute.data = of({ organizationType });
      comp.ngOnInit();

      // WHEN
      comp.save();
      expect(comp.isSaving).toEqual(true);
      saveSubject.error('This is an error!');

      // THEN
      expect(organizationTypeService.update).toHaveBeenCalled();
      expect(comp.isSaving).toEqual(false);
      expect(comp.previousState).not.toHaveBeenCalled();
    });
  });

  describe('Compare relationships', () => {
    describe('compareMetadataProperty', () => {
      it('Should forward to metadataPropertyService', () => {
        const entity = { id: 123 };
        const entity2 = { id: 456 };
        jest.spyOn(metadataPropertyService, 'compareMetadataProperty');
        comp.compareMetadataProperty(entity, entity2);
        expect(metadataPropertyService.compareMetadataProperty).toHaveBeenCalledWith(entity, entity2);
      });
    });
  });
});
