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

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

import { DashboardComponentFormService } from './dashboard-component-form.service';

describe('DashboardComponent Form Service', () => {
  let service: DashboardComponentFormService;

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

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            name: expect.any(Object),
            description: expect.any(Object),
            label: expect.any(Object),
            version: expect.any(Object),
          }),
        );
      });

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

        expect(formGroup.controls).toEqual(
          expect.objectContaining({
            id: expect.any(Object),
            name: expect.any(Object),
            description: expect.any(Object),
            label: expect.any(Object),
            version: expect.any(Object),
          }),
        );
      });
    });

    describe('getDashboardComponent', () => {
      it('should return NewDashboardComponent for default DashboardComponent initial value', () => {
        const formGroup = service.createDashboardComponentFormGroup(sampleWithNewData);

        const DashboardComponent = service.getDashboardComponent(formGroup) as any;

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

      it('should return NewDashboardComponent for empty DashboardComponent initial value', () => {
        const formGroup = service.createDashboardComponentFormGroup();

        const DashboardComponent = service.getDashboardComponent(formGroup) as any;

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

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

        const DashboardComponent = service.getDashboardComponent(formGroup) as any;

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

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

        service.resetForm(formGroup, sampleWithRequiredData);

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

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

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

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