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

import { IVariableScope } from '../variable-scope.model';
import { sampleWithRequiredData, sampleWithNewData, sampleWithPartialData, sampleWithFullData } from '../variable-scope.test-samples';

import { VariableScopeService } from './variable-scope.service';

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

describe('VariableScope Service', () => {
  let service: VariableScopeService;
  let httpMock: HttpTestingController;
  let expectedResult: IVariableScope | IVariableScope[] | boolean | null;

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

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

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

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

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

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

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

      it('should not add a VariableScope to an array that contains it', () => {
        const variableScope: IVariableScope = sampleWithRequiredData;
        const variableScopeCollection: IVariableScope[] = [
          {
            ...variableScope,
          },
          sampleWithPartialData,
        ];
        expectedResult = service.addVariableScopeToCollectionIfMissing(variableScopeCollection, variableScope);
        expect(expectedResult).toHaveLength(2);
      });

      it("should add a VariableScope to an array that doesn't contain it", () => {
        const variableScope: IVariableScope = sampleWithRequiredData;
        const variableScopeCollection: IVariableScope[] = [sampleWithPartialData];
        expectedResult = service.addVariableScopeToCollectionIfMissing(variableScopeCollection, variableScope);
        expect(expectedResult).toHaveLength(2);
        expect(expectedResult).toContain(variableScope);
      });

      it('should add only unique VariableScope to an array', () => {
        const variableScopeArray: IVariableScope[] = [sampleWithRequiredData, sampleWithPartialData, sampleWithFullData];
        const variableScopeCollection: IVariableScope[] = [sampleWithRequiredData];
        expectedResult = service.addVariableScopeToCollectionIfMissing(variableScopeCollection, ...variableScopeArray);
        expect(expectedResult).toHaveLength(3);
      });

      it('should accept varargs', () => {
        const variableScope: IVariableScope = sampleWithRequiredData;
        const variableScope2: IVariableScope = sampleWithPartialData;
        expectedResult = service.addVariableScopeToCollectionIfMissing([], variableScope, variableScope2);
        expect(expectedResult).toHaveLength(2);
        expect(expectedResult).toContain(variableScope);
        expect(expectedResult).toContain(variableScope2);
      });

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

      it('should return initial array if no VariableScope is added', () => {
        const variableScopeCollection: IVariableScope[] = [sampleWithRequiredData];
        expectedResult = service.addVariableScopeToCollectionIfMissing(variableScopeCollection, undefined, null);
        expect(expectedResult).toEqual(variableScopeCollection);
      });
    });

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

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

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

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