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 { IOrganizationType } from 'app/entities/organization-type/organization-type.model';
import { OrganizationTypeService } from 'app/entities/organization-type/service/organization-type.service';
import { MetadataPropertyService } from '../service/metadata-property.service';
import { IMetadataProperty } from '../metadata-property.model';
import { MetadataPropertyFormService } from './metadata-property-form.service';

import { MetadataPropertyUpdateComponent } from './metadata-property-update.component';

describe('MetadataProperty Management Update Component', () => {
  let comp: MetadataPropertyUpdateComponent;
  let fixture: ComponentFixture<MetadataPropertyUpdateComponent>;
  let activatedRoute: ActivatedRoute;
  let metadataPropertyFormService: MetadataPropertyFormService;
  let metadataPropertyService: MetadataPropertyService;
  let organizationTypeService: OrganizationTypeService;

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

    fixture = TestBed.createComponent(MetadataPropertyUpdateComponent);
    activatedRoute = TestBed.inject(ActivatedRoute);
    metadataPropertyFormService = TestBed.inject(MetadataPropertyFormService);
    metadataPropertyService = TestBed.inject(MetadataPropertyService);
    organizationTypeService = TestBed.inject(OrganizationTypeService);

    comp = fixture.componentInstance;
  });

  describe('ngOnInit', () => {
    it('Should call OrganizationType query and add missing value', () => {
      const metadataProperty: IMetadataProperty = { id: 456 };
      const organizationTypes: IOrganizationType[] = [{ id: 25562 }];
      metadataProperty.organizationTypes = organizationTypes;

      const organizationTypeCollection: IOrganizationType[] = [{ id: 32124 }];
      jest.spyOn(organizationTypeService, 'query').mockReturnValue(of(new HttpResponse({ body: organizationTypeCollection })));
      const additionalOrganizationTypes = [...organizationTypes];
      const expectedCollection: IOrganizationType[] = [...additionalOrganizationTypes, ...organizationTypeCollection];
      jest.spyOn(organizationTypeService, 'addOrganizationTypeToCollectionIfMissing').mockReturnValue(expectedCollection);

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

      expect(organizationTypeService.query).toHaveBeenCalled();
      expect(organizationTypeService.addOrganizationTypeToCollectionIfMissing).toHaveBeenCalledWith(
        organizationTypeCollection,
        ...additionalOrganizationTypes.map(expect.objectContaining),
      );
      expect(comp.organizationTypesSharedCollection).toEqual(expectedCollection);
    });

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

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

      expect(comp.organizationTypesSharedCollection).toContain(organizationType);
      expect(comp.metadataProperty).toEqual(metadataProperty);
    });
  });

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

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

      // THEN
      expect(metadataPropertyFormService.getMetadataProperty).toHaveBeenCalled();
      expect(comp.previousState).toHaveBeenCalled();
      expect(metadataPropertyService.update).toHaveBeenCalledWith(expect.objectContaining(metadataProperty));
      expect(comp.isSaving).toEqual(false);
    });

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

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

      // THEN
      expect(metadataPropertyFormService.getMetadataProperty).toHaveBeenCalled();
      expect(metadataPropertyService.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<IMetadataProperty>>();
      const metadataProperty = { id: 123 };
      jest.spyOn(metadataPropertyService, 'update').mockReturnValue(saveSubject);
      jest.spyOn(comp, 'previousState');
      activatedRoute.data = of({ metadataProperty });
      comp.ngOnInit();

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

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

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