import { sortFromToDates } from './sort.util';

const A_LATEST = {
  a: { from: '20210101', to: '20210801' },
  b: { from: '20200101', to: '20200801' },
};
const A_LATEST_DAY_MISSING = {
  a: { from: '202101', to: '202108' },
  b: { from: '202001', to: '202008' },
};
const A_LATEST_DATE = {
  a: { from: new Date('2021-01-01'), to: new Date('2021-08-01') },
  b: { from: new Date('2020-01-01'), to: new Date('2020-08-01') },
};
const B_LATEST = {
  a: { from: '20200101', to: '20200801' },
  b: { from: '20210101', to: '20210801' },
};
const A_B_EQUAL = {
  a: { from: '20210101', to: '20210801' },
  b: { from: '20210101', to: '20210801' },
};
const MISSING_FROM_DATE = {
  a: { from: undefined, to: '20210801' },
  b: { from: '20200101', to: '20200801' },
};

describe('SortUtil', () => {
  describe('Sort complete from/to date-strings from latest to oldest', () => {
    it('should sort A dates before B dates when A dates are later', () => {
      expect(sortFromToDates(A_LATEST.a, A_LATEST.b)).toBe(-1);
    });

    it('should sort A dates before B dates when DAY is missing and A dates are later', () => {
      expect(sortFromToDates(A_LATEST_DAY_MISSING.a, A_LATEST_DAY_MISSING.b)).toBe(-1);
    });

    it('should sort A dates before B dates when provided JS Date and A dates are later', () => {
      expect(sortFromToDates(A_LATEST_DATE.a, A_LATEST_DATE.b)).toBe(-1);
    });

    it('should sort B from dates before A dates when B dates are later', () => {
      expect(sortFromToDates(B_LATEST.a, B_LATEST.b)).toBe(1);
    });

    it('should sort A dates before B dates when dates are equal', () => {
      expect(sortFromToDates(A_B_EQUAL.a, A_B_EQUAL.b)).toBe(0);
    });

    it('should sort A dates before B dates when some from-date is missing', () => {
      expect(sortFromToDates(MISSING_FROM_DATE.a, MISSING_FROM_DATE.b)).toBe(-1);
    });
  });
});
