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

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

import { MetadataPropertyFormService } from './metadata-property-form.service';

describe('MetadataProperty Form Service', () => {
  let service: MetadataPropertyFormService;

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

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            code: expect.any(Object),
            name: expect.any(Object),
            description: expect.any(Object),
            mandatory: expect.any(Object),
            metadataType: expect.any(Object),
            pattern: expect.any(Object),
            version: expect.any(Object),
            organizationTypes: expect.any(Object),
          }),
        );
      });

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            code: expect.any(Object),
            name: expect.any(Object),
            description: expect.any(Object),
            mandatory: expect.any(Object),
            metadataType: expect.any(Object),
            pattern: expect.any(Object),
            version: expect.any(Object),
            organizationTypes: expect.any(Object),
          }),
        );
      });
    });

    describe('getMetadataProperty', () => {
      it('should return NewMetadataProperty for default MetadataProperty initial value', () => {
        const formGroup = service.createMetadataPropertyFormGroup(sampleWithNewData);

        const metadataProperty = service.getMetadataProperty(formGroup) as any;

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

      it('should return NewMetadataProperty for empty MetadataProperty initial value', () => {
        const formGroup = service.createMetadataPropertyFormGroup();

        const metadataProperty = service.getMetadataProperty(formGroup) as any;

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

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

        const metadataProperty = service.getMetadataProperty(formGroup) as any;

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

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

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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