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

import { sampleWithRequiredData, sampleWithNewData } from '../unit-type.test-samples';

import { UnitTypeFormService } from './unit-type-form.service';

describe('UnitType Form Service', () => {
  let service: UnitTypeFormService;

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

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            name: expect.any(Object),
            description: expect.any(Object),
            valueType: expect.any(Object),
            version: expect.any(Object),
          }),
        );
      });

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            name: expect.any(Object),
            description: expect.any(Object),
            valueType: expect.any(Object),
            version: expect.any(Object),
          }),
        );
      });
    });

    describe('getUnitType', () => {
      it('should return NewUnitType for default UnitType initial value', () => {
        const formGroup = service.createUnitTypeFormGroup(sampleWithNewData);

        const unitType = service.getUnitType(formGroup) as any;

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

      it('should return NewUnitType for empty UnitType initial value', () => {
        const formGroup = service.createUnitTypeFormGroup();

        const unitType = service.getUnitType(formGroup) as any;

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

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

        const unitType = service.getUnitType(formGroup) as any;

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

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

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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