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

import { IEmissionFactor } from '../emission-factor.model';
import { sampleWithRequiredData, sampleWithNewData, sampleWithPartialData, sampleWithFullData } from '../emission-factor.test-samples';

import { EmissionFactorService } from './emission-factor.service';

const requireRestSample: IEmissionFactor = {
  ...sampleWithRequiredData,
};

describe('EmissionFactor Service', () => {
  let service: EmissionFactorService;
  let httpMock: HttpTestingController;
  let expectedResult: IEmissionFactor | IEmissionFactor[] | boolean | null;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
    });
    expectedResult = null;
    service = TestBed.inject(EmissionFactorService);
    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 EmissionFactor', () => {
      const emissionFactor = { ...sampleWithNewData };
      const returnedFromService = { ...requireRestSample };
      const expected = { ...sampleWithRequiredData };

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

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

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

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

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

    it('should partial update a EmissionFactor', () => {
      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 EmissionFactor', () => {
      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 EmissionFactor', () => {
      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('addEmissionFactorToCollectionIfMissing', () => {
      it('should add a EmissionFactor to an empty array', () => {
        const emissionFactor: IEmissionFactor = sampleWithRequiredData;
        expectedResult = service.addEmissionFactorToCollectionIfMissing([], emissionFactor);
        expect(expectedResult).toHaveLength(1);
        expect(expectedResult).toContain(emissionFactor);
      });

      it('should not add a EmissionFactor to an array that contains it', () => {
        const emissionFactor: IEmissionFactor = sampleWithRequiredData;
        const emissionFactorCollection: IEmissionFactor[] = [
          {
            ...emissionFactor,
          },
          sampleWithPartialData,
        ];
        expectedResult = service.addEmissionFactorToCollectionIfMissing(emissionFactorCollection, emissionFactor);
        expect(expectedResult).toHaveLength(2);
      });

      it("should add a EmissionFactor to an array that doesn't contain it", () => {
        const emissionFactor: IEmissionFactor = sampleWithRequiredData;
        const emissionFactorCollection: IEmissionFactor[] = [sampleWithPartialData];
        expectedResult = service.addEmissionFactorToCollectionIfMissing(emissionFactorCollection, emissionFactor);
        expect(expectedResult).toHaveLength(2);
        expect(expectedResult).toContain(emissionFactor);
      });

      it('should add only unique EmissionFactor to an array', () => {
        const emissionFactorArray: IEmissionFactor[] = [sampleWithRequiredData, sampleWithPartialData, sampleWithFullData];
        const emissionFactorCollection: IEmissionFactor[] = [sampleWithRequiredData];
        expectedResult = service.addEmissionFactorToCollectionIfMissing(emissionFactorCollection, ...emissionFactorArray);
        expect(expectedResult).toHaveLength(3);
      });

      it('should accept varargs', () => {
        const emissionFactor: IEmissionFactor = sampleWithRequiredData;
        const emissionFactor2: IEmissionFactor = sampleWithPartialData;
        expectedResult = service.addEmissionFactorToCollectionIfMissing([], emissionFactor, emissionFactor2);
        expect(expectedResult).toHaveLength(2);
        expect(expectedResult).toContain(emissionFactor);
        expect(expectedResult).toContain(emissionFactor2);
      });

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

      it('should return initial array if no EmissionFactor is added', () => {
        const emissionFactorCollection: IEmissionFactor[] = [sampleWithRequiredData];
        expectedResult = service.addEmissionFactorToCollectionIfMissing(emissionFactorCollection, undefined, null);
        expect(expectedResult).toEqual(emissionFactorCollection);
      });
    });

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

        const compareResult = service.compareEmissionFactor(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.compareEmissionFactor(entity1, entity2);
        const compareResult2 = service.compareEmissionFactor(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.compareEmissionFactor(entity1, entity2);
        const compareResult2 = service.compareEmissionFactor(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.compareEmissionFactor(entity1, entity2);
        const compareResult2 = service.compareEmissionFactor(entity2, entity1);

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

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