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

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

import { UnitSearchFormService } from './unit-search-form.service';

describe('Unit Search Form Service', () => {
  let service: UnitSearchFormService;

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

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

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

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

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

    describe('getUnit', () => {
      it('should return SearchUnit for default Unit initial value', () => {
        const formGroup = service.createUnitSearchFormGroup(sampleWithNewData);

        const unit = service.getUnit(formGroup) as any;

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

      it('should return SearchUnit for empty Unit initial value', () => {
        const formGroup = service.createUnitSearchFormGroup();

        const unit = service.getUnit(formGroup) as any;

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

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

        const unit = service.getUnit(formGroup) as any;

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

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

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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