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

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

import { VariableClassFormService } from './variable-class-form.service';

describe('VariableClass Form Service', () => {
  let service: VariableClassFormService;

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

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

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

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

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

    describe('getVariableClass', () => {
      it('should return NewVariableClass for default VariableClass initial value', () => {
        const formGroup = service.createVariableClassFormGroup(sampleWithNewData);

        const variableClass = service.getVariableClass(formGroup) as any;

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

      it('should return NewVariableClass for empty VariableClass initial value', () => {
        const formGroup = service.createVariableClassFormGroup();

        const variableClass = service.getVariableClass(formGroup) as any;

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

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

        const variableClass = service.getVariableClass(formGroup) as any;

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

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

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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