const Helper = require('./helper');

describe('Helper', () => {
  it('should return pictograms 01, 02', () => {
    const ghs = [{ hazardCode: 'H220' }, { hazardCode: 'H200' }];

    expect(Helper.pictograms(ghs)).toEqual(['GHS01', 'GHS02']);
  });

  it('should return pictograms 03, 04, 05, 07, 08', () => {
    const ghs = [
      { hazardCode: 'H318' },
      { hazardCode: 'H315' },
      { hazardCode: 'H304' },
      { hazardCode: 'H280' },
      { hazardCode: 'H272' },
      { hazardCode: 'H360' },
    ];

    expect(Helper.pictograms(ghs)).toEqual(['GHS03', 'GHS04', 'GHS05', 'GHS07', 'GHS08']);
  });

  it('should find tenant code base on campus code', () => {
    expect(Helper.campusCodeToTenantCode('03')).toEqual('UC');
  });

  it('should return title case for READY_FOR_USE as Ready For Use', () => {
    expect(Helper.toTitleCase('READY_FOR_USE')).toEqual('Ready For Use');
  });

  it('should validate UC Davis barcodes', () => {
    const barcodePattern = new RegExp(Helper.getBarcodePattern({ tenantCode: 'UC', campusCode: '03' }));

    expect(barcodePattern.test('UC0000000001')).toBeTruthy();
    expect(barcodePattern.test('CA000000000000000002CAD4')).toBeTruthy();
    expect(barcodePattern.test('3415925DA0000000000008D4')).toBeTruthy();
    expect(barcodePattern.test('201800000000000000000020')).toBeTruthy();
    expect(barcodePattern.test('201806000123456789ABCDEF')).toBeTruthy();
  });

  it('should validate a label', () => {
    const label = {
      a: 'string',
      b: 2,
      c: false,
    };

    expect(Helper.validateLabel(label)).toBeTruthy();

    label.d = { test: 'fail' };
    expect(Helper.validateLabel(label)).toBeFalsy();

    label.d = ['fail'];
    expect(Helper.validateLabel(label)).toBeFalsy();

    label.d = () => 'fail';
    expect(Helper.validateLabel(label)).toBeFalsy();
  });

  it('should validate a date is in range 1-1-1000 -- 12-31-2999', () => {
    expect(Helper.validateDate('476-9-01')).toBeFalsy();
    expect(Helper.validateDate('1000-1-01')).toBeTruthy();
    expect(Helper.validateDate('1983-1-03')).toBeTruthy();
    expect(Helper.validateDate('3000-1-01')).toBeFalsy();
    expect(Helper.validateDate('21018-7-05')).toBeFalsy();
  });

  it('should calculate deep diff between two objects when adding fields to an object (two forms)', () => {
    const original = {
      _id: '12345',
      status: 'DRAFT',
      array: [],
      deepArray: [{ a: 1 }],
      responses: null,
    };
    const modified = {
      _id: '12345',
      status: 'COMPLETED',
      array: [1],
      deepArray: [{ a: 2 }],
      responses: {
        chemical: {
          C2: 'No',
          C3: 'No',
        },
      },
    };

    const diff = Helper.getDiff(original, modified);
    expect(diff).toEqual({
      status: 'COMPLETED',
      array: [1],
      deepArray: [{ a: 2 }],
      responses: {
        chemical: {
          C2: 'No',
          C3: 'No',
        },
      },
    });
  });

  it('should calculate deep diff between two objects with the same shape (two forms)', () => {
    const original = {
      _id: '12345',
      status: 'DRAFT',
      array: [],
      responses: {
        chemical: {
          C2: 'Yes',
          C3: 'No',
        },
      },
    };
    const modified = {
      _id: '12345',
      status: 'COMPLETED',
      array: [1],
      responses: {
        chemical: {
          C2: 'No',
          C3: 'No',
        },
      },
    };

    const diff = Helper.getDiff(original, modified);
    expect(diff).toEqual({
      status: 'COMPLETED',
      array: [1],
      responses: {
        chemical: {
          C2: 'No',
        },
      },
    });
  });
});
