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

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

import { VariableScopeFormService } from './variable-scope-form.service';

describe('VariableScope Form Service', () => {
  let service: VariableScopeFormService;

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

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

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

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

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

    describe('getVariableScope', () => {
      it('should return NewVariableScope for default VariableScope initial value', () => {
        const formGroup = service.createVariableScopeFormGroup(sampleWithNewData);

        const variableScope = service.getVariableScope(formGroup) as any;

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

      it('should return NewVariableScope for empty VariableScope initial value', () => {
        const formGroup = service.createVariableScopeFormGroup();

        const variableScope = service.getVariableScope(formGroup) as any;

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

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

        const variableScope = service.getVariableScope(formGroup) as any;

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

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

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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