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 { VariableScopeService } from '../service/variable-scope.service';
import { IVariableScope } from '../variable-scope.model';
import { VariableScopeFormService } from './variable-scope-form.service';

import { VariableScopeUpdateComponent } from './variable-scope-update.component';

describe('VariableScope Management Update Component', () => {
  let comp: VariableScopeUpdateComponent;
  let fixture: ComponentFixture<VariableScopeUpdateComponent>;
  let activatedRoute: ActivatedRoute;
  let variableScopeFormService: VariableScopeFormService;
  let variableScopeService: VariableScopeService;

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

    fixture = TestBed.createComponent(VariableScopeUpdateComponent);
    activatedRoute = TestBed.inject(ActivatedRoute);
    variableScopeFormService = TestBed.inject(VariableScopeFormService);
    variableScopeService = TestBed.inject(VariableScopeService);

    comp = fixture.componentInstance;
  });

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

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

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

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

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

      // THEN
      expect(variableScopeFormService.getVariableScope).toHaveBeenCalled();
      expect(comp.previousState).toHaveBeenCalled();
      expect(variableScopeService.update).toHaveBeenCalledWith(expect.objectContaining(variableScope));
      expect(comp.isSaving).toEqual(false);
    });

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

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

      // THEN
      expect(variableScopeFormService.getVariableScope).toHaveBeenCalled();
      expect(variableScopeService.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<IVariableScope>>();
      const variableScope = { id: 123 };
      jest.spyOn(variableScopeService, 'update').mockReturnValue(saveSubject);
      jest.spyOn(comp, 'previousState');
      activatedRoute.data = of({ variableScope });
      comp.ngOnInit();

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

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