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

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

import { UnitConverterFormService } from './unit-converter-form.service';

describe('UnitConverter Form Service', () => {
  let service: UnitConverterFormService;

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

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

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

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

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

    describe('getUnitConverter', () => {
      it('should return NewUnitConverter for default UnitConverter initial value', () => {
        const formGroup = service.createUnitConverterFormGroup(sampleWithNewData);

        const unitConverter = service.getUnitConverter(formGroup) as any;

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

      it('should return NewUnitConverter for empty UnitConverter initial value', () => {
        const formGroup = service.createUnitConverterFormGroup();

        const unitConverter = service.getUnitConverter(formGroup) as any;

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

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

        const unitConverter = service.getUnitConverter(formGroup) as any;

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

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

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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