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 { IInputDataUpload } from 'app/entities/input-data-upload/input-data-upload.model';
import { InputDataUploadService } from 'app/entities/input-data-upload/service/input-data-upload.service';
import { InputDataUploadLogService } from '../service/input-data-upload-log.service';
import { IInputDataUploadLog } from '../input-data-upload-log.model';
import { InputDataUploadLogFormService } from './input-data-upload-log-form.service';

import { InputDataUploadLogUpdateComponent } from './input-data-upload-log-update.component';

describe('InputDataUploadLog Management Update Component', () => {
  let comp: InputDataUploadLogUpdateComponent;
  let fixture: ComponentFixture<InputDataUploadLogUpdateComponent>;
  let activatedRoute: ActivatedRoute;
  let inputDataUploadLogFormService: InputDataUploadLogFormService;
  let inputDataUploadLogService: InputDataUploadLogService;
  let inputDataUploadService: InputDataUploadService;

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

    fixture = TestBed.createComponent(InputDataUploadLogUpdateComponent);
    activatedRoute = TestBed.inject(ActivatedRoute);
    inputDataUploadLogFormService = TestBed.inject(InputDataUploadLogFormService);
    inputDataUploadLogService = TestBed.inject(InputDataUploadLogService);
    inputDataUploadService = TestBed.inject(InputDataUploadService);

    comp = fixture.componentInstance;
  });

  describe('ngOnInit', () => {
    it('Should call InputDataUpload query and add missing value', () => {
      const inputDataUploadLog: IInputDataUploadLog = { id: 456 };
      const inputDataUpload: IInputDataUpload = { id: 32591 };
      inputDataUploadLog.inputDataUpload = inputDataUpload;

      const inputDataUploadCollection: IInputDataUpload[] = [{ id: 13045 }];
      jest.spyOn(inputDataUploadService, 'query').mockReturnValue(of(new HttpResponse({ body: inputDataUploadCollection })));
      const additionalInputDataUploads = [inputDataUpload];
      const expectedCollection: IInputDataUpload[] = [...additionalInputDataUploads, ...inputDataUploadCollection];
      jest.spyOn(inputDataUploadService, 'addInputDataUploadToCollectionIfMissing').mockReturnValue(expectedCollection);

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

      expect(inputDataUploadService.query).toHaveBeenCalled();
      expect(inputDataUploadService.addInputDataUploadToCollectionIfMissing).toHaveBeenCalledWith(
        inputDataUploadCollection,
        ...additionalInputDataUploads.map(expect.objectContaining),
      );
      expect(comp.inputDataUploadsSharedCollection).toEqual(expectedCollection);
    });

    it('Should update editForm', () => {
      const inputDataUploadLog: IInputDataUploadLog = { id: 456 };
      const inputDataUpload: IInputDataUpload = { id: 14978 };
      inputDataUploadLog.inputDataUpload = inputDataUpload;

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

      expect(comp.inputDataUploadsSharedCollection).toContain(inputDataUpload);
      expect(comp.inputDataUploadLog).toEqual(inputDataUploadLog);
    });
  });

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

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

      // THEN
      expect(inputDataUploadLogFormService.getInputDataUploadLog).toHaveBeenCalled();
      expect(comp.previousState).toHaveBeenCalled();
      expect(inputDataUploadLogService.update).toHaveBeenCalledWith(expect.objectContaining(inputDataUploadLog));
      expect(comp.isSaving).toEqual(false);
    });

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

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

      // THEN
      expect(inputDataUploadLogFormService.getInputDataUploadLog).toHaveBeenCalled();
      expect(inputDataUploadLogService.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<IInputDataUploadLog>>();
      const inputDataUploadLog = { id: 123 };
      jest.spyOn(inputDataUploadLogService, 'update').mockReturnValue(saveSubject);
      jest.spyOn(comp, 'previousState');
      activatedRoute.data = of({ inputDataUploadLog });
      comp.ngOnInit();

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

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

  describe('Compare relationships', () => {
    describe('compareInputDataUpload', () => {
      it('Should forward to inputDataUploadService', () => {
        const entity = { id: 123 };
        const entity2 = { id: 456 };
        jest.spyOn(inputDataUploadService, 'compareInputDataUpload');
        comp.compareInputDataUpload(entity, entity2);
        expect(inputDataUploadService.compareInputDataUpload).toHaveBeenCalledWith(entity, entity2);
      });
    });
  });
});
