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

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

import { InputDataFormService } from './input-data-form.service';

describe('InputData Form Service', () => {
  let service: InputDataFormService;

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

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            sourceValue: expect.any(Object),
            variableValue: expect.any(Object),
            imputedValue: expect.any(Object),
            sourceType: expect.any(Object),
            dataDate: expect.any(Object),
            changeDate: expect.any(Object),
            changeUsername: expect.any(Object),
            dataSource: expect.any(Object),
            dataUser: expect.any(Object),
            dataComments: expect.any(Object),
            creationDate: expect.any(Object),
            creationUsername: expect.any(Object),
            version: expect.any(Object),
            variable: expect.any(Object),
            period: expect.any(Object),
            periodVersion: expect.any(Object),
            sourceUnit: expect.any(Object),
            unit: expect.any(Object),
            owner: expect.any(Object),
            sourceInputData: expect.any(Object),
            sourceInputDataUpload: expect.any(Object),
          }),
        );
      });

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            sourceValue: expect.any(Object),
            variableValue: expect.any(Object),
            imputedValue: expect.any(Object),
            sourceType: expect.any(Object),
            dataDate: expect.any(Object),
            changeDate: expect.any(Object),
            changeUsername: expect.any(Object),
            dataSource: expect.any(Object),
            dataUser: expect.any(Object),
            dataComments: expect.any(Object),
            creationDate: expect.any(Object),
            creationUsername: expect.any(Object),
            version: expect.any(Object),
            variable: expect.any(Object),
            period: expect.any(Object),
            periodVersion: expect.any(Object),
            sourceUnit: expect.any(Object),
            unit: expect.any(Object),
            owner: expect.any(Object),
            sourceInputData: expect.any(Object),
            sourceInputDataUpload: expect.any(Object),
          }),
        );
      });
    });

    describe('getInputData', () => {
      it('should return NewInputData for default InputData initial value', () => {
        const formGroup = service.createInputDataFormGroup(sampleWithNewData);

        const inputData = service.getInputData(formGroup) as any;

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

      it('should return NewInputData for empty InputData initial value', () => {
        const formGroup = service.createInputDataFormGroup();

        const inputData = service.getInputData(formGroup) as any;

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

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

        const inputData = service.getInputData(formGroup) as any;

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

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

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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