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 { DashboardComponentService } from '../service/dashboard-component.service';
import { IDashboardComponent } from '../dashboard-component.model';
import { DashboardComponentFormService } from './dashboard-component-form.service';

import { DashboardComponentUpdateComponent } from './dashboard-component-update.component';

describe('DashboardComponent Management Update Component', () => {
  let comp: DashboardComponentUpdateComponent;
  let fixture: ComponentFixture<DashboardComponentUpdateComponent>;
  let activatedRoute: ActivatedRoute;
  let DashboardComponentFormService: DashboardComponentFormService;
  let DashboardComponentService: DashboardComponentService;

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

    fixture = TestBed.createComponent(DashboardComponentUpdateComponent);
    activatedRoute = TestBed.inject(ActivatedRoute);
    DashboardComponentFormService = TestBed.inject(DashboardComponentFormService);
    DashboardComponentService = TestBed.inject(DashboardComponentService);

    comp = fixture.componentInstance;
  });

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

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

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

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

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

      // THEN
      expect(DashboardComponentFormService.getDashboardComponent).toHaveBeenCalled();
      expect(comp.previousState).toHaveBeenCalled();
      expect(DashboardComponentService.update).toHaveBeenCalledWith(expect.objectContaining(DashboardComponent));
      expect(comp.isSaving).toEqual(false);
    });

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

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

      // THEN
      expect(DashboardComponentFormService.getDashboardComponent).toHaveBeenCalled();
      expect(DashboardComponentService.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<IDashboardComponent>>();
      const DashboardComponent = { id: 123 };
      jest.spyOn(DashboardComponentService, 'update').mockReturnValue(saveSubject);
      jest.spyOn(comp, 'previousState');
      activatedRoute.data = of({ DashboardComponent });
      comp.ngOnInit();

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

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