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

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

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

describe('Variable Form Service', () => {
  let service: VariableFormService;

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

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            code: expect.any(Object),
            variableIndex: expect.any(Object),
            name: expect.any(Object),
            description: expect.any(Object),
            input: expect.any(Object),
            inputMode: expect.any(Object),
            output: expect.any(Object),
            outputFormula: expect.any(Object),
            version: expect.any(Object),
            baseUnit: expect.any(Object),
            variableScope: expect.any(Object),
            variableCategory: expect.any(Object),
          }),
        );
      });

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            code: expect.any(Object),
            variableIndex: expect.any(Object),
            name: expect.any(Object),
            description: expect.any(Object),
            input: expect.any(Object),
            inputMode: expect.any(Object),
            output: expect.any(Object),
            outputFormula: expect.any(Object),
            version: expect.any(Object),
            baseUnit: expect.any(Object),
            variableScope: expect.any(Object),
            variableCategory: expect.any(Object),
          }),
        );
      });
    });

    describe('getVariable', () => {
      it('should return NewVariable for default Variable initial value', () => {
        const formGroup = service.createVariableFormGroup(sampleWithNewData);

        const variable = service.getVariable(formGroup) as any;

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

      it('should return NewVariable for empty Variable initial value', () => {
        const formGroup = service.createVariableFormGroup();

        const variable = service.getVariable(formGroup) as any;

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

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

        const variable = service.getVariable(formGroup) as any;

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

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

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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