/**
 * To prevent problems related to timezones (e.g. date formating), we can set
 * node timezone in jest config file. Now we are sure all tests are executed
 * on the same timezone no matter where our colleagues or our CI server are.
 *
 * // jest.config.js
 * process.env.TZ = 'UTC';
 *
 * module.exports = {
 *   // ...
 * }
 */

import {
  formatterData,
  formattedLabel,
  formatterId,
  setDomainCalc,
  textToDate,
  formatPercent
} from './formatters';

import { cleanup } from '@testing-library/react';

afterEach(cleanup);

describe('Check "utils/charts/formatters"', () => {
  it(`Check textToDate function with date`, () => {
    const textDate = '2021-03-01';
    const format = {
      month: 'short' as Intl.DateTimeFormatOptions['month']
    };
    const result = 'Mar';
    expect(textToDate(textDate, format)).toEqual(result);
  });

  it(`Check textToDate function without date`, () => {
    const textDate = 'auto';
    const format = {
      month: 'short' as Intl.DateTimeFormatOptions['month']
    };
    const result = '';
    expect(textToDate(textDate, format)).toEqual(result);
  });

  it(`Check formatterData function`, () => {
    let value = '2021-08-24T04:55:36.964Z';
    let dataKey = 'date';
    let dataFormat, result;

    dataFormat = 'month';
    result = 'Aug';
    expect(formatterData(value, dataKey, dataFormat)).toEqual(result);

    dataFormat = 'day';
    result = 'Aug 24';
    expect(formatterData(value, dataKey, dataFormat)).toEqual(result);

    dataFormat = 'hour';
    result = '04:55';
    expect(formatterData(value, dataKey, dataFormat)).toEqual(result);

    dataFormat = '';
    result = value;
    expect(formatterData(value, dataKey, dataFormat)).toEqual(result);

    /* non-date types */
    dataKey = '';

    value = 'hello';
    result = 'hello';
    expect(formatterData(value, dataKey, dataFormat)).toEqual(result);

    value = 'auto';
    result = '';
    expect(formatterData(value, dataKey, dataFormat)).toEqual(result);

    value = '12.3';
    dataFormat = 'percent';
    result = '12.3%';
    expect(formatterData(value, dataKey, dataFormat)).toEqual(result);

    value = '4500';
    dataFormat = 'money';
    result = '$4,500';
    expect(formatterData(value, dataKey, dataFormat)).toEqual(result);
  });

  it(`Check formattedLabel function`, () => {
    let label = '2021-08-23T06:00:00.000Z';
    let format = 'date';
    let dataFormat, result;

    dataFormat = 'day';
    result = 'Aug 23, 2021';
    expect(formattedLabel(label, format, dataFormat)).toEqual(result);

    dataFormat = 'month';
    result = 'Aug 2021';
    expect(formattedLabel(label, format, dataFormat)).toEqual(result);

    dataFormat = 'hour';
    result = 'Aug 23, 2021, 06:00';
    expect(formattedLabel(label, format, dataFormat)).toEqual(result);

    dataFormat = '';
    result = label;
    expect(formatterData(label, format, dataFormat)).toEqual(result);

    label = 'hello';
    format = '';
    dataFormat = '';
    result = 'hello';
    expect(formattedLabel(label, format, dataFormat)).toEqual(result);
  });

  it(`Check setDomainCalc function with calculation`, () => {
    const dataMax = 14;
    const domainCalc = true;
    const domainMax = 10;

    const result = 20;
    expect(setDomainCalc(dataMax, domainCalc, domainMax)).toEqual(result);
  });
  it(`Check setDomainCalc function without calculation`, () => {
    const dataMax = 14;
    const domainCalc = false;
    const domainMax = 10;

    const result = 10;
    expect(setDomainCalc(dataMax, domainCalc, domainMax)).toEqual(result);
  });

  it(`Check formatterId function`, () => {
    const id = 'test-id string 123';
    const result = 'test-id_string_123';
    expect(formatterId(id)).toEqual(result);
  });

  describe('formatPercent', () => {
    const tests = [
      { value: 0.005 },
      { value: 1 },
      { value: 1.5 },
      { value: 5.001 },
      { value: 5.5 },
      { value: 10 },
      { value: 99.999 },
      { value: 100 }
    ];
    it.each(tests)('should format percent correctly', ({ value }) => {
      expect(formatPercent(value)).toMatchSnapshot();
    });
  });
});
