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 { MetadataValueService } from '../service/metadata-value.service';
import { IMetadataValue } from '../metadata-value.model';
import { MetadataValueFormService } from './metadata-value-form.service';

import { MetadataValueUpdateComponent } from './metadata-value-update.component';

describe('MetadataValue Management Update Component', () => {
  let comp: MetadataValueUpdateComponent;
  let fixture: ComponentFixture<MetadataValueUpdateComponent>;
  let activatedRoute: ActivatedRoute;
  let metadataValueFormService: MetadataValueFormService;
  let metadataValueService: MetadataValueService;

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

    fixture = TestBed.createComponent(MetadataValueUpdateComponent);
    activatedRoute = TestBed.inject(ActivatedRoute);
    metadataValueFormService = TestBed.inject(MetadataValueFormService);
    metadataValueService = TestBed.inject(MetadataValueService);

    comp = fixture.componentInstance;
  });

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

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

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

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

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

      // THEN
      expect(metadataValueFormService.getMetadataValue).toHaveBeenCalled();
      expect(comp.previousState).toHaveBeenCalled();
      expect(metadataValueService.update).toHaveBeenCalledWith(expect.objectContaining(metadataValue));
      expect(comp.isSaving).toEqual(false);
    });

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

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

      // THEN
      expect(metadataValueFormService.getMetadataValue).toHaveBeenCalled();
      expect(metadataValueService.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<IMetadataValue>>();
      const metadataValue = { id: 123 };
      jest.spyOn(metadataValueService, 'update').mockReturnValue(saveSubject);
      jest.spyOn(comp, 'previousState');
      activatedRoute.data = of({ metadataValue });
      comp.ngOnInit();

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

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