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

import { sampleWithRequiredData, sampleWithNewData } from '../variable-units.test-samples';

import { VariableUnitsFormService } from './variable-units-form.service';

describe('VariableUnits Form Service', () => {
  let service: VariableUnitsFormService;

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

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            version: expect.any(Object),
            variable: expect.any(Object),
            unit: expect.any(Object),
          }),
        );
      });

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            version: expect.any(Object),
            variable: expect.any(Object),
            unit: expect.any(Object),
          }),
        );
      });
    });

    describe('getVariableUnits', () => {
      it('should return NewVariableUnits for default VariableUnits initial value', () => {
        const formGroup = service.createVariableUnitsFormGroup(sampleWithNewData);

        const variableUnits = service.getVariableUnits(formGroup) as any;

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

      it('should return NewVariableUnits for empty VariableUnits initial value', () => {
        const formGroup = service.createVariableUnitsFormGroup();

        const variableUnits = service.getVariableUnits(formGroup) as any;

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

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

        const variableUnits = service.getVariableUnits(formGroup) as any;

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

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

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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