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

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

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

describe('InputDataUpload Form Service', () => {
  let service: InputDataUploadFormService;

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

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            title: expect.any(Object),
            dataFile: expect.any(Object),
            uploadFileName: expect.any(Object),
            diskFileName: expect.any(Object),
            type: expect.any(Object),
            state: expect.any(Object),
            comments: expect.any(Object),
            creationDate: expect.any(Object),
            creationUsername: expect.any(Object),
            version: expect.any(Object),
            period: expect.any(Object),
            periodVersion: expect.any(Object),
            owner: expect.any(Object),
          }),
        );
      });

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            title: expect.any(Object),
            dataFile: expect.any(Object),
            uploadFileName: expect.any(Object),
            diskFileName: expect.any(Object),
            type: expect.any(Object),
            state: expect.any(Object),
            comments: expect.any(Object),
            creationDate: expect.any(Object),
            creationUsername: expect.any(Object),
            version: expect.any(Object),
            period: expect.any(Object),
            periodVersion: expect.any(Object),
            owner: expect.any(Object),
          }),
        );
      });
    });

    describe('getInputDataUpload', () => {
      it('should return NewInputDataUpload for default InputDataUpload initial value', () => {
        const formGroup = service.createInputDataUploadFormGroup(sampleWithNewData);

        const inputDataUpload = service.getInputDataUpload(formGroup) as any;

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

      it('should return NewInputDataUpload for empty InputDataUpload initial value', () => {
        const formGroup = service.createInputDataUploadFormGroup();

        const inputDataUpload = service.getInputDataUpload(formGroup) as any;

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

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

        const inputDataUpload = service.getInputDataUpload(formGroup) as any;

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

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

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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