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 { IVariableScope } from 'app/entities/variable-scope/variable-scope.model';
import { VariableScopeService } from 'app/entities/variable-scope/service/variable-scope.service';
import { IVariableCategory } from 'app/entities/variable-category/variable-category.model';
import { VariableCategoryService } from 'app/entities/variable-category/service/variable-category.service';
import { IEmissionFactor } from '../emission-factor.model';
import { EmissionFactorService } from '../service/emission-factor.service';
import { EmissionFactorFormService } from './emission-factor-form.service';

import { EmissionFactorUpdateComponent } from './emission-factor-update.component';

describe('EmissionFactor Management Update Component', () => {
  let comp: EmissionFactorUpdateComponent;
  let fixture: ComponentFixture<EmissionFactorUpdateComponent>;
  let activatedRoute: ActivatedRoute;
  let emissionFactorFormService: EmissionFactorFormService;
  let emissionFactorService: EmissionFactorService;
  let variableScopeService: VariableScopeService;
  let variableCategoryService: VariableCategoryService;

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

    fixture = TestBed.createComponent(EmissionFactorUpdateComponent);
    activatedRoute = TestBed.inject(ActivatedRoute);
    emissionFactorFormService = TestBed.inject(EmissionFactorFormService);
    emissionFactorService = TestBed.inject(EmissionFactorService);
    variableScopeService = TestBed.inject(VariableScopeService);
    variableCategoryService = TestBed.inject(VariableCategoryService);

    comp = fixture.componentInstance;
  });

  describe('ngOnInit', () => {
    it('Should call VariableScope query and add missing value', () => {
      const emissionFactor: IEmissionFactor = { id: 456 };
      const variableScope: IVariableScope = { id: 17355 };
      emissionFactor.variableScope = variableScope;

      const variableScopeCollection: IVariableScope[] = [{ id: 28014 }];
      jest.spyOn(variableScopeService, 'query').mockReturnValue(of(new HttpResponse({ body: variableScopeCollection })));
      const additionalVariableScopes = [variableScope];
      const expectedCollection: IVariableScope[] = [...additionalVariableScopes, ...variableScopeCollection];
      jest.spyOn(variableScopeService, 'addVariableScopeToCollectionIfMissing').mockReturnValue(expectedCollection);

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

      expect(variableScopeService.query).toHaveBeenCalled();
      expect(variableScopeService.addVariableScopeToCollectionIfMissing).toHaveBeenCalledWith(
        variableScopeCollection,
        ...additionalVariableScopes.map(expect.objectContaining),
      );
      expect(comp.variableScopesSharedCollection).toEqual(expectedCollection);
    });

    it('Should call VariableCategory query and add missing value', () => {
      const emissionFactor: IEmissionFactor = { id: 456 };
      const variableCategory: IVariableCategory = { id: 29593 };
      emissionFactor.variableCategory = variableCategory;

      const variableCategoryCollection: IVariableCategory[] = [{ id: 12963 }];
      jest.spyOn(variableCategoryService, 'query').mockReturnValue(of(new HttpResponse({ body: variableCategoryCollection })));
      const additionalVariableCategories = [variableCategory];
      const expectedCollection: IVariableCategory[] = [...additionalVariableCategories, ...variableCategoryCollection];
      jest.spyOn(variableCategoryService, 'addVariableCategoryToCollectionIfMissing').mockReturnValue(expectedCollection);

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

      expect(variableCategoryService.query).toHaveBeenCalled();
      expect(variableCategoryService.addVariableCategoryToCollectionIfMissing).toHaveBeenCalledWith(
        variableCategoryCollection,
        ...additionalVariableCategories.map(expect.objectContaining),
      );
      expect(comp.variableCategoriesSharedCollection).toEqual(expectedCollection);
    });

    it('Should update editForm', () => {
      const emissionFactor: IEmissionFactor = { id: 456 };
      const variableScope: IVariableScope = { id: 17239 };
      emissionFactor.variableScope = variableScope;
      const variableCategory: IVariableCategory = { id: 21647 };
      emissionFactor.variableCategory = variableCategory;

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

      expect(comp.variableScopesSharedCollection).toContain(variableScope);
      expect(comp.variableCategoriesSharedCollection).toContain(variableCategory);
      expect(comp.emissionFactor).toEqual(emissionFactor);
    });
  });

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

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

      // THEN
      expect(emissionFactorFormService.getEmissionFactor).toHaveBeenCalled();
      expect(comp.previousState).toHaveBeenCalled();
      expect(emissionFactorService.update).toHaveBeenCalledWith(expect.objectContaining(emissionFactor));
      expect(comp.isSaving).toEqual(false);
    });

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

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

      // THEN
      expect(emissionFactorFormService.getEmissionFactor).toHaveBeenCalled();
      expect(emissionFactorService.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<IEmissionFactor>>();
      const emissionFactor = { id: 123 };
      jest.spyOn(emissionFactorService, 'update').mockReturnValue(saveSubject);
      jest.spyOn(comp, 'previousState');
      activatedRoute.data = of({ emissionFactor });
      comp.ngOnInit();

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

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

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

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