import { TestBed } from '@angular/core/testing';

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

import { EmissionFactorSearchFormService } from './emission-factor-search-form.service';

describe('EmissionFactor Search Form Service', () => {
  let service: EmissionFactorSearchFormService;

  beforeEach(() => {
    TestBed.configureTestingModule({});
    service = TestBed.inject(EmissionFactorSearchFormService);
  });

  describe('Service methods', () => {
    describe('createEmissionFactorsearchFormGroup', () => {
      it('should create a new form with FormControl', () => {
        const formGroup = service.createEmissionFactorSearchFormGroup();

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            emissionFactorNameOrCode: expect.any(Object),
            year: expect.any(Object),
            code: expect.any(Object),
            name: expect.any(Object),
            unit: expect.any(Object),
            source: expect.any(Object),
            comments: expect.any(Object),
            version: expect.any(Object),
            variableScopeId: expect.any(Object),
            variableCategoryId: expect.any(Object),
          }),
        );
      });

      it('passing SearchEmissionFactor should create a new form with FormGroup', () => {
        const formGroup = service.createEmissionFactorSearchFormGroup(sampleWithRequiredData);

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            emissionFactorNameOrCode: expect.any(Object),
            year: expect.any(Object),
            code: expect.any(Object),
            name: expect.any(Object),
            unit: expect.any(Object),
            source: expect.any(Object),
            comments: expect.any(Object),
            version: expect.any(Object),
            variableScopeId: expect.any(Object),
            variableCategoryId: expect.any(Object),
          }),
        );
      });
    });

    describe('getEmissionFactor', () => {
      it('should return SearchEmissionFactor for default EmissionFactor initial value', () => {
        const formGroup = service.createEmissionFactorSearchFormGroup(sampleWithNewData);

        const emissionFactor = service.getEmissionFactor(formGroup) as any;

        expect(emissionFactor).toMatchObject(sampleWithNewData);
      });

      it('should return SearchEmissionFactor for empty EmissionFactor initial value', () => {
        const formGroup = service.createEmissionFactorSearchFormGroup();

        const emissionFactor = service.getEmissionFactor(formGroup) as any;

        expect(emissionFactor).toMatchObject({});
      });

      it('should return SearchEmissionFactor', () => {
        const formGroup = service.createEmissionFactorSearchFormGroup(sampleWithRequiredData);

        const emissionFactor = service.getEmissionFactor(formGroup) as any;

        expect(emissionFactor).toMatchObject(sampleWithRequiredData);
      });
    });

    describe('resetForm', () => {
      it('passing IEmissionFactor should not enable id FormControl', () => {
        const formGroup = service.createEmissionFactorSearchFormGroup();
        expect(formGroup.controls.id.disabled).toBe(true);

        service.resetForm(formGroup, sampleWithRequiredData);

        expect(formGroup.controls.id.disabled).toBe(true);
      });

      it('passing SearchEmissionFactor should disable id FormControl', () => {
        const formGroup = service.createEmissionFactorSearchFormGroup(sampleWithRequiredData);
        expect(formGroup.controls.id.disabled).toBe(true);

        service.resetForm(formGroup, { id: null });

        expect(formGroup.controls.id.disabled).toBe(true);
      });
    });
  });
});
