import { FileUploadListContainerComponent, MultiFileUploadEditorComponent } from '@alfa-client/binary-file';
import { FileUploadType } from '@alfa-client/binary-file-shared';
import { existsAsHtmlElement, getElementComponentFromFixtureByCss } from '@alfa-client/test-utils';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { faker } from '@faker-js/faker/.';
import { expect } from '@jest/globals';
import { Resource } from '@ngxp/rest';
import { MockComponent } from 'ng-mocks';
import { getDataTestIdOf } from '../../../../tech-shared/test/data-test';
import { DummyLinkRel } from '../../../../tech-shared/test/dummy';
import { createDummyResource } from '../../../../tech-shared/test/resource';
import { MultiFileUploadComponent } from './multi-file-upload.component';

describe('OdsMultiFileUploadComponent', () => {
  let component: MultiFileUploadComponent;
  let fixture: ComponentFixture<MultiFileUploadComponent>;

  const fileUploadListTestId: string = getDataTestIdOf('file-list');
  const fileUploadEditorTestId: string = getDataTestIdOf('multi-file-upload-editor');

  const fileUploadType: FileUploadType = faker.word.noun();
  const uploadLinkRel: string = DummyLinkRel.DUMMY;
  const uploadResource: Resource = createDummyResource();
  const filesLinkRel: string = DummyLinkRel.DUMMY;
  const filesResource: Resource = createDummyResource();
  const formFieldsName: string = faker.word.noun();

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [MultiFileUploadComponent],
      declarations: [MockComponent(MultiFileUploadEditorComponent), MockComponent(FileUploadListContainerComponent)],
    }).compileComponents();

    fixture = TestBed.createComponent(MultiFileUploadComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  describe('template', () => {
    beforeEach(() => {
      component.filesFormFieldName = formFieldsName;
      component.fileUploadType = fileUploadType;
      component.uploadResource = uploadResource;
      component.uploadLinkRelation = uploadLinkRel;
      component.filesResource = filesResource;
      component.filesLinkRelation = filesLinkRel;
    });

    describe('file upload list', () => {
      it('should exists', () => {
        existsAsHtmlElement(fixture, fileUploadListTestId);
      });

      it('should have inputs', () => {
        fixture.detectChanges();

        const fileUploadListComponent: FileUploadListContainerComponent = getElementComponentFromFixtureByCss(
          fixture,
          fileUploadListTestId,
        );

        expect(fileUploadListComponent.parentFormArrayName).toEqual(formFieldsName);
        expect(fileUploadListComponent.fileUploadType).toEqual(fileUploadType);
        expect(fileUploadListComponent.filesResource).toEqual(filesResource);
        expect(fileUploadListComponent.filesLinkRel).toEqual(filesLinkRel);
      });
    });

    describe('file upload editor', () => {
      it('should exists', () => {
        existsAsHtmlElement(fixture, fileUploadEditorTestId);
      });

      it('should have inputs', () => {
        fixture.detectChanges();

        const fileUploadEditorComponent: MultiFileUploadEditorComponent = getElementComponentFromFixtureByCss(
          fixture,
          fileUploadEditorTestId,
        );

        expect(fileUploadEditorComponent.fileUploadType).toEqual(fileUploadType);
        expect(fileUploadEditorComponent.uploadResource).toEqual(uploadResource);
        expect(fileUploadEditorComponent.uploadLinkRelation).toEqual(uploadLinkRel);
      });
    });
  });
});