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 { IUnitType } from 'app/entities/unit-type/unit-type.model';
import { UnitTypeService } from 'app/entities/unit-type/service/unit-type.service';
import { UnitService } from '../service/unit.service';
import { IUnit } from '../unit.model';
import { UnitFormService } from './unit-form.service';

import { UnitUpdateComponent } from './unit-update.component';

describe('Unit Management Update Component', () => {
  let comp: UnitUpdateComponent;
  let fixture: ComponentFixture<UnitUpdateComponent>;
  let activatedRoute: ActivatedRoute;
  let unitFormService: UnitFormService;
  let unitService: UnitService;
  let unitTypeService: UnitTypeService;

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

    fixture = TestBed.createComponent(UnitUpdateComponent);
    activatedRoute = TestBed.inject(ActivatedRoute);
    unitFormService = TestBed.inject(UnitFormService);
    unitService = TestBed.inject(UnitService);
    unitTypeService = TestBed.inject(UnitTypeService);

    comp = fixture.componentInstance;
  });

  describe('ngOnInit', () => {
    it('Should call UnitType query and add missing value', () => {
      const unit: IUnit = { id: 456 };
      const unitType: IUnitType = { id: 19389 };
      unit.unitType = unitType;

      const unitTypeCollection: IUnitType[] = [{ id: 20356 }];
      jest.spyOn(unitTypeService, 'query').mockReturnValue(of(new HttpResponse({ body: unitTypeCollection })));
      const additionalUnitTypes = [unitType];
      const expectedCollection: IUnitType[] = [...additionalUnitTypes, ...unitTypeCollection];
      jest.spyOn(unitTypeService, 'addUnitTypeToCollectionIfMissing').mockReturnValue(expectedCollection);

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

      expect(unitTypeService.query).toHaveBeenCalled();
      expect(unitTypeService.addUnitTypeToCollectionIfMissing).toHaveBeenCalledWith(
        unitTypeCollection,
        ...additionalUnitTypes.map(expect.objectContaining),
      );
      expect(comp.unitTypesSharedCollection).toEqual(expectedCollection);
    });

    it('Should update editForm', () => {
      const unit: IUnit = { id: 456 };
      const unitType: IUnitType = { id: 6897 };
      unit.unitType = unitType;

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

      expect(comp.unitTypesSharedCollection).toContain(unitType);
      expect(comp.unit).toEqual(unit);
    });
  });

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

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

      // THEN
      expect(unitFormService.getUnit).toHaveBeenCalled();
      expect(comp.previousState).toHaveBeenCalled();
      expect(unitService.update).toHaveBeenCalledWith(expect.objectContaining(unit));
      expect(comp.isSaving).toEqual(false);
    });

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

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

      // THEN
      expect(unitFormService.getUnit).toHaveBeenCalled();
      expect(unitService.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<IUnit>>();
      const unit = { id: 123 };
      jest.spyOn(unitService, 'update').mockReturnValue(saveSubject);
      jest.spyOn(comp, 'previousState');
      activatedRoute.data = of({ unit });
      comp.ngOnInit();

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

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

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