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 { UnitTypeService } from '../service/unit-type.service';
import { IUnitType } from '../unit-type.model';
import { UnitTypeFormService } from './unit-type-form.service';

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

describe('UnitType Management Update Component', () => {
  let comp: UnitTypeUpdateComponent;
  let fixture: ComponentFixture<UnitTypeUpdateComponent>;
  let activatedRoute: ActivatedRoute;
  let unitTypeFormService: UnitTypeFormService;
  let unitTypeService: UnitTypeService;

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

    fixture = TestBed.createComponent(UnitTypeUpdateComponent);
    activatedRoute = TestBed.inject(ActivatedRoute);
    unitTypeFormService = TestBed.inject(UnitTypeFormService);
    unitTypeService = TestBed.inject(UnitTypeService);

    comp = fixture.componentInstance;
  });

  describe('ngOnInit', () => {
    it('Should update editForm', () => {
      const unitType: IUnitType = { id: 456 };

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

      expect(comp.unitType).toEqual(unitType);
    });
  });

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

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

      // THEN
      expect(unitTypeFormService.getUnitType).toHaveBeenCalled();
      expect(comp.previousState).toHaveBeenCalled();
      expect(unitTypeService.update).toHaveBeenCalledWith(expect.objectContaining(unitType));
      expect(comp.isSaving).toEqual(false);
    });

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

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

      // THEN
      expect(unitTypeFormService.getUnitType).toHaveBeenCalled();
      expect(unitTypeService.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<IUnitType>>();
      const unitType = { id: 123 };
      jest.spyOn(unitTypeService, 'update').mockReturnValue(saveSubject);
      jest.spyOn(comp, 'previousState');
      activatedRoute.data = of({ unitType });
      comp.ngOnInit();

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

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