import { ServiceFileNameFactory } from '@/utils/file/ServiceFileNameFactory';
import { DocumentPrototype } from '@/interfaces/entities/Document/DocumentPrototype';
import { Application } from '@interfaces/entities/Application';

describe('ServiceFileNameFactory', () => {
  let docPrototype: DocumentPrototype, dataSource: Partial<Application>;
  beforeEach(() => {
    docPrototype = getDocumentPrototype();
    dataSource = getDataSource() ?? {};
  });

  test('ServiceFileNameFactory should create correct fileName with single link', () => {
    dataSource.common!.orderId = 1111;
    docPrototype.nameTemplate = 'RejectApplyResult-{common.orderId}.docx';
    const fileName = ServiceFileNameFactory.create(dataSource, docPrototype);
    expect(fileName).toBe(
      `RejectApplyResult-${dataSource.common?.orderId}.docx`
    );
  });

  test('ServiceFileNameFactory should create correct fileName with multiple links', () => {
    const fileName = ServiceFileNameFactory.create(dataSource, docPrototype);
    expect(fileName).toBe(
      `RejectApplyResult-${dataSource.common?.orderId}-${dataSource.id}.docx`
    );
  });

  test('ServiceFileNameFactory should create correct fileName without links', () => {
    dataSource.common = {};
    docPrototype.nameTemplate = 'RejectApplyResult-.docx';
    const fileName = ServiceFileNameFactory.create(dataSource, docPrototype);
    expect(fileName).toBe(`RejectApplyResult-.docx`);
  });

  test("ServiceFileNameFactory shouldn't crush script without nameTemplate field", () => {
    docPrototype.nameTemplate = undefined;
    const fileName = ServiceFileNameFactory.create(dataSource, docPrototype);
    expect(fileName).toBe(``);
  });
});

function getDataSource(): Partial<Application> {
  return {
    common: {
      chanel: 'ЕПГУ',
      currentDate: '24.07.2020',
      delegateType: 'BUSINESSMAN',
      dueDate: '2021-06-11T13:49:04.777+0300',
      gettingResults: { isPaperDocumentRequired: false },
      oktmo: 17701000,
      orderId: 11111111,
      userType: 'EMPLOYEE',
    },
    id: '0e846410-497dfdf6-4c4e-dfa3bc-7aca62e4b587',
  };
}
function getDocumentPrototype(): DocumentPrototype {
  return {
    attachments: [],
    code: 'RejectApplyResult',
    form: [],
    id: '4fda0811-02ea-4104-aedb-e9a033aa7719',
    isReject: true,
    isResult: true,
    modified: 1622200581,
    name: 'RejectApplyResult',
    nameTemplate: 'RejectApplyResult-{common.orderId}-{id}.docx',
    result: false,
    resultStatus: '12',
    resultType: 'rejectDoc',
    schema: 'ServiceDocument',
    source: 'result',
    stage: '1',
    stages: 'reject',
    templateFormat: 'DOCX',
    templateUrl: 'template_.docx',
    version: '41.3',
  };
}
