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

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

import { MetadataValueFormService } from './metadata-value-form.service';

describe('MetadataValue Form Service', () => {
  let service: MetadataValueFormService;

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

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            metadataPropertyCode: expect.any(Object),
            targetDomainKey: expect.any(Object),
            targetDomainId: expect.any(Object),
            value: expect.any(Object),
            version: expect.any(Object),
          }),
        );
      });

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            metadataPropertyCode: expect.any(Object),
            targetDomainKey: expect.any(Object),
            targetDomainId: expect.any(Object),
            value: expect.any(Object),
            version: expect.any(Object),
          }),
        );
      });
    });

    describe('getMetadataValue', () => {
      it('should return NewMetadataValue for default MetadataValue initial value', () => {
        const formGroup = service.createMetadataValueFormGroup(sampleWithNewData);

        const metadataValue = service.getMetadataValue(formGroup) as any;

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

      it('should return NewMetadataValue for empty MetadataValue initial value', () => {
        const formGroup = service.createMetadataValueFormGroup();

        const metadataValue = service.getMetadataValue(formGroup) as any;

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

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

        const metadataValue = service.getMetadataValue(formGroup) as any;

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

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

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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