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

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

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

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

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

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            code: expect.any(Object),
            name: expect.any(Object),
            unit: expect.any(Object),
            value: expect.any(Object),
            source: expect.any(Object),
            comments: expect.any(Object),
            variableScope: expect.any(Object),
            variableCategory: expect.any(Object),
          }),
        );
      });

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            code: expect.any(Object),
            name: expect.any(Object),
            unit: expect.any(Object),
            value: expect.any(Object),
            source: expect.any(Object),
            comments: expect.any(Object),
            variableScope: expect.any(Object),
            variableCategory: expect.any(Object),
          }),
        );
      });
    });

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

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

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

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

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

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

      it('should return IEmissionFactor', () => {
        const formGroup = service.createEmissionFactorFormGroup(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.createEmissionFactorFormGroup();
        expect(formGroup.controls.id.disabled).toBe(true);

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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