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

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

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

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

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

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

        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 SearchInputData should create a new form with FormGroup', () => {
        const formGroup = service.createInputDataSearchFormGroup(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 SearchInputData for default InputData initial value', () => {
        const formGroup = service.createInputDataSearchFormGroup(sampleWithNewData);

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

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

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

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

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

      it('should return SearchInputData', () => {
        const formGroup = service.createInputDataSearchFormGroup(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.createInputDataSearchFormGroup();
        expect(formGroup.controls.id.disabled).toBe(true);

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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