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

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

import { OutputDataSearchFormService } from './output-data-search-form.service';

describe('OutputData Search Form Service', () => {
  let service: OutputDataSearchFormService;

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

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

        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 SearchOutputData should create a new form with FormGroup', () => {
        const formGroup = service.createOutputDataSearchFormGroup(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('getOutputData', () => {
      it('should return SearchOutputData for default OutputData initial value', () => {
        const formGroup = service.createOutputDataSearchFormGroup(sampleWithNewData);

        const outputData = service.getOutputData(formGroup) as any;

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

      it('should return SearchOutputData for empty OutputData initial value', () => {
        const formGroup = service.createOutputDataSearchFormGroup();

        const outputData = service.getOutputData(formGroup) as any;

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

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

        const outputData = service.getOutputData(formGroup) as any;

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

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

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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