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 { VariableClassTypeService } from '../service/variable-class-type.service';
import { IVariableClassType } from '../variable-class-type.model';
import { VariableClassTypeFormService } from './variable-class-type-form.service';

import { VariableClassTypeUpdateComponent } from './variable-class-type-update.component';

describe('VariableClassType Management Update Component', () => {
  let comp: VariableClassTypeUpdateComponent;
  let fixture: ComponentFixture<VariableClassTypeUpdateComponent>;
  let activatedRoute: ActivatedRoute;
  let variableClassTypeFormService: VariableClassTypeFormService;
  let variableClassTypeService: VariableClassTypeService;

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

    fixture = TestBed.createComponent(VariableClassTypeUpdateComponent);
    activatedRoute = TestBed.inject(ActivatedRoute);
    variableClassTypeFormService = TestBed.inject(VariableClassTypeFormService);
    variableClassTypeService = TestBed.inject(VariableClassTypeService);

    comp = fixture.componentInstance;
  });

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

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

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

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

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

      // THEN
      expect(variableClassTypeFormService.getVariableClassType).toHaveBeenCalled();
      expect(comp.previousState).toHaveBeenCalled();
      expect(variableClassTypeService.update).toHaveBeenCalledWith(expect.objectContaining(variableClassType));
      expect(comp.isSaving).toEqual(false);
    });

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

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

      // THEN
      expect(variableClassTypeFormService.getVariableClassType).toHaveBeenCalled();
      expect(variableClassTypeService.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<IVariableClassType>>();
      const variableClassType = { id: 123 };
      jest.spyOn(variableClassTypeService, 'update').mockReturnValue(saveSubject);
      jest.spyOn(comp, 'previousState');
      activatedRoute.data = of({ variableClassType });
      comp.ngOnInit();

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

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