import { relativeDate } from '@/helpers/date.helper';

const relativeDateI18n = {
  MINUTE: ['minute', 'minutes'],
  HOUR: ['hour', 'hours'],
  DAY: ['day', 'days'],
  AGO: 'ago',
  IN: 'In',
  NEAR: 'less than a',
};

const relativeDateFormatFunction = (val: string | number | Date) => `${val}`;

const factory = (params: any) => {
  const now = Date.now();
  return {
    to: now,
    from: now,
    output: {
      AGO: 'VALUE TIMING OPERATOR',
      IN: 'OPERATOR VALUE TIMING',
    },
    i18n: {
      ...relativeDateI18n,
    },
    ...params,
  };
};

describe('relativeDate', () => {
  it('should return less than a minute ago', () => {
    const { value: result } = relativeDate(
      factory({ to: 1602249814871, from: 1602249814871 }),
      relativeDateFormatFunction,
    );
    expect(result).toEqual(
      `${relativeDateI18n.NEAR} ${relativeDateI18n.MINUTE[0]} ${relativeDateI18n.AGO}`,
    );
  });

  it('should return 5 minutes ago', () => {
    const { value: result } = relativeDate(
      factory({
        to: Date.now() - 300000,
      }),
      relativeDateFormatFunction,
    );

    expect(result).toEqual(
      `5 ${relativeDateI18n.MINUTE[1]} ${relativeDateI18n.AGO}`,
    );
  });

  it('should return 1 day ago', () => {
    const { value: result } = relativeDate(
      factory({
        to: Date.now() - 86400000,
      }),
      relativeDateFormatFunction,
    );

    expect(result).toEqual(
      `1 ${relativeDateI18n.DAY[0]} ${relativeDateI18n.AGO}`,
    );
  });

  it('should return in 1 hour', () => {
    const { value: result } = relativeDate(
      factory({
        to: Date.now() + 3600000,
      }),
      relativeDateFormatFunction,
    );

    expect(result).toEqual(
      `${relativeDateI18n.IN} 1 ${relativeDateI18n.HOUR[0]}`,
    );
  });

  it('should return the timestamp', () => {
    const date = Date.now() + 800000000;
    const { value: result } = relativeDate(
      factory({
        to: date,
      }),
      relativeDateFormatFunction,
    );

    expect(result).toEqual(`${date}`);
  });
});
