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 { VariableCategoryService } from '../service/variable-category.service';
import { IVariableCategory } from '../variable-category.model';
import { VariableCategoryFormService } from './variable-category-form.service';

import { VariableCategoryUpdateComponent } from './variable-category-update.component';

describe('VariableCategory Management Update Component', () => {
  let comp: VariableCategoryUpdateComponent;
  let fixture: ComponentFixture<VariableCategoryUpdateComponent>;
  let activatedRoute: ActivatedRoute;
  let variableCategoryFormService: VariableCategoryFormService;
  let variableCategoryService: VariableCategoryService;
  let variableScopeService: VariableScopeService;

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

    fixture = TestBed.createComponent(VariableCategoryUpdateComponent);
    activatedRoute = TestBed.inject(ActivatedRoute);
    variableCategoryFormService = TestBed.inject(VariableCategoryFormService);
    variableCategoryService = TestBed.inject(VariableCategoryService);
    variableScopeService = TestBed.inject(VariableScopeService);

    comp = fixture.componentInstance;
  });

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

      const variableScopeCollection: IVariableScope[] = [{ id: 6574 }];
      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({ variableCategory });
      comp.ngOnInit();

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

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

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

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

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

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

      // THEN
      expect(variableCategoryFormService.getVariableCategory).toHaveBeenCalled();
      expect(comp.previousState).toHaveBeenCalled();
      expect(variableCategoryService.update).toHaveBeenCalledWith(expect.objectContaining(variableCategory));
      expect(comp.isSaving).toEqual(false);
    });

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

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

      // THEN
      expect(variableCategoryFormService.getVariableCategory).toHaveBeenCalled();
      expect(variableCategoryService.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<IVariableCategory>>();
      const variableCategory = { id: 123 };
      jest.spyOn(variableCategoryService, 'update').mockReturnValue(saveSubject);
      jest.spyOn(comp, 'previousState');
      activatedRoute.data = of({ variableCategory });
      comp.ngOnInit();

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

      // THEN
      expect(variableCategoryService.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);
      });
    });
  });
});
