import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';

import { IInputDataUpload } from '../input-data-upload.model';
import { sampleWithRequiredData, sampleWithNewData, sampleWithPartialData, sampleWithFullData } from '../input-data-upload.test-samples';

import { InputDataUploadService, RestInputDataUpload } from './input-data-upload.service';

const requireRestSample: RestInputDataUpload = {
  ...sampleWithRequiredData,
  creationDate: sampleWithRequiredData.creationDate?.toJSON(),
};

describe('InputDataUpload Service', () => {
  let service: InputDataUploadService;
  let httpMock: HttpTestingController;
  let expectedResult: IInputDataUpload | IInputDataUpload[] | boolean | null;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
    });
    expectedResult = null;
    service = TestBed.inject(InputDataUploadService);
    httpMock = TestBed.inject(HttpTestingController);
  });

  describe('Service methods', () => {
    it('should find an element', () => {
      const returnedFromService = { ...requireRestSample };
      const expected = { ...sampleWithRequiredData };

      service.find(123).subscribe(resp => (expectedResult = resp.body));

      const req = httpMock.expectOne({ method: 'GET' });
      req.flush(returnedFromService);
      expect(expectedResult).toMatchObject(expected);
    });

    it('should create a InputDataUpload', () => {
      const inputDataUpload = { ...sampleWithNewData };
      const returnedFromService = { ...requireRestSample };
      const expected = { ...sampleWithRequiredData };

      service.create(inputDataUpload).subscribe(resp => (expectedResult = resp.body));

      const req = httpMock.expectOne({ method: 'POST' });
      req.flush(returnedFromService);
      expect(expectedResult).toMatchObject(expected);
    });

    it('should update a InputDataUpload', () => {
      const inputDataUpload = { ...sampleWithRequiredData };
      const returnedFromService = { ...requireRestSample };
      const expected = { ...sampleWithRequiredData };

      service.update(inputDataUpload).subscribe(resp => (expectedResult = resp.body));

      const req = httpMock.expectOne({ method: 'PUT' });
      req.flush(returnedFromService);
      expect(expectedResult).toMatchObject(expected);
    });

    it('should partial update a InputDataUpload', () => {
      const patchObject = { ...sampleWithPartialData };
      const returnedFromService = { ...requireRestSample };
      const expected = { ...sampleWithRequiredData };

      service.partialUpdate(patchObject).subscribe(resp => (expectedResult = resp.body));

      const req = httpMock.expectOne({ method: 'PATCH' });
      req.flush(returnedFromService);
      expect(expectedResult).toMatchObject(expected);
    });

    it('should return a list of InputDataUpload', () => {
      const returnedFromService = { ...requireRestSample };

      const expected = { ...sampleWithRequiredData };

      service.query().subscribe(resp => (expectedResult = resp.body));

      const req = httpMock.expectOne({ method: 'GET' });
      req.flush([returnedFromService]);
      httpMock.verify();
      expect(expectedResult).toMatchObject([expected]);
    });

    it('should delete a InputDataUpload', () => {
      const expected = true;

      service.delete(123).subscribe(resp => (expectedResult = resp.ok));

      const req = httpMock.expectOne({ method: 'DELETE' });
      req.flush({ status: 200 });
      expect(expectedResult).toBe(expected);
    });

    describe('addInputDataUploadToCollectionIfMissing', () => {
      it('should add a InputDataUpload to an empty array', () => {
        const inputDataUpload: IInputDataUpload = sampleWithRequiredData;
        expectedResult = service.addInputDataUploadToCollectionIfMissing([], inputDataUpload);
        expect(expectedResult).toHaveLength(1);
        expect(expectedResult).toContain(inputDataUpload);
      });

      it('should not add a InputDataUpload to an array that contains it', () => {
        const inputDataUpload: IInputDataUpload = sampleWithRequiredData;
        const inputDataUploadCollection: IInputDataUpload[] = [
          {
            ...inputDataUpload,
          },
          sampleWithPartialData,
        ];
        expectedResult = service.addInputDataUploadToCollectionIfMissing(inputDataUploadCollection, inputDataUpload);
        expect(expectedResult).toHaveLength(2);
      });

      it("should add a InputDataUpload to an array that doesn't contain it", () => {
        const inputDataUpload: IInputDataUpload = sampleWithRequiredData;
        const inputDataUploadCollection: IInputDataUpload[] = [sampleWithPartialData];
        expectedResult = service.addInputDataUploadToCollectionIfMissing(inputDataUploadCollection, inputDataUpload);
        expect(expectedResult).toHaveLength(2);
        expect(expectedResult).toContain(inputDataUpload);
      });

      it('should add only unique InputDataUpload to an array', () => {
        const inputDataUploadArray: IInputDataUpload[] = [sampleWithRequiredData, sampleWithPartialData, sampleWithFullData];
        const inputDataUploadCollection: IInputDataUpload[] = [sampleWithRequiredData];
        expectedResult = service.addInputDataUploadToCollectionIfMissing(inputDataUploadCollection, ...inputDataUploadArray);
        expect(expectedResult).toHaveLength(3);
      });

      it('should accept varargs', () => {
        const inputDataUpload: IInputDataUpload = sampleWithRequiredData;
        const inputDataUpload2: IInputDataUpload = sampleWithPartialData;
        expectedResult = service.addInputDataUploadToCollectionIfMissing([], inputDataUpload, inputDataUpload2);
        expect(expectedResult).toHaveLength(2);
        expect(expectedResult).toContain(inputDataUpload);
        expect(expectedResult).toContain(inputDataUpload2);
      });

      it('should accept null and undefined values', () => {
        const inputDataUpload: IInputDataUpload = sampleWithRequiredData;
        expectedResult = service.addInputDataUploadToCollectionIfMissing([], null, inputDataUpload, undefined);
        expect(expectedResult).toHaveLength(1);
        expect(expectedResult).toContain(inputDataUpload);
      });

      it('should return initial array if no InputDataUpload is added', () => {
        const inputDataUploadCollection: IInputDataUpload[] = [sampleWithRequiredData];
        expectedResult = service.addInputDataUploadToCollectionIfMissing(inputDataUploadCollection, undefined, null);
        expect(expectedResult).toEqual(inputDataUploadCollection);
      });
    });

    describe('compareInputDataUpload', () => {
      it('Should return true if both entities are null', () => {
        const entity1 = null;
        const entity2 = null;

        const compareResult = service.compareInputDataUpload(entity1, entity2);

        expect(compareResult).toEqual(true);
      });

      it('Should return false if one entity is null', () => {
        const entity1 = { id: 123 };
        const entity2 = null;

        const compareResult1 = service.compareInputDataUpload(entity1, entity2);
        const compareResult2 = service.compareInputDataUpload(entity2, entity1);

        expect(compareResult1).toEqual(false);
        expect(compareResult2).toEqual(false);
      });

      it('Should return false if primaryKey differs', () => {
        const entity1 = { id: 123 };
        const entity2 = { id: 456 };

        const compareResult1 = service.compareInputDataUpload(entity1, entity2);
        const compareResult2 = service.compareInputDataUpload(entity2, entity1);

        expect(compareResult1).toEqual(false);
        expect(compareResult2).toEqual(false);
      });

      it('Should return false if primaryKey matches', () => {
        const entity1 = { id: 123 };
        const entity2 = { id: 123 };

        const compareResult1 = service.compareInputDataUpload(entity1, entity2);
        const compareResult2 = service.compareInputDataUpload(entity2, entity1);

        expect(compareResult1).toEqual(true);
        expect(compareResult2).toEqual(true);
      });
    });
  });

  afterEach(() => {
    httpMock.verify();
  });
});
